我的桌子就像这样
SlNo Date Session MemberCode Litres Fat Price Amount GS Col1 Col
16 2013-07-02 16:27:17.000 E 0 1.00 0.00 28.00 28.00 NULL S NULL
17 2013-07-08 16:55:26.000 E 1 38.00 3.80 17.56 667.28 4.00 P NULL
18 2013-07-08 22:10:03.000 E 0 0.00 3.90 17.63 0.00 0.00 GT -
19 2013-07-08 23:03:08.000 E 2 2.00 3.50 17.50 35.00 4.00 P -
20 2013-07-08 23:03:22.000 E 13 3.90 3.80 17.56 68.48 4.00 P -
我想要一个查询来获得结果
Date,
Session,
Total Members for session,
Total Litres for Session,
Total Amount For the Session,
Fat(Where Col1='GT') as GTFat,
Price(Where Col1='GT') as GTPrice,
NetAmount as GTPrice * (TotalLitres for Session),
Profit or Loss(NetAmount-Session Amount)
请帮助我如何获得重拍。
先谢谢
我尝试使用此代码但无法获得所需的结果,因为日期比较存在问题
DECLARE @t TABLE (SlNo INT IDENTITY (1, 1) NOT NULL, Date DATETIME, Session VARCHAR(30), TotalMembers INT, TotalLitres DECIMAL(8, 2), Amount DECIMAL(8, 2),
GTFat DECIMAL(8, 2), GTPrice DECIMAL(8, 2), GTAmount DECIMAL(8, 2), PnL DECIMAL(8, 2));
INSERT INTO @t (Date, Session, TotalMembers, TotalLitres, Amount)
SELECT Cast(Date as date), Session, Count(MemberCode), Sum(Litres), Sum(Amount) FROM myTable Where Col1 = 'P' and Date between '2013-07-01' and '2013-07-09' GROUP BY Cast(Date as Date), Session Order By Cast(Date as date)
declare @maxcount int,@loop int
declare @GTAmt money,@PL money
set @loop =1
select @maxcount= MAX(SlNo ) from @t
while(@loop <= @maxcount)
begin
Update @t Set GTFat = (Select Fat From myTable Where Date=Date and Session=Session and Col1='GT'),GTPrice=(Select Price From myTable Where Date=Date and Session=Session and Col1='GT')
set @loop = @loop + 1
end
SELECT * FROM @t order by Cast(Date as date)
答案 0 :(得分:0)
我相信你需要这样的东西,但你的净金额是什么意思?你想计算吗?
日期,
会议上,
会员总数,
会话总数,
会议总金额,
脂肪(其中Col1 ='GT')为GTFat,
价格(Col1 ='GT')为GTPrice,
NetAmount作为GTPrice *(会话的TotalLitres),
损益(NetAmount-Session Amount)
选择
Date,
Session,
COUNT(Session) AS TotalnumberForSession,
SUM(amount) AS TotalSumAmount,
CASE
WHEN Col1 = 'GT' THEN Fat
ELSE NULL
END AS GTPrice,
CASE
WHEN Col1 = 'GT' THEN Price
ELSE NULL
END AS GTFAT,
amount*SUM(litres) OVER (PARTITION BY session)
答案 1 :(得分:0)
感谢您的回答。
我通过编写存储过程
获得了结果创建一个临时表并收集所有必需的数据并获得结果。
而不是存储过程我可以直接使用临时表...就我而言,这是第二个选项。
再次感谢