[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Multiple Select Statements in a View

Posted on 2010-11-23
8
Medium Priority
?
623 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
Industry Leaders: 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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

650 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