Solved

Multiple Select Statements in a View

Posted on 2010-11-23
8
589 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
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server - Set Value of Multiple Fields in One Query 10 24
Help Required 3 97
Better way to make a query with date filter. 5 27
T-SQL Default value in Select? 5 27
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

777 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