Query Assistance

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
I have the following Query...

Code:
SELECT [Location], Count(X) AS CountOfX
FROM TempQuery
WHERE [Region]="Madagascar" AND X="Y"
GROUP BY [Location]

I want a 3rd column which indicates the count when X='N', but I'm drawing a blank.

Any assistance would be greatly appreciated. :pray:
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Something like this maybe?

Code:
SELECT [Location], X, COUNT(X) AS CountOfX
FROM TempQuery
WHERE [Region] = "Madagascar" AND X IN ("N", "Y")
GROUP BY [Location], X
 
Upvote 0
Something like this maybe?

Code:
SELECT [Location], X, COUNT(X) AS CountOfX
FROM TempQuery
WHERE [Region] = "Madagascar" AND X IN ("N", "Y")
GROUP BY [Location], X

No, basically, the values in X are either N or Y, so I'm trying to show the split of the # of N's and the # of Y's.
 
Upvote 0
This looks good to me:
Code:
SELECT [Location], X, COUNT(X) AS CountOfX
FROM TempQuery
WHERE [Region] = "Madagascar" AND X IN ("N", "Y")
GROUP BY [Location], X

It will group all Madagascar fields into either Y or N, and report the count of each.
 
Upvote 0
Or maybe you're after a cross tab? Untested
Code:
TRANSFORM COUNT(X)
SELECT Location
FROM TempQuery
GROUP BY Location
PIVOT X

Add the "WHERE Location = 'Madagascar'" or similar criteria between the FROM & GROUP clauses

Alternative final clause : PIVOT IN ('N', 'Y')

cheers
 
Upvote 0
Or maybe you're after a cross tab? Untested
Code:
TRANSFORM COUNT(X)
SELECT Location
FROM TempQuery
GROUP BY Location
PIVOT X

cheers

I kinda like the Pivot, but is it possible to add addition columns, like [Employee] to Location and the Y/N counts?
 
Upvote 0
Hi,
I rarely write crosstab SQL by hand but if you switch into design view, it is usually quite easy to add "row" fields to the crosstab. However, this will affect the way you group the counts - it will be counts of Y and N for Madagascar by employee.
 
Upvote 0
I kinda like the Pivot, but is it possible to add addition columns, like [Employee] to Location and the Y/N counts?
For additional columns like [Employee] I assume that is like an Excel pivot table row field. If so, add to the SELECT and GROUP BY clauses,

Code:
TRANSFORM COUNT(X)
SELECT Location, Employee
FROM TempQuery
GROUP BY Location, Employee
PIVOT X

Maybe an Excel pivot table would be suitable?
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

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