solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Whitepapers SQL Server Tools SQL Server Webcasts SQL Server Questions and Answers SQL Server Questions and Answers













SQL Statement

Asked 2/20/2012 4:04:21 AM by ZeS

Good day Sir, I have one table <StoTrans>

Table Structure = RepDate (DateTime), PartNo Nvarchar(20), PartDesc Nvarchar(30), StkInHand (Float), NPurQ (Float), IssQty (Float)

I want to... Select Partno, PartDesc, StkInHand from the table that has min(RepDate) group by PartNo to get the Opening Stock, and then

Select Sum(NPurQ), Sum(IssQty), (Sum(StkInHand + Sum(NPurQ) - Sum(IssQty))

I am new in SQL so i dont know whether i am talking logically.   

The idea is to display  PartNo .... PartDesc .... OpStk .... NewPur .... QtyIss .... BalStk 

Can any one assist me Please



Answered 2/23/2012 6:41:00 PM by Scott C (0) vote answer is useful vote answer is NOT useful

I would question the use of an approximate data type (FLOAT) for the transaction quantities instead of an exact type such as NUMERIC(15,8).  If you're not dealing with fractions of items you might even get away with INT.

I would also ask why PartDesc is in this table.  Do the descriptions vary over time?  If you use "GROUP BY PartNo, PartDesc" you'll get multiple rows for the same part number, which I assume is not desirable.  Database normalization rules would suggest a separate table with (PartNo, PartDesc).

But back to your query, I'm assuming the Opening Stock is StkInHand from the earliest transaction for a PartNo, and PartDesc is taken from the most recent.  The first version does it with one subquery.

SELECT  PartNo, 
        PartDesc = MAX(CASE WHEN Latest = 1 THEN PartDesc END),
        OpStk = MAX(CASE WHEN Earliest = 1 THEN StkInHand END),
        NewPur = SUM(NPurQ),
        QtyIss = SUM(IssQty),
        BalStk = MAX(CASE WHEN Earliest = 1 THEN StkInHand END) + SUM(NPurQ) +  SUM(IssQty)
FROM (
    SELECT  PartNo, PartDesc, StkInHand, NPurQ, IssQty,
            Earliest = ROW_NUMBER() OVER (PARTITION BY PartNo ORDER BY RepDate),
            Latest = ROW_NUMBER() OVER (PARTITION BY PartNo ORDER BY RepDate DESC)
    FROM dbo.StoTrans
) st
GROUP BY PartNo

The second version uses separate subqueries and is IMHO more readable and maintainable.  It may perform differently than the first if the table gets very large, but you'd have to have a large table for testing to prove it.

SELECT  trans.PartNo, pt.PartDesc, OpStk = InitialStock, NewPur, QtyIss, BalStk = InitialStock + NewPur + QtyIss
FROM (
    -- Get aggregate values from all transactions
    SELECT  PartNo, NewPur = SUM(NPurQ), QtyIss = SUM(IssQty)
    FROM dbo.StoTrans
    GROUP BY PartNo
) trans
INNER JOIN (
    -- Find earliest StkInHand
    SELECT  PartNo, InitialStock = StkInHand,
            Sequence = ROW_NUMBER() OVER (PARTITION BY PartNo ORDER BY RepDate)
    FROM dbo.StoTrans
) init ON trans.PartNo = init.PartNo AND init.Sequence = 1
INNER JOIN (
    -- Find most recent PartDesc
    SELECT  PartNo, PartDesc,
            Sequence = ROW_NUMBER() OVER (PARTITION BY PartNo ORDER BY RepDate DESC)
    FROM dbo.StoTrans
) pt ON trans.PartNo = pt.PartNo AND pt.Sequence = 1


Post an Answer

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
"SQL doctor is the best SQL product on the market, by far. All of Idera's tools are great, but this is the icing on the cake!"

SQL Monitor – For database professionals who need results on Day One. Try it online.

What grade do you think your SQL Servers get? Find out with Edgewood's SQL Server Health Check consulting services.

Get SQL Server Tips Straight from Kevin Kline.

Join the over million SQL Server Professionals who get their issues resolved daily.

The SQL Server Security THREAT - It’s Closer Than You Think


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com