I have a SQL query that pull data from the SQL Server. I need to group a field (Alpha field) and if there are more than one record, display 'NO MATCH'. This query will help users spot jobs with a missing Alpha value.
Here is the query to get the data with the grouping:
select name,Alpha from T
where name = 'John'
GROUP BY name, Alpha
Here are the results from the query:
name Alpha
John 4
John 7
John 20
I am trying to get the result to say 'NO MATCH' if the data doesn't match. The example above have three records that do not match. How do I get the result to be 'No match'? If all records are a match display the alpha value.
Here is the link to the SQL Fiddle I created with the example:
http://sqlfiddle.com/#!18/ccd77/1/0
2条答案
按热度按时间ssgvzors1#
It's possibly more performant to use
MIN = MAX
instead ofCOUNT(DISTINCT
.This has the same result in this case, but does not require a sort on
Alpha
.gkl3eglg2#
I have added two more records having same
Alpha
filed:You can use COUNT with DISTINCT to check if all values are the same:
gives: