Solved

Stored Procedure nested select statemetns

Posted on 2010-11-14
8
325 Views
Last Modified: 2012-05-10
hi,

Can someone help me with nesting 2 select statements.  I need to use them in a procedure.
So here are the 2 select statements i first made using views.

Setup_Process_A_01B_View_All_No (this is the first view)

SELECT     CustomerNumber, MIN(StationNum) AS Expr1, ShowForDashboard
FROM         dbo.StationSetupCharacteristics
GROUP BY CustomerNumber, ShowForDashboard
HAVING      (CustomerNumber = N'100003-1-01-01-100001') AND (ShowForDashboard = N'No') AND (MIN(StationNum) = 1 OR
                      MIN(StationNum) = 2 OR
                      MIN(StationNum) = 3 OR
                      MIN(StationNum) = 4 OR
                      MIN(StationNum) = 5 OR
                      MIN(StationNum) = 6 OR
                      MIN(StationNum) = 7 OR
                      MIN(StationNum) = 8 OR
                      MIN(StationNum) = 9 OR
                      MIN(StationNum) = 10)

So i would like to nest the above inside the below select.

SELECT     dbo.StationSetupCharacteristics.CustomerNumber, dbo.StationSetupCharacteristics.StationNum, dbo.StationSetupCharacteristics.[Station Name],
                      dbo.StationSetupCharacteristics.StationGivenName
FROM         dbo.Setup_Process_A_01B_View_All_No INNER JOIN

dbo.StationSetupCharacteristics ON dbo.Setup_Process_A_01B_View_All_No.CustomerNumber = dbo.StationSetupCharacteristics.CustomerNumber AND
                      dbo.Setup_Process_A_01B_View_All_No.Expr1 = dbo.StationSetupCharacteristics.StationNum

Thanks
0
Comment
Question by:joygomez
[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
  • 3
8 Comments
 
LVL 2

Expert Comment

by:AarthiPrabakaran
ID: 34133835
use union to do the same
0
 

Author Comment

by:joygomez
ID: 34133846
Can you elaborate please.
0
 
LVL 2

Expert Comment

by:AarthiPrabakaran
ID: 34133850
Hi,
If u want results from bothe query  then use union as below :

SELECT     CustomerNumber, MIN(StationNum) AS Expr1, ShowForDashboard
FROM         dbo.StationSetupCharacteristics
GROUP BY CustomerNumber, ShowForDashboard
HAVING      (CustomerNumber = N'100003-1-01-01-100001') AND (ShowForDashboard = N'No') AND (MIN(StationNum) = 1 OR
                      MIN(StationNum) = 2 OR
                      MIN(StationNum) = 3 OR
                      MIN(StationNum) = 4 OR
                      MIN(StationNum) = 5 OR
                      MIN(StationNum) = 6 OR
                      MIN(StationNum) = 7 OR
                      MIN(StationNum) = 8 OR
                      MIN(StationNum) = 9 OR
                      MIN(StationNum) = 10)


UNION



SELECT     dbo.StationSetupCharacteristics.CustomerNumber, dbo.StationSetupCharacteristics.StationNum, dbo.StationSetupCharacteristics.[Station Name],
                      dbo.StationSetupCharacteristics.StationGivenName
FROM         dbo.Setup_Process_A_01B_View_All_No INNER JOIN

dbo.StationSetupCharacteristics ON dbo.Setup_Process_A_01B_View_All_No.CustomerNumber = dbo.StationSetupCharacteristics.CustomerNumber AND
                      dbo.Setup_Process_A_01B_View_All_No.Expr1 = dbo.StationSetupCharacteristics.StationNum

0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:joygomez
ID: 34133860
That will not work for me, because the second query is reffereing to the first one by name. -  Setup_Process_A_01B_View_All_No.

I need to have this all in a procedure without any views.  that is why i thought a nested select statement might work.

Thanks
0
 

Author Comment

by:joygomez
ID: 34133865
i also need results only from the bottom query.
0
 
LVL 2

Accepted Solution

by:
AarthiPrabakaran earned 250 total points
ID: 34133898
Hi pls check this



SELECT     dbo.StationSetupCharacteristics.CustomerNumber, dbo.StationSetupCharacteristics.StationNum, dbo.StationSetupCharacteristics.[Station Name],
                      dbo.StationSetupCharacteristics.StationGivenName
FROM        

dbo.StationSetupCharacteristics
INNER JOIN (SELECT     CustomerNumber, MIN(StationNum) AS Expr1, ShowForDashboard
FROM         dbo.StationSetupCharacteristics
GROUP BY CustomerNumber, ShowForDashboard
HAVING      (CustomerNumber = N'100003-1-01-01-100001') AND (ShowForDashboard = N'No') AND (MIN(StationNum) = 1 OR
                      MIN(StationNum) = 2 OR
                      MIN(StationNum) = 3 OR
                      MIN(StationNum) = 4 OR
                      MIN(StationNum) = 5 OR
                      MIN(StationNum) = 6 OR
                      MIN(StationNum) = 7 OR
                      MIN(StationNum) = 8 OR
                      MIN(StationNum) = 9 OR
                      MIN(StationNum) = 10)
) as t




 ON t.CustomerNumber = dbo.StationSetupCharacteristics.CustomerNumber AND
                      t.Expr1 = dbo.StationSetupCharacteristics.StationNum
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 250 total points
ID: 34135252
For your specific query, PLEASE do not use a HAVING clause to filter on columns that you can equally filter using WHERE.
WHERE will always be faster.
This is the equivalent of your query, much simpler

      SELECT  CustomerNumber, MIN(StationNum) AS Expr1, ShowForDashboard
      FROM    dbo.StationSetupCharacteristics
      WHERE   (CustomerNumber = N'100003-1-01-01-100001') AND (ShowForDashboard = N'No')
      --GROUP BY CustomerNumber, ShowForDashboard -- the filter ensures only one group
      HAVING MIN(StationNum) between 1 and 10

You plug it in exactly where you had the view:

SELECT B.CustomerNumber, B.StationNum, B.[Station Name], B.StationGivenName
FROM (
      SELECT  CustomerNumber, MIN(StationNum) AS Expr1, ShowForDashboard
      FROM    dbo.StationSetupCharacteristics
      WHERE   (CustomerNumber = N'100003-1-01-01-100001') AND (ShowForDashboard = N'No')
      --GROUP BY CustomerNumber, ShowForDashboard -- the filter ensures only one group
      HAVING MIN(StationNum) between 1 and 10
) A
INNER JOIN dbo.StationSetupCharacteristics B
      ON A.CustomerNumber = B.CustomerNumber AND A.Expr1 = B.StationNum

For a general form if you required more Customers, then

SELECT B.CustomerNumber, B.StationNum, B.[Station Name], B.StationGivenName
FROM (
      SELECT  CustomerNumber, MIN(StationNum) AS Expr1--, ShowForDashboard -- last one is unused
      FROM    dbo.StationSetupCharacteristics
      --WHERE   (CustomerNumber = N'100003-1-01-01-100001') AND (ShowForDashboard = N'No')
      GROUP BY CustomerNumber, ShowForDashboard
      HAVING MIN(StationNum) between 1 and 10
) A
INNER JOIN dbo.StationSetupCharacteristics B
      ON A.CustomerNumber = B.CustomerNumber AND A.Expr1 = B.StationNum
0
 

Author Closing Comment

by:joygomez
ID: 34137334
thanks both
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

687 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