Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

combine 2 rows into 1 row Expand / Collapse
Author
Message
Posted Tuesday, October 30, 2012 6:37 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 24, 2014 8:50 AM
Points: 46, Visits: 51
Hello All,
I hope someone can help me out here.
Currently my script will bring back 2 rows, all the fields in both rows are identical except for the Source field, this field is either "Labor" or "Parts" and "Total Amount"

I need to combine both rows into one row.
EquipID Location Cycles DateTimeStamp Source Total Amount
JRH-681 Daytona 2079347 2012-01-17 06:36:22.897 Labor 2237.5
JRH-681 Daytona 2079347 2012-01-17 06:36:22.897 Parts 27791.85
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME

SET @StartDate = '1/1/2012'
SET @EndDate = '1/31/2012'

SELECT B.EngEquipID
,B.Location
,B.Cycles
,B.DateTimeStamp
,B.Source
,SUM(B.TRANS_tambase_amt) as [Total Amount]
FROM (
SELECT ER.EngEquipID
,ER.Location
,ER.Cycles
,ER.DateTimeStamp
,A.Source
,A.TRANS_tambase_amt
FROM Engine_Prod.mc.WORKORDER WO
LEFT OUTER JOIN MSM.dbo.tblEngRunData ER ON ER.WONumber = ltrim(rtrim(WO.id))
LEFT OUTER JOIN mc.WORKTASK WT ON WO.wordoi = WT.wrkord_oi
LEFT OUTER JOIN mc.WORKORDERTYPE WOT ON WO.wot_oi = WOT.wotyoi
LEFT OUTER JOIN (
SELECT 'Labor' Source
,CHARGEINF.wt_oi
,LABORTRANS.tambase_amt TRANS_tambase_amt
FROM mc.CHARGEINF CHARGEINF
INNER JOIN mc.lbtr_chgs lbtr_chgs ON CHARGEINF.chgioi = lbtr_chgs.ref_oi
INNER JOIN mc.LABORTRANS LABORTRANS ON lbtr_chgs.owner_oi = LABORTRANS.lbtroi
WHERE LABORTRANS.posted = 1

UNION ALL

SELECT 'Parts' Source
,CHARGEINF.wt_oi
,INVTRANS.tambase_amt TRANS_tambase_amt
FROM mc.CHARGEINF CHARGEINF
INNER JOIN mc.intr_chgs intr_chgs ON CHARGEINF.chgioi = intr_chgs.ref_oi
INNER JOIN mc.INVTRANS INVTRANS ON intr_chgs.owner_oi = INVTRANS.introi
WHERE INVTRANS.posted = 1
) A ON WT.wtskoi = A.wt_oi
WHERE ER.DateTimeStamp BETWEEN @StartDate
AND @EndDate
AND WO.clsdt_date BETWEEN @StartDate
AND @EndDate
AND WT.ci_cnc_date IS NULL
AND WT.tmplt = 0
AND A.Source is not null
GROUP BY ER.EngEquipID
,ER.Location
,ER.Cycles
,ER.DateTimeStamp
,A.Source
,A.TRANS_tambase_amt

) B

GROUP BY B.EngEquipID
,B.Location
,B.Cycles
,B.DateTimeStamp
,B.Source

Post #1378732
Posted Tuesday, October 30, 2012 7:21 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, April 24, 2014 9:12 AM
Points: 285, Visits: 504
What do you want the "Source" field to say when the 2 rows are combined? I'm assuming the TotalAmount is summed but we can't really sum the source field. Can you give samples of what you're looking for as a result.

---------------------------------------------------------------
Mike Hahn - Future MCM 2025
Right way to ask for help!!
http://www.sqlservercentral.com/articles/Best+Practices/61537/
I post so I can see my avatar
I want a personal webpage
I want to win the lotto
I want a gf like Tiffa
Post #1378760
Posted Tuesday, October 30, 2012 7:21 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 8:48 AM
Points: 1,872, Visits: 2,999
Just remove the B.Source field from your outer SELECT & GROUP BY
Post #1378761
Posted Tuesday, October 30, 2012 7:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:38 AM
Points: 5,143, Visits: 4,942
What do you need the output to be

EquipID Location Cycles DateTimeStamp LaborAmount PartsAmount TotalAmount
JRH-681 Daytona 2079347 2012-01-17 06:36:22.897 2237.5 27791.85 30029.35

EquipID Location Cycles DateTimeStamp TotalAmount
JRH-681 Daytona 2079347 2012-01-17 06:36:22.897 30029.35






Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1378776
Posted Tuesday, October 30, 2012 8:37 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 24, 2014 8:50 AM
Points: 46, Visits: 51
This is what I'd want my output to be.
From:
JRH-681 Daytona 2079347 2012-01-17 06:36:22.897 Labor 2237.5
JRH-681 Daytona 2079347 2012-01-17 06:36:22.897 Parts 27791.85

To:
JRH-681 Daytona 2079347 2012-01-17 06:36:22.897 Labor 2237.5 Parts 27791.85

source amount source amount
So Labor 2237 and parts 27791 are on the same line, with the rest of the info.
Post #1378803
Posted Tuesday, October 30, 2012 8:42 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 24, 2014 8:50 AM
Points: 46, Visits: 51
Doing that just gives me a total of 30029.35, both parts and labor added together, I need them separate fields/columns

Parts 27791.85 labor 2237.50
Post #1378808
Posted Tuesday, October 30, 2012 8:42 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:16 PM
Points: 3,373, Visits: 7,290
EDIT: Nevermind, I hadn't examined your code.


Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1378810
Posted Tuesday, October 30, 2012 8:45 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:38 AM
Points: 5,143, Visits: 4,942
Based on your original code and if you only ever have parts and labor then the below should work.

