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'
smantzDirector of TechnologyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
If the columns names/data types are the same then you could UNION ALL them together.

SELECT name, title
FROM x
WHERE whatever
UNION ALL
sELECT name, title
FROM x
WHERE whatever
UNION ALL
... repeat 25 times
0
Brian CroweDatabase AdministratorCommented:
Since they're all pulling from the same table just include your WHERE clause conditions in the respective CASE statements.


SELECT
   SUM(CASE WHEN ethnic='H' AND S.religion_ = 'Catholic' AND s.status = 'A' THEN 1 else 0 end) as "Hispanic Catholic"
   SUM(CASE WHEN ethnic='H' AND s.religion_ <>'Catholic' and s.religion_ <> 'NR' and s.status='A' then 1 else 0 end) as "Hispanic Other"
   SUM(CASE when ethnic='H' AND s.religion_ = 'NR' and s.status='A' then 1 else 0 end) as "Hispanic Unknown"
  ...
From Student AS s
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hnasrCommented:
You may approach this by joining student table with a religion table and status table.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

PortletPaulfreelancerCommented:
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).
0
smantzDirector of TechnologyAuthor Commented:
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
0
Brian CroweDatabase AdministratorCommented:
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"
0
PortletPaulfreelancerCommented:
>> 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
0
smantzDirector of TechnologyAuthor Commented:
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
0
PortletPaulfreelancerCommented:
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)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.