我的查询是:
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)
但我的结果为:
请帮帮我。
答案 0 :(得分:1)
将convert(datetime,sauda_date)
更改为datediff(d,0,sauda_date)
并按相同列分组
即
SELECT datediff(d,0,sauda_date) sauda_date,Scrip_Code,...
group by Scrip_Code,ExpiryDate,datediff(d,0,sauda_date);
答案 1 :(得分: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
答案 2 :(得分:1)
与@richardTheKewi类似,但我使用convert从日期时间字段中删除时间元素
select convert(varchar(10),sauda_date,102) sauda_date,Scrip_Code, .....
group by Scrip_Code,ExpiryDate,convert(varchar(10),sauda_date,102)