Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Help avoid using a curser

Author  Topic 

Togaspoon
Starting Member

42 Posts

Posted - 2004-03-22 : 11:11:36
I have the following recordset

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 * FROM InvoiceDetail

DROP TABLE InvoiceDetail


What 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 Y
4BRZZZ 1 65.0000 65.0000 Y
4BLKCP 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





Brett

8-)
Go to Top of Page

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.
Go to Top of Page

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 #Temp
FROM InvoiceDetail A

SELECT
A.ProductNbr,
B.Qty,
B.Price,
B.ExtPrice
FROM 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.SortOrder
ORDER BY A.SortOrder

DROP TABLE InvoiceDetail
DROP TABLE #TEMP
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-03-22 : 13:23:42
Running totals in the back end...

hmmmmmmm

What meaning does this have?



Brett

8-)
Go to Top of Page

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.

Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-03-22 : 19:41:23
Nice work Brett.
Go to Top of Page

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2004-03-22 : 20:16:44
This should work...



create view DerivedGroup
AS
SELECT I.*, (SELECT COUNT(*) from InvoiceDetail D WHERE I.SortOrder > D.SortOrder AND D.PrintYn = 'n')
as Derived
FROM InvoiceDetail I
WHERE I.PrintYN = 'y'
GO
create view FinalDervied
AS
select ProductNbr, Qty, Price, ExtPrice, SortOrder, Derived + Derived - (SELECT SUM(Derived) From derivedgroup D where D.SortOrder <= G.SortOrder) as Derived
from derivedgroup G

Select 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 ExtPrice
from FinalDervied G
left join InvoiceDetail I ON i.SortOrder between G.SortOrder - G.Derived and I.SortOrder AND G.Derived > 0
group 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.. "
Go to Top of Page

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...



Brett

8-)
Go to Top of Page
   

- Advertisement -