0

我的查询如下:

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

在此处输入图像描述 请指导我。

4

3 回答 3

1

您可以使用order by sauda_date 使其成为您想要的方式

于 2013-04-06T08:45:26.473 回答
1
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) asc
于 2013-04-06T08:57:48.103 回答
1
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 sauda_date order by sauda_date asc

这应该工作!

于 2013-04-08T12:22:10.333 回答