我的查询如下:
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)"
在这个查询中,我使用了 group by sauda_date。
它按 sauda_date 对它进行分组,但首先对当前月份的值进行分组,然后对上个月的值进行分组。
我想要完全相反,它应该在二月之前分组,然后三月。
我的数据可视化工具如下:
在这里我们可以看到它应该首先显示 2013 年 2 月 28 日的值,然后显示 3 月的值。
我还尝试了通过以下查询订购:
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%' order by CONVERT(VARCHAR(11),sauda_date) desc
但它给了我以下错误:
Column 'tradefile.Sauda_Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
注意: sauda 日期是 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(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
请指导我。