Solved

Multiple Select Statements in a View

Posted on 2010-11-23
8
607 Views
Last Modified: 2012-05-10
I would like to create a view in MS SQL Server to return the results of multiple select statements.  When I tried to create the view only the first select statement was included.  I am trying to make it so that the view can be returned in an Excel spreadsheet.  I tried Union All and that did not work.  Can anyone recommend a solution?
0
Comment
Question by:mounty95
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34199600
A view can only return a single result set. A stored proc can return multiple, but it can be tricky to access them all from your programming environment.

A single view returns data in a specific number of columns - so if your query 1 returns
int,int,varchar
and the 2nd intended result is
money,varchar,datetime
that won't gel even using UNION all.

Either fire 2 queries from the front-end, or convolutedly merge the data by lining up the separate result set columns and casting to a type that will accept both results -- very bad idea
0
 

Author Comment

by:mounty95
ID: 34199678
Here are the first 2 queries which other than the column that the evaluations are being made on are identical.

Select FY as Roadways
     , Superior = IsNull(sum(case when roadways ='Superior' then 1 end),0)
     , SuperiorPercentage = (IsNull(sum(case when roadways ='Superior' then 1.0 end),0.0) / count(*))*100
     , Good =IsNull(sum(case when roadways ='Good' then 1 end),0)
     , GoodPercentage = (IsNull(sum(case when roadways ='Good' then 1.0 end),0.0) / count(*))*100
     , SuperiorGood = ((IsNull(sum(case when roadways ='Superior' then 1.0 end),0.0) / count(*))*100)+((IsNull(sum(case when roadways ='Good' then 1.0 end),0.0) / count(*))*100)
     , Adequate = IsNull(sum(case when roadways ='Adequate' then 1 end),0)
     , AdequatePercentage = (IsNull(sum(case when roadways ='Adequate' then 1.0 end),0.0) / count(*))*100
     ,SuperiorGoodAdequate=((IsNull(sum(case when roadways ='Superior' then 1.0 end),0.0) / count(*))*100)+((IsNull(sum(case when roadways ='Good' then 1.0 end),0.0) / count(*))*100)+((IsNull(sum(case when roadways ='Adequate' then 1.0 end),0.0) / count(*))*100)
     , NotAdequate = IsNull(sum(case when roadways ='Not Adequate' then 1 end),0)
     , NotAdequatePercentage = (IsNull(sum(case when roadways ='Not Adequate' then 1.0 end),0.0) / count(*))*100
     , Poor = IsNull(sum(case when roadways ='Poor' then 1 end),0)
     , PoorPercentage = (IsNull(sum(case when roadways ='Poor' then 1.0 end),0.0) / count(*))*100
     , NotAdequatePoor=((IsNull(sum(case when roadways ='Not Adequate' then 1.0 end),0.0) / count(*))*100)+((IsNull(sum(case when roadways ='Poor' then 1.0 end),0.0) / count(*))*100)
     ,count(*) as [Total Inspections]
FROM maintenance_survey_results
where reinspection<>'1' or reinspection is null
GROUP BY FY
ORDER BY FY

Select FY as site_appearance
     , Superior = IsNull(sum(case when site_appearance ='Superior' then 1 end),0)
     , SuperiorPercentage = (IsNull(sum(case when site_appearance ='Superior' then 1.0 end),0.0) / count(*))*100
     , Good =IsNull(sum(case when site_appearance ='Good' then 1 end),0)
     , GoodPercentage = (IsNull(sum(case when site_appearance ='Good' then 1.0 end),0.0) / count(*))*100
     , SuperiorGood = ((IsNull(sum(case when site_appearance ='Superior' then 1.0 end),0.0) / count(*))*100)+((IsNull(sum(case when site_appearance ='Good' then 1.0 end),0.0) / count(*))*100)
     , Adequate = IsNull(sum(case when site_appearance ='Adequate' then 1 end),0)
     , AdequatePercentage = (IsNull(sum(case when site_appearance ='Adequate' then 1.0 end),0.0) / count(*))*100
     ,SuperiorGoodAdequate=((IsNull(sum(case when site_appearance ='Superior' then 1.0 end),0.0) / count(*))*100)+((IsNull(sum(case when site_appearance ='Good' then 1.0 end),0.0) / count(*))*100)+((IsNull(sum(case when site_appearance ='Adequate' then 1.0 end),0.0) / count(*))*100)
     , NotAdequate = IsNull(sum(case when site_appearance ='Not Adequate' then 1 end),0)
     , NotAdequatePercentage = (IsNull(sum(case when site_appearance ='Not Adequate' then 1.0 end),0.0) / count(*))*100
     , Poor = IsNull(sum(case when site_appearance ='Poor' then 1 end),0)
     , PoorPercentage = (IsNull(sum(case when site_appearance ='Poor' then 1.0 end),0.0) / count(*))*100
     , NotAdequatePoor=((IsNull(sum(case when site_appearance ='Not Adequate' then 1.0 end),0.0) / count(*))*100)+((IsNull(sum(case when site_appearance ='Poor' then 1.0 end),0.0) / count(*))*100)
     ,count(*) as [Total Inspections]
FROM maintenance_survey_results
where reinspection<>'1' or reinspection is null
GROUP BY FY
ORDER BY FY

I am pulling these into Excel for presentation purposes only.  I have Crystal Reports available as well, but I can't see to get this to work properly there so I figured a simple pulling in of the data with all of the columns that I needed would work.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34199719
since they do line up, drop the first ORDER BY in exchange for UNION ALL
you cannot have two ORDER BY's in the same query.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mounty95
ID: 34199789
Not quite what I was looking for.  I want the select statements to return as individual grids for each select.  I didn't realize that Union was going to literally join the results...guess that is common sense.  Any other ideas?
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34199871
As noted above, a view cannot return 2 results.
Much as a Table cannot store 2 sets of data... (you could store 1 set logically broken by a column, but you get what I mean)
It is just not possible.
0
 

Author Comment

by:mounty95
ID: 34199940
Any suggestions?  Have you ever seen this need?  I understand what you mean, but there must be some way to take the individual grids that I have and somehow copy them into Excel or maybe not
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34199981
You can always fire multiple queries from Excel?
Or add a column which you can use to filter.  Copy once to Excel, then break up using VBA...

select * from
(select dataset=1, col1, col2... from ..
 union all
 select dataset=2, col1, col2... from ..) sq
0
 

Author Closing Comment

by:mounty95
ID: 34200033
Thanks for the attention to this question.  Unfortunately I don't see any easy way of handling 35 queries.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question