使用堆栈上的答案之一,我使用了以下查询:
select
AVG(CASE WHEN Buy_sell = 1 and scrip_code='" + ds.Tables[0].Rows[i][2].ToString() + "' and Sauda_Date between convert(datetime,'" + dtpForDate.Value.Date.ToString("dd/MM/yyyy") + "') and convert(datetime,'" + dtpToDate.Value.Date.ToString("dd/MM/yyyy") + "') THEN Market_Rate ELSE 0 END) AS AVGBuyRate ,
AVG(CASE WHEN Buy_sell = 2 THEN Market_Rate ELSE 0 END) AS AVGSellRate,
CONVERT(VARCHAR(11),sauda_date) AS sauda_date,
SUM(CASE WHEN Buy_sell = 1 and scrip_code='" + ds.Tables[0].Rows[i][2].ToString() + "' and Sauda_Date between convert(datetime,'" + dtpForDate.Value.Date.ToString("dd/MM/yyyy") + "') and convert(datetime,'" + dtpToDate.Value.Date.ToString("dd/MM/yyyy") + "') THEN Trade_Qty ELSE 0 END) AS BuyQty,
SUM(CASE WHEN Buy_sell = 2 and scrip_code='" + ds.Tables[0].Rows[i][2].ToString() + "' and Sauda_Date between convert(datetime,'" + dtpForDate.Value.Date.ToString("dd/MM/yyyy") + "') and convert(datetime,'" + dtpToDate.Value.Date.ToString("dd/MM/yyyy") + "') THEN Trade_Qty ELSE 0 END) AS SellQty ,
SUM(CASE WHEN Buy_sell = 1 and scrip_code='" + ds.Tables[0].Rows[i][2].ToString() + "' THEN Trade_Qty ELSE 0 END) -SUM(CASE WHEN Buy_sell = 2 and scrip_code='" + ds.Tables[0].Rows[i][2].ToString() + "' THEN Trade_Qty ELSE 0 END) AS CarryForword
from tradefile
where scrip_code='" + ds.Tables[0].Rows[i][2].ToString() + "'
and INST_TYPE LIKE 'FUT%'
group by CONVERT(VARCHAR(11),sauda_date) order by CONVERT(VARCHAR(11),sauda_date) desc
这个查询给了我下面的输出:
在此,如果我们查看 sauda_date 列,在 2013 年 2 月 28 日之后,它会在 2013 年 3 月 22 日之前订购,然后是 2013 年 3 月 21 日,然后是 2013 年 3 月 20 日,等等。
但它应该是 2013 年 2 月 28 日、2013 年 3 月 1 日、2013 年 3 月 2 日 ....、2013 年 3 月 20 日、2013 年 3 月 21 日、2013 年 3 月 22 日。
我应该在查询中进行哪些更改?
注意: sauda_date 是 nvarchar 类型
新查询:
select AVG(CASE WHEN Buy_sell = 1 and scrip_code='" + ds.Tables[0].Rows[i][2].ToString() + "' and Sauda_Date between convert(datetime,'" + dtpForDate.Value.Date.ToString("dd/MM/yyyy") + "') and convert(datetime,'" + dtpToDate.Value.Date.ToString("dd/MM/yyyy") + "') THEN Market_Rate ELSE 0 END) AS AVGBuyRate , AVG(CASE WHEN Buy_sell = 2 THEN Market_Rate ELSE 0 END) AS AVGSellRate, CONVERT(VARCHAR, CONVERT(DATETIME, sauda_date, 106), 106) as Sauda_Date,SUM(CASE WHEN Buy_sell = 1 and scrip_code='" + ds.Tables[0].Rows[i][2].ToString() + "' and Sauda_Date between convert(datetime,'" + dtpForDate.Value.Date.ToString("dd/MM/yyyy") + "') and convert(datetime,'" + dtpToDate.Value.Date.ToString("dd/MM/yyyy") + "') THEN Trade_Qty ELSE 0 END) AS BuyQty,SUM(CASE WHEN Buy_sell = 2 and scrip_code='" + ds.Tables[0].Rows[i][2].ToString() + "' and Sauda_Date between convert(datetime,'" + dtpForDate.Value.Date.ToString("dd/MM/yyyy") + "',106) and convert(datetime,'" + dtpToDate.Value.Date.ToString("dd/MM/yyyy") + "',106) THEN Trade_Qty ELSE 0 END) AS SellQty , SUM(CASE WHEN Buy_sell = 1 and scrip_code='" + ds.Tables[0].Rows[i][2].ToString() + "' THEN Trade_Qty ELSE 0 END) -SUM(CASE WHEN Buy_sell = 2 and scrip_code='" + ds.Tables[0].Rows[i][2].ToString() + "' THEN Trade_Qty ELSE 0 END) AS CarryForword from tradefile where scrip_code='" + ds.Tables[0].Rows[i][2].ToString() + "' and INST_TYPE LIKE 'FUT%' group by convert(datetime,sauda_date,106) order by convert(datetime,sauda_date,106) desc