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.
| Author |
Topic |
|
teejay8841
Starting Member
2 Posts |
Posted - 2011-12-19 : 11:35:41
|
| Hii have a table which stores transactions as followsCustomer Date DocNumber Debit Credit001 [01-01-2011] 20 100 0001 [02-01-2011] 20 0 90i need to create a simple view which shows in a single row as follows001 [01-01-2011] 20 100 90 10it should pick the of the date of the debit entry always and show credit adjucent while summing up as they will be many entries for that customer. i have done many things but im stuck now. please assist.... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-19 : 11:41:20
|
view code will be likeCREATE VIEW viewnameASSELECT Customer, MIN(CASE WHEN Debit> 0 THEN Date ELSE NULL END) AS Date, DocNumber,SUM(Debit) AS Debit,SUM(Credit) AS CreditFROM TableGROUP BY Customer,DocNumber ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2011-12-19 : 11:42:08
|
| create view MyViewasselect Customer, Min(Date) Date, DocNumber, Sum(Debit) Debits, Sum(Credit) Credits, Sum(Debit) - Sum(Credit) Balancefrom MyTablegroup by Customer, DocNumber=======================================Faced with the choice between changing one's mind and proving that there is no need to do so, almost everyone gets busy on the proof. -John Kenneth Galbraith |
 |
|
|
teejay8841
Starting Member
2 Posts |
Posted - 2011-12-19 : 11:58:00
|
| @visakh16 your script worked thanks!!!!!!! now lets say for example i there is also a field named TrnType and i want to show the TrnType of the debit entry on the single row not of the credit entry. how do i go about it. Or i a column Seqence number and i want to take the value that is on the debit transaction as i might want to link it to another view as welll.....good to go |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-19 : 12:02:51
|
quote: Originally posted by teejay8841 @visakh16 your script worked thanks!!!!!!! now lets say for example i there is also a field named TrnType and i want to show the TrnType of the debit entry on the single row not of the credit entry. how do i go about it. Or i a column Seqence number and i want to take the value that is on the debit transaction as i might want to link it to another view as welll.....good to go
CREATE VIEW viewnameASSELECT Customer, MIN(CASE WHEN Debit> 0 THEN Date ELSE NULL END) AS Date,MIN(CASE WHEN Debit> 0 THEN TranType ELSE NULL END) AS TrnType, DocNumber,SUM(Debit) AS Debit,SUM(Credit) AS CreditFROM TableGROUP BY Customer,DocNumber ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|