Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
I want to add a runbalance calculated field in query
Message
 
To
26/11/2006 05:15:49
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Environment versions
SQL Server:
SQL Server 7.0
Miscellaneous
Thread ID:
01172408
Message ID:
01172459
Views:
12
>select outdt as date ,packsetid, shetkariid,
> sum(inqty) as inqty,
> sum(outqty) as outqty ,
> sum(inqty - outqty) as Balance
>
>will this balance be a running balance (i.e. yesterdays closing is todays opening)
>
>rajesh

No, that will be day balance, today input - today output, Try:
DECLARE @Test TABLE (OutDt datetime, packsetid int, shetkariid int, inqty int, outqty int)
INSERT INTO @Test VALUES ('20060101', 1, 1, 10,5)
INSERT INTO @Test VALUES ('20060101', 1, 1, 10,5)
INSERT INTO @Test VALUES ('20060101', 1, 1, 10,5)
INSERT INTO @Test VALUES ('20060102', 1, 1, 10,10)
INSERT INTO @Test VALUES ('20060102', 1, 1, 10,3)
INSERT INTO @Test VALUES ('20060102', 1, 1, 20,10)
INSERT INTO @Test VALUES ('20060103', 1, 1, 20,5)

select Test.date,
      Test.packsetid,
      Test.shetkariid,
      Test.inqty,
      Test.outqty,
      sum(ISNULL(Tbl1.inqty - Tbl1.outqty,0)) as RunningBalance
FROM (SELECT Test.outdt as date ,Test.packsetid, Test.shetkariid,
             SUM(Test.inqty)  as inqty,
             SUM(Test.outqty) as outqty
      FROM @Test Test
      GROUP BY Test.outdt,Test.packsetid, Test.shetkariid) Test
LEFT JOIN @Test Tbl1 ON Test.date       > Tbl1.OutDt       AND 
                        Test.packsetid  = Tbl1.packsetid   AND
                        Test.shetkariid = Tbl1.shetkariid
GROUP BY Test.date,Test.packsetid, Test.shetkariid,Test.inqty, Test.outqty
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform