0

我的查询是:

SELECT   sauda_date,Scrip_Code,
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty ELSE 0 END) AS BuyQty, 
SUM(CASE WHEN Buy_sell = 1 THEN Market_Rate ELSE 0 END) AS BuyRate, 
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty * Market_Rate ELSE 0 END) 
AS BuyAmount,
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty ELSE 0 END) AS SellQty, 
SUM(CASE WHEN Buy_sell = 2 THEN Market_Rate ELSE 0 END) AS SellRate,
(CASE WHEN SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty ELSE 0 END)
>SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty ELSE 0 END) 
THEN 'BF' ELSE 'BT' END ) as TradeType, 
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty * Market_Rate ELSE 0 END) AS SellAmount, 
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty ELSE 0 END)-
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty ELSE 0 END) as NETQTY,
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty * Market_Rate ELSE 0 END)-
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty * Market_Rate ELSE 0 END)as NetAmt,
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty * Market_Rate ELSE 0 END)-
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty * Market_Rate ELSE 0 END) as PNLAmt 
FROM tradeFile where Inst_Type='FUTIDX' OR Inst_Type='FUTSTK' 
  and Sauda_Date between convert(datetime,'1 Mar 2013') 
  and convert(datetime,'4 Mar 2013') group by Scrip_Code,ExpiryDate,sauda_date

通过此查询,我得到的结果为:

在此处输入图像描述

正如我们在 2013-03-11 看到的,有多个记录。我想要他们的 BuyQty、SellQty 等列的总和。即按日期求和记录为

sauda_date  scrip_code  BuyQty
2013-03-11   BANKNIFTY   150 [sum for this date]

我努力了:

SELECT Scrip_Code,sauda_date,
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty ELSE 0 END) AS BuyQty,
SUM(CASE WHEN Buy_sell = 1 THEN Market_Rate ELSE 0 END) AS BuyRate,
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty * Market_Rate ELSE 0 END)
AS BuyAmount,
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty ELSE 0 END) AS SellQty,
SUM(CASE WHEN Buy_sell = 2 THEN Market_Rate ELSE 0 END) AS SellRate,
(CASE WHEN SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty ELSE 0 END)>
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty ELSE 0 END) THEN 'BF' ELSE 'BT' END ) 
as TradeType,
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty * Market_Rate ELSE 0 END) AS SellAmount,
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty ELSE 0 END)-
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty ELSE 0 END) as NETQTY,
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty * Market_Rate ELSE 0 END)-
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty * Market_Rate ELSE 0 END)as NetAmt,
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty * Market_Rate ELSE 0 END)-
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty * Market_Rate ELSE 0 END) as PNLAmt 
FROM tradeFile where Inst_Type='FUTIDX' OR Inst_Type='FUTSTK' 
   and Sauda_Date between convert(datetime,'1 Mar 2013') 
   and convert(datetime,'10 Mar 2013') 
   group by Scrip_Code,convert(datetime,sauda_date) 
   order by convert(varchar(11),sauda_date)

但我得到的结果是:

在此处输入图像描述

请帮我。

4

3 回答 3

1

更改convert(datetime,sauda_date)datediff(d,0,sauda_date)同一列并按同一列分组

IE

SELECT   datediff(d,0,sauda_date) sauda_date,Scrip_Code,...
group by Scrip_Code,ExpiryDate,datediff(d,0,sauda_date);
于 2013-04-23T11:32:32.760 回答
1

请试试:

SELECT DISTINCT
    *, 
    SUM(BuyQty) OVER (PARTITION BY CONVERT(NVARCHAR(20), sauda_date, 106)sauda_date),
    SUM(SellQty) OVER (PARTITION BY CONVERT(NVARCHAR(20), sauda_date, 106)),
    .....
FROM YourTable
于 2013-04-23T11:33:53.810 回答
1

类似于@richardTheKewi,但我使用转换从日期时间字段中释放时间元素

select convert(varchar(10),sauda_date,102) sauda_date,Scrip_Code, .....
group by Scrip_Code,ExpiryDate,convert(varchar(10),sauda_date,102)
于 2013-04-23T11:46:07.893 回答