?
Solved

Stored Procedure nested select statemetns

Posted on 2010-11-14
8
Medium Priority
?
330 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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 1000 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 1000 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

764 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