Solved

Stored Procedure nested select statemetns

Posted on 2010-11-14
8
317 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
  • 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
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.

 

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

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

     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 …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now