Not tested due to not having the DDL of the tables and sample data but the general idea is there for you to tweak.

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME

SET @StartDate = '1/1/2012'
SET @EndDate = '1/31/2012'

SELECT B.EngEquipID
,B.Location
,B.Cycles
,B.DateTimeStamp
,'Labor' AS Labour
,CASE WHEN B.Source = 'Labor' THEN SUM(B.TRANS_tambase_amt) END as [Labor Total Amount]
,'Parts' AS Parts
,CASE WHEN B.Source = 'Parts' THEN SUM(B.TRANS_tambase_amt) END as [Parts Total Amount]
FROM (
SELECT ER.EngEquipID
,ER.Location
,ER.Cycles
,ER.DateTimeStamp
,A.Source
,A.TRANS_tambase_amt
FROM Engine_Prod.mc.WORKORDER WO
LEFT OUTER JOIN MSM.dbo.tblEngRunData ER ON ER.WONumber = ltrim(rtrim(WO.id))
LEFT OUTER JOIN mc.WORKTASK WT ON WO.wordoi = WT.wrkord_oi
LEFT OUTER JOIN mc.WORKORDERTYPE WOT ON WO.wot_oi = WOT.wotyoi
LEFT OUTER JOIN (
SELECT 'Labor' Source
,CHARGEINF.wt_oi
,LABORTRANS.tambase_amt TRANS_tambase_amt
FROM mc.CHARGEINF CHARGEINF
INNER JOIN mc.lbtr_chgs lbtr_chgs ON CHARGEINF.chgioi = lbtr_chgs.ref_oi
INNER JOIN mc.LABORTRANS LABORTRANS ON lbtr_chgs.owner_oi = LABORTRANS.lbtroi
WHERE LABORTRANS.posted = 1

UNION ALL

SELECT 'Parts' Source
,CHARGEINF.wt_oi
,INVTRANS.tambase_amt TRANS_tambase_amt
FROM mc.CHARGEINF CHARGEINF
INNER JOIN mc.intr_chgs intr_chgs ON CHARGEINF.chgioi = intr_chgs.ref_oi
INNER JOIN mc.INVTRANS INVTRANS ON intr_chgs.owner_oi = INVTRANS.introi
WHERE INVTRANS.posted = 1
) A ON WT.wtskoi = A.wt_oi
WHERE ER.DateTimeStamp BETWEEN @StartDate
AND @EndDate
AND WO.clsdt_date BETWEEN @StartDate
AND @EndDate
AND WT.ci_cnc_date IS NULL
AND WT.tmplt = 0
AND A.Source is not null
GROUP BY ER.EngEquipID
,ER.Location
,ER.Cycles
,ER.DateTimeStamp
,A.Source
,A.TRANS_tambase_amt

) B

GROUP BY B.EngEquipID
,B.Location
,B.Cycles
,B.DateTimeStamp
--,B.Source





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1378813
Posted Tuesday, October 30, 2012 8:53 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:16 PM
Points: 3,373, Visits: 7,290
I believe there's a way to do it much simpler. But you'll need to test.

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME

SET @StartDate = '1/1/2012'
SET @EndDate = '1/31/2012'

SELECT ER.EngEquipID
,ER.Location
,ER.Cycles
,ER.DateTimeStamp
,'Labor' AS Labor
,ISNULL( A.TRANS_tambase_amt, 0) AS LaborAmount
,'Parts' AS Parts
,ISNULL( B.TRANS_tambase_amt, 0) AS PartsAmount
,ISNULL( A.TRANS_tambase_amt, 0) + ISNULL( B.TRANS_tambase_amt, 0) AS TotalAmount
FROM Engine_Prod.mc.WORKORDER WO
LEFT OUTER JOIN MSM.dbo.tblEngRunData ER ON ER.WONumber = ltrim(rtrim(WO.id))
LEFT OUTER JOIN mc.WORKTASK WT ON WO.wordoi = WT.wrkord_oi
LEFT OUTER JOIN mc.WORKORDERTYPE WOT ON WO.wot_oi = WOT.wotyoi
LEFT OUTER JOIN (
SELECT CHARGEINF.wt_oi
,LABORTRANS.tambase_amt TRANS_tambase_amt
FROM mc.CHARGEINF CHARGEINF
INNER JOIN mc.lbtr_chgs lbtr_chgs ON CHARGEINF.chgioi = lbtr_chgs.ref_oi
INNER JOIN mc.LABORTRANS LABORTRANS ON lbtr_chgs.owner_oi = LABORTRANS.lbtroi
WHERE LABORTRANS.posted = 1) A ON WT.wtskoi = A.wt_oi
LEFT OUTER JOIN (
SELECT CHARGEINF.wt_oi
,INVTRANS.tambase_amt TRANS_tambase_amt
FROM mc.CHARGEINF CHARGEINF
INNER JOIN mc.intr_chgs intr_chgs ON CHARGEINF.chgioi = intr_chgs.ref_oi
INNER JOIN mc.INVTRANS INVTRANS ON intr_chgs.owner_oi = INVTRANS.introi
WHERE INVTRANS.posted = 1
) B ON WT.wtskoi = B.wt_oi
WHERE ER.DateTimeStamp BETWEEN @StartDate
AND @EndDate
AND WO.clsdt_date BETWEEN @StartDate
AND @EndDate
AND WT.ci_cnc_date IS NULL
AND WT.tmplt = 0




Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1378822
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse