Solved

Stored Procedure nested select statemetns

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

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
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…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

21 Experts available now in Live!

Get 1:1 Help Now