[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

combining sql statements

Posted on 2014-08-28
6
Medium Priority
?
128 Views
Last Modified: 2014-08-29
I need to combine these three sql statements.


select a.CustomerID, PartNumber as [Part Number], SUM(Quantity) as Submitted
from ServiceRequest a , Frus b
where a.FrusID = b.FrusID and SRequestStatusID=1
group by a.CustomerID, PartNumber


select a.CustomerID, PartNumber as [Part Number], Count([Part #]) as [Due In]
from OutstandingEquipmentReport a, Frus b
where a.[Part #] = b.PartNumber
group by a.CustomerID, PartNumber


select a.[CustomerID]
      ,b.[PartNumber]
        ,a.[AWAITING REPAIR]
      ,a.[IN PROCESS]
      ,a.[AVAILABLE]
from HotSwapOnsiteInventoryReport a , Frus b
where a.[Part #] = b.PartNumber

The goal is to have the query to look like

Part Number        Submitted        Due In    Awaiting Repair    In Process     Available     Total(excluding submitted)

The information needs to reference the frus table because the other tables might not contain information.
0
Comment
Question by:JDay2
  • 3
  • 2
6 Comments
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40291280
This should be close.  I had to guess at which table some columns came from.  I also assumed that the OutstandingEquipment table had only a single row per part# -- if that's not true, that code will need adjusted.

SELECT
    f.PartNumber AS [Part Number],
    COALESCE(sr.CustomerID, oer.CustomerID, hso.CustomerID) AS CustomerID,
    SUM(sr.Quantity) AS Submitted,
    SUM(CASE WHEN oer.[Part #] IS NULL THEN 0 ELSE 1 END) AS [Due In],
    MAX(hso.[AWAITING REPAIR]) AS [AWAITING REPAIR],
    MAX(hso.[IN PROCESS]) AS [IN PROCESS],
    MAX(hso.[AVAILABLE]) AS [AVAILABLE]
FROM Frus f
LEFT OUTER JOIN ServiceRequest sr ON
    sr.FrusID = f.FrusID AND
    sr.SRequestStatusID = 1
FULL OUTER JOIN OutstandingEquipmentReport oer ON
    oer.[Part #] = f.PartNumber AND
    oer.CustomerID = sr.CustomerID
FULL OUTER JOIN HotSwapOnsiteInventoryReport hso ON
    hso.[Part #] = f.PartNumber AND
    hso.CustomerID = COALESCE(sr.CustomerID, oer.CustomerID)
GROUP BY
    f.PartNumber,
    COALESCE(sr.CustomerID, oer.CustomerID, hso.CustomerID)
0
 
LVL 53

Expert Comment

by:Vitor Montalvão
ID: 40292077
Try this:
WITH Submitted_CTE (CustomerID, PartNumber,Submitted)
AS 
( select a.CustomerID, PartNumber, SUM(Quantity) 
 from ServiceRequest a , Frus b
 where a.FrusID = b.FrusID and SRequestStatusID=1 
 group by a.CustomerID, PartNumber),

Due_CTE (CustomerID, PartNumber,DueIn)
AS
( select a.CustomerID, PartNumber, Count([Part #]) 
 from OutstandingEquipmentReport a, Frus b
 where a.[Part #] = b.PartNumber
 group by a.CustomerID, PartNumber)

select a.[CustomerID]
       ,a.[Part #] [Part Number]
       , s.Submitted        
       ,d.DueIn [Due In]    
       ,a.[AWAITING REPAIR]
       ,a.[IN PROCESS]
       ,a.[AVAILABLE]
      ,d.DueIn+a.[AWAITING REPAIR]+a.[IN PROCESS]+a.[AVAILABLE] Total
from HotSwapOnsiteInventoryReport a
inner join Submitted_CTE s
      on a.[Part #] = s.PartNumber
inner join Due_CTE d
       on a.[Part #] = d.PartNumber

Open in new window

0
 

Author Comment

by:JDay2
ID: 40292496
Ok the second comes closer than the first.  I had to modify the inner joins to left outer but I need all the parts from the frus table.  There might be a scenario where there is no data in all three tables.  How do I accomplish that?  

The first query accomplishes that information but it does total the submitted and Due in columns correctly.

WITH Submitted_CTE (CustomerID, PartNumber,Submitted)
AS 
( select a.CustomerID, PartNumber, SUM(Quantity) 
 from ServiceRequest a , Frus b
 where a.FrusID = b.FrusID and SRequestStatusID=1 
 group by a.CustomerID, PartNumber),

Due_CTE (CustomerID, PartNumber,DueIn)
AS
( select a.CustomerID, PartNumber, Count([Part #]) 
 from OutstandingEquipmentReport a, Frus b
 where a.[Part #] = b.PartNumber
 group by a.CustomerID, PartNumber)

select a.[CustomerID]
       ,a.[Part #] [Part Number]
       ,s.Submitted        
       ,d.DueIn [Due In]    
       ,a.[AWAITING REPAIR]
       ,a.[IN PROCESS]
       ,a.[AVALIABLE]
      ,d.DueIn+a.[AWAITING REPAIR]+a.[IN PROCESS]+a.[AVALIABLE] Total
from HotSwapOnsiteInventoryReport a
left outer join Submitted_CTE s
      on a.[Part #] = s.PartNumber
left outer join Due_CTE d
       on a.[Part #] = d.PartNumber

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 40292518
Then add the Frus table to the select:
WITH Submitted_CTE (CustomerID, PartNumber,Submitted)
AS 
( select a.CustomerID, PartNumber, SUM(Quantity) 
 from ServiceRequest a , Frus b
 where a.FrusID = b.FrusID and SRequestStatusID=1 
 group by a.CustomerID, PartNumber),

Due_CTE (CustomerID, PartNumber,DueIn)
AS
( select a.CustomerID, PartNumber, Count([Part #]) 
 from OutstandingEquipmentReport a, Frus b
 where a.[Part #] = b.PartNumber
 group by a.CustomerID, PartNumber)

select a.[CustomerID]
       ,b.PartNumber
       ,s.Submitted        
       ,d.DueIn [Due In]    
       ,a.[AWAITING REPAIR]
       ,a.[IN PROCESS]
       ,a.[AVALIABLE]
      ,d.DueIn+a.[AWAITING REPAIR]+a.[IN PROCESS]+a.[AVALIABLE] Total
from frus b
left outer join HotSwapOnsiteInventoryReport a
      on b.PartNumber =  a.[Part #]
left outer join Submitted_CTE s
      on b.PartNumber = s.PartNumber
left outer join Due_CTE d
       on b.PartNumber = d.PartNumber

Open in new window

0
 

Author Comment

by:JDay2
ID: 40292741
Is there a statement to grab everything from the left table even if it is not in the other three?
0
 
LVL 53

Expert Comment

by:Vitor Montalvão
ID: 40292761
LEFT OUTER JOIN do that. But if you want to see more data then add the columns from the left table in the SELECT statement. In your query only Frus.PartNumber is there so add the columns that you think are relevant to you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

830 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