我的 tradefile 表格如下:
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)
FOClosing 表格为:
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
和tradefile.saudadate=foclosing.tradedate
以及Inst_Type='FUTURISTIK'
我有这些条件但是当我尝试在上面的查询中附加它们时它不起作用,因为已经使用了分组。
任何人都可以帮我在上面的查询???中添加上述条件的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
{
}
}
通过这种方式,我通过不同的查询获取收盘价。
答案 0 :(得分:1)
将您的主要查询更改为
SELECT tf.ExpiryDate,tf.Scrip_Code,fc.cl_rate,SUM(CASE WHEN tf.Buy_sell = 1 THEN tf.Trade_Qty ELSE 0 END)AS BuyQty,SUM(CASE WHFf.Buy_sell = 1那么tf.Market_Rate ELSE 0 END) AS BuyRate,SUM(例如tf.Buy_sell = 1那么tf.Trade_Qty * tf.Market_Rate ELSE 0 END)AS BuyAmount,SUM(如果tf.Buy_sell = 2那么tf.Trade_Qty ELSE 0 END)AS SellQty,SUM(CASE当tf.Buy_sell = 2那么tf.Market_Rate ELSE 0 END)AS SellRate,(情况是SUM(例如tf.Buy_sell = 1那么tf.Trade_Qty ELSE 0 END)&gt; SUM(CASE WHFf.Buy_sell = 2那么tf .Trade_Qty ELSE 0 END)然后'BF'ELSE'BT'END)作为TradeType,SUM(如果tf.Buy_sell = 2那么tf.Trade_Qty * tf.Market_Rate ELSE 0 END)作为SellAmount,SUM(例如tf.Buy_sell = 1那么tf.Trade_Qty ELSE 0 END)-SUM(当tf.Buy_sell = 2那么tf.Trade_Qty ELSE 0 END)作为NETQTY,SUM(例如tf.Buy_sell = 1那么tf.Trade_Qty * tf.Market_Rate ELSE 0 END)-SUM(如果tf.Buy_sell = 2那么tf.Trade_Qty * tf.Market_Rate ELSE 0 END)作为NetAmt,SUM(CASE WH) EN tf.Buy_sell = 2 THEN tf.Trade_Qty * tf.Market_Rate ELSE 0 END)-SUM(tf.Buy_sell = 1 THEN tf.Trade_Qty * tf.Market_Rate ELSE 0 END)作为PNLAmt FROM tradeFile tf,foClosing fc where tf .Inst_Type ='FUTIDX'或tf.Inst_Type ='FUTSTK'和tf.Sauda_Date之间的转换(datetime,'“+ dtpForDate.Value.Date.ToShortDateString()+”')和转换(datetime,convert(varchar(11) ,fc.ExpiryDate))='“+ dtExpDate.Date.ToString(”yyyy-MM-dd“)+”'和convert(datetime,'“+ dtpToDate.Value.Date.ToShortDateString()+”')和fc .symbol = tf.Scrip_Code和fc.Inst_Type ='FUTSTK'和fc.trade_date = tf.sauda_date和fc.ExpiryDate = tf.ExpiryDate group by Scrip_Code,ExpiryDate,sauda_date
您之前的问题没有说明的是外键..您将forClosing.Symbol
与tradeFile.Scrip_Code
相匹配。这是缺失的部分。