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
 General SQL Server Forums
 New to SQL Server Programming
 Hi experts

Author  Topic 

poongundran
Starting Member

1 Post

Posted - 2009-04-07 : 11:53:10
Hi all

The following is the structure of my table and its content

Sno ItemName Quantity Date
1 A 13 01/04/09
2 B 5 01/04/09
3 C 8 01/04/09
4 B 7 02/04/09
5 C 10 02/04/09
6 A 50 03/04/09
7 B 20 03/04/09
8 C 5 03/04/09


Im my scenario, i need to use the date in where condition for instance when i m selecting the date 03/04/09 (in my where condition)
my output should be as like below

ItemName Date OpeningQty Quantity
A 03/04/09 13 50
B 03/04/09 12 20
C 03/04/09 18 5

here under the column " OpeningQty" all the Quantity should be summarized till , the date that was given in where condition . Please suggest me query or stored procedure in getting this output


DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-04-07 : 12:22:51
1) Why MUST you use the date in the WHERE clause?
2) What have you tried so far?

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL or How to sell Used Cars
For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-07 : 12:59:00
[code]
SELECT t.ItemName, t.Date,
(select sum(Quantity) from Table WHERE ItemName=t.ItemName AND Date < t.Date) AS OpeningQty
,t.Quantity
FROM Table t
WHERE Date=@Date
[/code]
@Date is date value you pass
Go to Top of Page
   

- Advertisement -