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)"

このクエリでは、sauda_date によるグループを使用しました。

sauda_date でグループ化しますが、最初に今月の値をグループ化し、次に先月の値をグループ化します。

私は正反対にしたい、それはそれを 2 月でグループ化し、次に 3 月にする必要があります。

私のデータビジュアライザーは次のとおりです。

ここに画像の説明を入力

ここでわかるように、最初に 2013 年 2 月 28 日の値が表示され、次に 3 月の値が表示されます。

次のクエリで Order by も試しました。

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.

注:サウダの日付は 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
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 に答える
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

order by sauda_date を使用して、希望どおりにすることができます

于 2013-04-06T08:45:26.473 に答える