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 |
sqldbaa
Starting Member
32 Posts |
Posted - 2007-12-18 : 05:32:51
|
Hi All,I am new to SQL programming, i have only a fair knowledge on sql programmin.So, I apologies for any silly questions-I have a Table1 which containsC1-acountidC2-dateC3-grossamount(postivie and negative decimal values)C4-netamountTable2C1-groupidC2-accountidTable 3 C1-groupidC2-groupnameI need to create a store procedure to retrieve the following on a single table1. top 10 losers of the day i.e. 10 AccountIDs with the greatest negative Grossamount for the dayNOTE:These 10 AccountIDs may be sam or differing each day2.sum of Netamount for each AccountIDs listed in STEP 1 since the beginning of the month.NOTE:These 10 AccountIDs may be same or differing each day and each day sum of netamount should be from beginning of the month till current date.3.Sum of Netamount for the last 5 days for each accountids in STEP1The result set must contain the columns as belowC1-accountidC2-dateC3-net loss for 10 losers on the current date since the beginning of the monthC4-Sum of Net for last 5 days C5-groupnamePlease help me.Below is the script that i have written, without calculating the sum (select top 10 a.date, a.accountid, a.gross, a.net, c.groupname from GBSys_Sum_EOD ajoin server2.dbname.dbo.table2 b on a.accontid=b.accounit=idjoin server2.dbname.dbo.Table3 c on b.groupid=c.groupidwhere date> getdate()-1and gross< (floor(-00.00)) order by gross)Thanks in advance. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-18 : 06:37:13
|
try this:-CREATE PROC AccntDet@Date datetimeASSELECT t.accountid,t.date,net.netamount,lastfive.netamount,t3.groupnameFROM (SELECT Top 10 accountid,dateFROM Table1WHERE date=@DateAND SUM(grossamount)< 0.00GROUP BY date,accountidORDER BY SUM(grossamount))tINNER JOIN (SELECT accountid,SUM(netamount) AS 'netamount' FROM Table1 WHERE date > '01/' + CASE WHEN MONTH(@Date)<10 THEN '0' +MONTH(@Date) ELSE MONTH(@Date) END +'/'+YEAR(@Date) AND date <= DATEADD(d,DATEDIFF(d,0,GETDATE()),0)GROUP BY accountid)netON net.accountid=t.accountidINNER JOIN (SELECT accountid,SUM(netamount) AS 'netamount' FROM Table1 WHERE DATEDIFF(d,date,DATEADD(d,DATEDIFF(d,0,GETDATE()),0))<=5GROUP BY accountid)lastfiveON lastfive.accountid=t.accountid INNER JOIN Table2 t2ON t2.accountid=t.accountid INNER JOIN Table3 t3ON t3.groupid = t2.groupidGO |
 |
|
sqldbaa
Starting Member
32 Posts |
Posted - 2007-12-20 : 09:15:54
|
Thanks a lot.But another help.If in my result set C3=net loss for each of the 10 losers on the current date since the beginning of the previous month till , then how to modify the script .C1-accountidC2-dateC3-net loss for 10 losers on the current date since the beginning of the previous month till dateC4-Sum of Net for last 5 days C5-groupnamePlease help me |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-20 : 09:24:42
|
[code]CREATE PROC AccntDet@Date datetimeASSELECT t.accountid,t.date,net.netamount,lastfive.netamount,t3.groupnameFROM (SELECT Top 10 accountid,dateFROM Table1WHERE date=@DateAND SUM(grossamount)< 0.00GROUP BY date,accountidORDER BY SUM(grossamount))tINNER JOIN (SELECT accountid,SUM(netamount) AS 'netamount' FROM Table1 WHERE date > '01/' + CASE WHEN MONTH(@Date)=1 THEN 12 WHEN MONTH(@Date)>1 AND MONTH(@Date)-1<=10 THEN '0' + MONTH(@Date)-1 ELSE MONTH(@Date)-1 END + '/' +CASE WHEN MONTH(@Date)=1 THEN YEAR(@Date) -1 ELSE YEAR(@Date) ENDAND date <= DATEADD(d,DATEDIFF(d,0,GETDATE()),0)GROUP BY accountid)netON net.accountid=t.accountidINNER JOIN (SELECT accountid,SUM(netamount) AS 'netamount' FROM Table1 WHERE DATEDIFF(d,date,DATEADD(d,DATEDIFF(d,0,GETDATE()),0))<=5GROUP BY accountid)lastfiveON lastfive.accountid=t.accountid INNER JOIN Table2 t2ON t2.accountid=t.accountid INNER JOIN Table3 t3ON t3.groupid = t2.groupidGO[/code] |
 |
|
|
|
|
|
|