Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

4 queries into 1???

Status
Not open for further replies.

SQLNewbiexaus

Technical User
Feb 18, 2011
3
NZ
I have these 4 queries but I am new to SQL and I can't figure out how to put them into 1 table. Can someone please help?

Select
count (P.Pass) PastLight
From
Transactions T (nolock)
Inner Join
Invoicing I (nolock) on T.InvoicingID = I.InvoicingID
Inner Join
Passes P (Nolock) on I.Pass = P.Pass
Where
T.DateTime > '20110130'
And
P.UseForPastTrips = '0'
And
Overall < '4'

Select
count (P.Pass) PastHeavy
From
Transactions T (nolock)
Inner Join
Invoicing I (nolock) on T.InvoicingID = I.InvoicingID
Inner Join
Passes P (Nolock) on I.Pass = P.Pass
Where
T.DateTime > '20110130'
And
P.UseForPastTrips = '0'
And
Overall > '3'

Select
count (P.Pass) FutureLight
From
Transactions T (nolock)
Inner Join
Invoicing I (nolock) on T.InvoicingID = I.InvoicingID
Inner Join
Passes P (Nolock) on I.Pass = P.Pass
Where
T.DateTime > '20110130'
And
P.UseForPastTrips = '1'
And
Overall < '4'

Select
count (P.Pass) FutureHeavy
From
Transactions T (nolock)
Inner Join
Invoice I (nolock) on T.Invoice = I.Invoice
Inner Join
pass P (Nolock) on I.Pass = P.Pass
Where
T.DateTime > '20110130'
And
P.past = '1'
And
Overall > '3'
 
Code:
SELECT COUNT(CASE WHEN P.UseForPastTrips = '0'
                   AND Overall < '4'
                  THEN P.Pass ELSE NULL END) PastLight
     , COUNT(CASE WHEN P.UseForPastTrips = '0'
                   AND Overall > '3'
                  THEN P.Pass ELSE NULL END) PastHeavy
     , COUNT(CASE WHEN P.UseForPastTrips = '1'
                   AND Overall < '4'
                  THEN P.Pass ELSE NULL END) FutureLight
     , COUNT(CASE WHEN P.UseForPastTrips = '0'
                   AND Overall > '3'
                  THEN P.Pass ELSE NULL END) FutureHeavy
  FROM Transactions T 
INNER 
  JOIN Invoicing I
    ON I.InvoicingID = T.InvoicingID
INNER 
  JOIN Passes P 
    ON P.Pass = I.Pass
 WHERE T.DateTime > '20110130'
:)

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top