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
Solved

Stored Procedure nested select statemetns

Posted on 2010-11-14
8
320 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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 (2008) Declare Variable (Date) and Set value 6 43
SQL server 2008 and after encryption method 32 61
database level memory cache..? 8 31
PolyServe for SQL server 12 30
     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 …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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 …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

791 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