我有如下贸易文件表:
ID int
TradeNo nvarchar(50)
Scrip_Code nvarchar(50)
Inst_Type nvarchar(50)
Expirydate datetime
Buy_Sell float
Trade_Qty float
Market_Rate float
Party_Code nvarchar(50)
Sauda_Date nvarchar(50)
FO关闭表为:
Cl_Rate money
Expirydate datetime
Trade_Date datetime
Inst_Type varchar(6)
Symbol varchar(12)
我只是触发以下查询以从 tradefile 表中获取数据:
SELECT 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,'1 Apr 2013') group by Scrip_Code,ExpiryDate,sauda_date
我只是想将 cl_Rate[Closing rate ] 附加到它。
关闭率检查为 tradeFile.ExpiryDate=flclosing.expirydate
andtradefile.saudadate=foclosing.tradedate
和Inst_Type='FUTURISTIK'
我有这些条件,但是当我尝试在上面的查询中附加这些条件时,它不起作用,因为 group by 已被使用。
谁能帮我在上述查询中附加具有上述条件的 cl_rate 列???
编辑:
try
{
con.Open();
float closingRate = 0;
DateTime dtExpDate=DateTime.Now.Date;
da = new SqlDataAdapter("SELECT ExpiryDate,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,'" + dtpForDate.Value.Date.ToShortDateString() + "') and convert(datetime,'" + dtpToDate.Value.Date.ToShortDateString() + "') group by Scrip_Code,ExpiryDate,sauda_date", con);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dtExpDate = DateTime.Parse(ds.Tables[0].Rows[i][0].ToString());
if (ds.Tables[0].Rows[i][7].ToString() == "BF")
{
daClRate = new SqlDataAdapter("select cl_rate,ExpiryDate,symbol from foClosing where Symbol='" + ds.Tables[0].Rows[i][1].ToString() + "' and convert(datetime,convert(varchar(11),ExpiryDate))= '" + dtExpDate.Date.ToString("yyyy-MM-dd") + "' and Inst_Type='FUTSTK' and trade_date between convert(datetime,'" + dtpForDate.Value.Date.ToShortDateString() + "') and convert(datetime,'" + dtpToDate.Value.Date.ToShortDateString() + "') order by trade_date ", con);
dsClRate = new DataSet();
daClRate.Fill(dsClRate);
for (int j = 0; j < dsClRate.Tables[0].Rows.Count; j++)
{
closingRate = float.Parse(dsClRate.Tables[0].Rows[j][0].ToString());
}
}
else
{
}
}
通过这种方式,我通过不同的查询获取收盘价。