Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Quick question on a simple query

Author  Topic 

jay83091
Starting Member

14 Posts

Posted - 2014-05-08 : 02:32:14
Hi All,

Could you please help with writing a SELECT statement for these conditions?? Thanks for your help in advance.

Data Set

Customer_ID | Customer_type
123 | B
123 | A
124 | A
125 | B

Rules are;
- If a given Customer_ID has more than one customer types (i.e. Customer_ID = 123 above) then always use the line where customer_type = A

- If there are no multiple customer types then do nothing (i.e. use the record available)

The expected result set would look like;
Customer_ID | Customer_type
123 | A
124 | A
125 | B


khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-05-08 : 02:41:16
[code]
SELECT Customer_ID, MIN(Customer_type)
FROM yourtable
GROUP BY Customer_ID
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jay83091
Starting Member

14 Posts

Posted - 2014-05-08 : 02:49:57
Thanks for your reply. are there any other approaches without using 'min'? or count?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-05-08 : 03:08:06
why can't you use MIN() ? smells like homework


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-05-08 : 03:17:08
you can also use ROW_NUMBER


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -