| Author |
Topic |
|
Togaspoon
Starting Member
42 Posts |
Posted - 2004-03-22 : 11:11:36
|
I have the following recordsetCREATE TABLE InvoiceDetail( ProductNbr varchar(6), Qty int, Price money, ExtPrice money, PrintYN varchar(1), SortOrder int )INSERT INTO InvoiceDetail VALUES('4COLCP',1,0,0,'Y',1)INSERT INTO InvoiceDetail VALUES('4BRZZZ',1,65,65,'Y',2)INSERT INTO InvoiceDetail VALUES('4BZFZZ',7752,.0051,39.54,'N',3)INSERT INTO InvoiceDetail VALUES('4BZEZZ',7752,.0119,92.25,'N',4)INSERT INTO InvoiceDetail VALUES('4BLKCP',1,0,0,'Y',5)SELECT * FROM InvoiceDetailDROP TABLE InvoiceDetailWhat I need is to total each record that has a 'N' in the PrintYN field to the next record with a 'Y'.What my end recordset should look like is:4COLCP 1 .0000 .0000 Y4BRZZZ 1 65.0000 65.0000 Y4BLKCP 7752 .0170 131.7900 Y |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-03-22 : 12:29:22
|
You sure about the last row? SELECT ProductNbr, SUM(Qty), SUM(Price, SUM(ExtPrice) FROM InvoiceDetails WHERE PrintYN = 'Y'GROUP BY ProductNbr Brett8-) |
 |
|
|
Togaspoon
Starting Member
42 Posts |
Posted - 2004-03-22 : 12:39:31
|
| Yeah, that last row should be the sum of the last 3 rows (the two with the 'N' and the first row in order that has a 'Y').Also the Qty isn't a sum but using MAX works. |
 |
|
|
Togaspoon
Starting Member
42 Posts |
Posted - 2004-03-22 : 12:43:30
|
This is working but seems kinda clunky.CREATE TABLE InvoiceDetail( ProductNbr varchar(6), Qty int, Price money, ExtPrice money, PrintYN varchar(1), SortOrder int )INSERT INTO InvoiceDetail VALUES('4COLCP',1,0,0,'Y',1)INSERT INTO InvoiceDetail VALUES('4BRZZZ',1,65,65,'Y',2)INSERT INTO InvoiceDetail VALUES('4BZFZZ',7752,.0051,39.54,'N',3)INSERT INTO InvoiceDetail VALUES('4BZEZZ',7752,.0119,92.25,'N',4)INSERT INTO InvoiceDetail VALUES('4BLKCP',1,0,0,'Y',5)SELECT *, COALESCE((SELECT SUM(1) FROM InvoiceDetail B WHERE b.PrintYN = 'Y' AND B.SortOrder < A.SortOrder),0) AS 'myGroup'INTO #TempFROM InvoiceDetail ASELECT A.ProductNbr, B.Qty, B.Price, B.ExtPriceFROM InvoiceDetail A INNER JOIN (SELECT MAX(Qty) AS 'Qty', SUM(Price) AS 'Price', SUM(ExtPrice) AS 'ExtPrice', MAX(SortOrder) AS 'SortOrder' FROM #Temp GROUP BY myGroup) B ON A.SortOrder = B.SortOrderORDER BY A.SortOrderDROP TABLE InvoiceDetailDROP TABLE #TEMP |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-03-22 : 13:23:42
|
| Running totals in the back end...hmmmmmmmWhat meaning does this have?Brett8-) |
 |
|
|
Togaspoon
Starting Member
42 Posts |
Posted - 2004-03-22 : 14:56:04
|
| Basically I need to recreate an invoice and the data is stored like my original table in a flat file. |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-03-22 : 19:41:23
|
| Nice work Brett. |
 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2004-03-22 : 20:16:44
|
This should work...create view DerivedGroupASSELECT I.*, (SELECT COUNT(*) from InvoiceDetail D WHERE I.SortOrder > D.SortOrder AND D.PrintYn = 'n') as DerivedFROM InvoiceDetail IWHERE I.PrintYN = 'y'GOcreate view FinalDerviedASselect ProductNbr, Qty, Price, ExtPrice, SortOrder, Derived + Derived - (SELECT SUM(Derived) From derivedgroup D where D.SortOrder <= G.SortOrder) as Derivedfrom derivedgroup GSelect G.ProductNbr, MAX(CASE WHEN I.Qty IS NULL THEN G.Qty ELSE I.Qty END) AS Qty, SUM(CASE WHEN I.Price IS NULL THEN G.Price ELSE I.Price END) AS Price, SUM(CASE WHEN I.ExtPrice IS NULL THEN G.ExtPrice ELSE I.ExtPrice END) AS ExtPricefrom FinalDervied Gleft join InvoiceDetail I ON i.SortOrder between G.SortOrder - G.Derived and I.SortOrder AND G.Derived > 0group by G.ProductNbr EDIT: Added second view (FinalDervied)DavidM"An Ugg Boot is a generic Australian term that has been in use for nearly 100 hundred years. Now some coporate wanker has trademarked it.. " |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-03-22 : 21:27:01
|
quote: Originally posted by ehorn Nice work Brett.
Thanks...looks like David was jammin...gotta check it out when I get to work...But ya know...What do you plan to do with that data...It's an Enigma (ok, there you go, an opening) wrapped up in a mystery...Brett8-) |
 |
|
|
|
|
|