我正在点击查询>>
SELECT sauda_date,
Sum(CASE
WHEN buy_sell = 1
AND scrip_code = 'ADANIENT'
AND sauda_date BETWEEN CONVERT(DATETIME, '1 Mar 2013') AND
CONVERT(DATETIME, '1 Apr 2013') THEN
trade_qty
ELSE 0
END) AS BuyQty,
Sum(CASE
WHEN buy_sell = 2
AND scrip_code = 'ADANIENT'
AND sauda_date BETWEEN CONVERT(DATETIME, '1 Mar 2013') AND
CONVERT(DATETIME, '1 Apr 2013') THEN
trade_qty
ELSE 0
END) AS SellQty,
Sum(CASE
WHEN buy_sell = 1
AND scrip_code = 'ADANIENT' THEN trade_qty
ELSE 0
END) - Sum(CASE
WHEN buy_sell = 2
AND scrip_code = 'ADANIENT' THEN trade_qty
ELSE 0
END) AS CarryForword
FROM tradefile
WHERE scrip_code = 'ADANIENT'
GROUP BY sauda_date
我得到了以下结果:
sauda_date | Buy_Qty | SellQty | CarryForward
01 Mar 2013 2000 0 2000
01 Mar 2013 0 2000 0
01 Mar 2013 4000 0 4000
02 Mar 2013 0 1000 3000
02 Mar 2013 2000 0 5000
03 Mar 2013 0 4000 1000
但我希望它为:
sauda_date | Buy_Qty | SellQty | CarryForward
01 Mar 2013 6000 2000 4000
02 Mar 2013 2000 1000 5000
03 Mar 2013 0 4000 1000
我们可以从表格中看到:
CaryyForward=Buy_Qty-Sell_Qty;
01 Mar 2013 is date and Buy_Qty=sum(buy_Qty) for 01 Mar 2013.
02 Mar 2013 is date and Buy_Qty=sum(buy_Qty) for 02 Mar 2013.
[与Sell_Qty和CarryForward相同]。
我尝试过不同的小组以防万一,但后来知道它不允许以防万一。
我也尝试过使用不同的条件但没有工作。
请帮帮我。
答案 0 :(得分:1)
将查询放在子查询中,例如
SELECT sauda_date ,
MAX(Buy_Qty) Buy_Qty,
MAX(SellQty) SellQty,
MAX(CarryForward) CarryForward
FROM
(
-- ...put your current query here ...
) x
GROUP BY sauda_date
答案 1 :(得分:0)
您可以将group by子句与多个参数一起使用。请注意我添加到代码末尾的部分:
select sauda_date,
SUM(CASE WHEN Buy_sell = 1
and scrip_code='ADANIENT'
and Sauda_Date between convert(datetime,'1 Mar 2013')
and convert(datetime,'1 Apr 2013')
THEN Trade_Qty ELSE 0 END) AS BuyQty,
SUM(CASE WHEN Buy_sell = 2
and scrip_code='ADANIENT'
and Sauda_Date between convert(datetime,'1 Mar 2013')
and convert(datetime,'1 Apr 2013')
THEN Trade_Qty ELSE 0 END) AS SellQty ,
SUM(CASE WHEN Buy_sell = 1
and scrip_code='ADANIENT'
THEN Trade_Qty ELSE 0 END)
- SUM(CASE WHEN Buy_sell = 2
and scrip_code='ADANIENT'
THEN Trade_Qty ELSE 0 END) AS CarryForword
from tradefile
where scrip_code='ADANIENT'
group by sauda_date, Buy_Qty, SellQty, CarryForward
这不会让你失去总数吗?
答案 2 :(得分:0)
尝试:
GROUP BY DATEADD(dd, 0, DATEDIFF(dd, 0, sauda_date))
如果没有您的样本数据,我无法确定,但看起来您在表格中的每一行都有不同的TIME部分。
按日期时间分组会使2013-04-04 08:24:13.790
和2013-04-04 17:24:13.790
之间产生差异。以上查询将首先将两者都转换为2013-04-04 00:00:00.000
。
编辑:您还必须使用相同的构造替换SELECT列表中的sauda_date。
答案 3 :(得分:0)
我的数据库也有时间附加到它。 因此,由于不同的时间,它采取不同的日期。
所以我把查询写成:
select CONVERT(VARCHAR(11),sauda_date) AS sauda_date,SUM(CASE WHEN Buy_sell = 1 and scrip_code='ADANIENT' and Sauda_Date between convert(datetime,'1 Mar 2013') and convert(datetime,'1 Apr 2013') THEN Trade_Qty ELSE 0 END) AS BuyQty,SUM(CASE WHEN Buy_sell = 2 and scrip_code='ADANIENT' and Sauda_Date between convert(datetime,'1 Mar 2013') and convert(datetime,'1 Apr 2013') THEN Trade_Qty ELSE 0 END) AS SellQty , SUM(CASE WHEN Buy_sell = 1 and scrip_code='ADANIENT' THEN Trade_Qty ELSE 0 END) -SUM(CASE WHEN Buy_sell = 2 and scrip_code='ADANIENT' THEN Trade_Qty ELSE 0 END) AS CarryForword from tradefile where scrip_code='ADANIENT' group by CONVERT(VARCHAR(11),sauda_date)
这很有用。