SQL Query - CASE WHEN

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
409
Does anyone know much about writing SQL queries, I have a basic knowledge and trying unsuccessfully to do something

I have created a JOIN to a table of data but i have duplicate ID's with different corresponding values in another column

IDSystemChannel
1234511
1234512
1111111
2222212

I want to write a query that searches the ID and returns the channel as a Y or N

Code:
,	CASE			WHEN Channel = 1 Then 'Y'
			ELSE 'N'
			END As 'Online'
	,	CASE
			WHEN Channel = 2 Then 'Y'
			ELSE 'N'
			END As 'Direct'

But I'm finding that this not returning any requests on 12345 when on multiple rows applied to multiple channels?

Any ideas would be helpful, can't find anything on Google?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Based on your sample data you posted, what exactly do you want the output to look like?
 
Upvote 0
Unfortunately, I no longer have SQL available to me, so I used Access (which is pretty similar - you may need to change one or two things, but the concepts should be the same).
I nested an Aggregate Query, like this:
Code:
[COLOR=#ff0000]SELECT x.ID, IIF(x.O>0,"Y","N") as Online, IIF(x.D>0,"Y","N") as Direct
FROM[/COLOR]
[COLOR=#ff0000]([/COLOR][COLOR=#0000ff]SELECT MyTable.ID, Sum(IIf([Channel]=1,1,0)) AS O, Sum(IIf([Channel]=2,1,0)) AS D[/COLOR]
[COLOR=#0000ff]FROM MyTable[/COLOR]
[COLOR=#0000ff]GROUP BY MyTable.ID[/COLOR][COLOR=#ff0000]) as x;[/COLOR]
Note, I do not know what the name of your table is, so I used "MyTable".
 
Last edited:
Upvote 0
Unfortunately, I no longer have SQL available to me, so I used Access (which is pretty similar - you may need to change one or two things, but the concepts should be the same).
I nested an Aggregate Query, like this:
Code:
[COLOR=#ff0000]SELECT x.ID, IIF(x.O>0,"Y","N") as Online, IIF(x.D>0,"Y","N") as Direct
FROM[/COLOR]
[COLOR=#ff0000]([/COLOR][COLOR=#0000ff]SELECT MyTable.ID, Sum(IIf([Channel]=1,1,0)) AS O, Sum(IIf([Channel]=2,1,0)) AS D[/COLOR]
[COLOR=#0000ff]FROM MyTable[/COLOR]
[COLOR=#0000ff]GROUP BY MyTable.ID[/COLOR][COLOR=#ff0000]) as x;[/COLOR]
Note, I do not know what the name of your table is, so I used "MyTable".

Once I get my head round this i'll try to apply those principles, thank you
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top