Link to home
Start Free TrialLog in
Avatar of smantz
smantzFlag for United States of America

asked on

Combining individual small queries into one large one?

Hi there,

Using SQL Server 2000;

I was wondering if there was a way to combine multiple small queries (27 to be exact) to create an output (table?) that could be used for a pivot table in an excel 2007?  Below is an example of 3 of the queries. 'NR' means Not Recorded.

----Hispanic "Catholic"

Select sum(case when ethnic='H' then 1 else 0 end) as "Hispanic Catholic"
From Student s
where s.religion_ ='Catholic' and s.status='A'

-----Hispanic "Other"

Select sum(case when ethnic='H' then 1 else 0 end) as "Hispanic Other"
From Student s
where s.religion_ <>'Catholic' and s.religion_ <> 'NR' and s.status='A'

-----Hispanic "Unknown"

Select sum(case when ethnic='H' then 1 else 0 end) as "Hispanic Unknown"
From Student s
where s.religion_ = 'NR' and s.status='A'
SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You may approach this by joining student table with a religion table and status table.
I agree with BriCrowe's logic, but would suggest you could filter the table for H records in the where clause as well (the others would be ignored anyway).
Avatar of smantz

ASKER

BriCrowe's solution worked well since  I was pulling the data into my spreadsheet.  Only drawback is the field headings didn't travel into the spreadsheet.  Also, if I had more time, I found the solution using UNION ALL interesting.  Thanks everyone for all the input.

-SM
How were you pulling the data into your spreadsheet?  There are a few options when saving the data as csv.

If you're just copying it from the view you can click in the grid, Ctrl-A, right-click -> Copy with Headers.

If you are right-clicking in the grid and selecting "Save Results As..." the first go to menu bar Query -> Query Options... then select the Results - Grid and check "Include column headers when copying or saving the results"
>> I found the solution using UNION ALL interesting.
oh please no....

there is simply no reason to use union here. the use of case expressions is the much more efficient approach

Lets say you are building a garden shed.
The UNION ALL approach to garden shed...

drive to timber yard
purchase timber for North wall
drive home, erect North wall

union all

drive to timber yard
purchase timber for East wall
drive home, erect East wall


union all

drive to timber yard
purchase timber for South wall
drive home, erect South wall


union all

drive to timber yard
purchase timber for West wall
drive home, erect West wall
Or, the CASE EXPRESS garden shed...
Drive to timber yard for all timber
Drive home
Erect All Walls & Roof
Avatar of smantz

ASKER

Hi again,
I'm using an OBDC connection to my SQL server and placing the query in the Query section of the  Excel  Data connection.  The results come into excel SS as columns 1-27 without the headings from the query.  I use one spreadsheet of the workbook for this and the on another spreadsheet, I reference the cells with the results.  It works.

As for using unions, I thought it was interesting, no more, no less.

--SM
UNION/UNION ALL
are useful when needed, they simply aren't needed here - that was my intent. :)

now you have the garden shed simile in your head it might help you choose when it is needed.

(e.g. if the goods you need come from 4 different suppliers/locations, union all could work)