not able to get individual date in query result

2019-08-28 02:49发布

My Query is :

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

With this query i am geting result as:

enter image description here

As we can see at 2013-03-11 there are multple records. I wanted to have their sum for BuyQty,SellQty,etc columns. i.e. Datewise sum record as

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

I have tried:

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)

But i got result as:

enter image description here

Please help me.

3条回答
三岁会撩人
2楼-- · 2019-08-28 03:12

Change convert(datetime,sauda_date) to datediff(d,0,sauda_date) and group by the same column

i.e.

SELECT   datediff(d,0,sauda_date) sauda_date,Scrip_Code,...
group by Scrip_Code,ExpiryDate,datediff(d,0,sauda_date);
查看更多
家丑人穷心不美
3楼-- · 2019-08-28 03:22

Similar to @richardTheKewi but I use convert to loose the time element from the date time field

select convert(varchar(10),sauda_date,102) sauda_date,Scrip_Code, .....
group by Scrip_Code,ExpiryDate,convert(varchar(10),sauda_date,102)
查看更多
Fickle 薄情
4楼-- · 2019-08-28 03:26

Please try:

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
查看更多
登录 后发表回答