我有一张表格如下>
Party_Code | Buy_Sell | Trade_Qty | Market_Rate
036L09 1 350 20
036L09 2 300 30
我试图在该数据中显示一个网格,如下所示>
BuyQty | BuyRate | BuyAmt|SellQty | SellRate | SellAmt
350 20 7000 300 30 9000
为此,我提出了两个查询>>
select sum(Trade_qty) as BuyQty, sum(Market_Rate) as BuyAmt from tradeFile where Buy_Sell='1' and Party_Code='036L09'
select sum(Trade_qty) as SellQty, sum(Market_Rate) as SellAmt from tradeFile where Buy_Sell='2' and Party_Code='036L09'
我希望这些查询适用于单个网格。对于我编写的代码为>>
try
{
da = new SqlDataAdapter("select sum(Trade_qty) as BuyQty, sum(Market_Rate) as BuyAmt from tradeFile where Buy_Sell='1' and Party_Code='0L036'", con);
DataSet ds = new DataSet();
da.Fill(ds);
SqlDataAdapter sellDA = new SqlDataAdapter("select sum(Trade_qty) as SellQty, sum(Market_Rate) as SellAmt from tradeFile where Buy_Sell='2' and Party_Code='0L036'", con);
DataSet dsSell = new DataSet();
sellDA.Fill(dsSell);
gv.DataSource = ds.Tables[0];
gv.DataSource = dsSell.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
但它只从最后一个数据源获取数据。
我该怎么做呢?
答案 0 :(得分:1)
此查询返回一个特定Party_Code
select 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,
sum (case when Buy_sell=2 then Trade_Qty*Market_Rate else 0 end) as SellAmount
from tradeFile
where Party_Code='0L036'
,这个返回所有Party_Codes的结果
select Party_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,
sum (case when Buy_sell=2 then Trade_Qty*Market_Rate else 0 end) as SellAmount
from tradeFile
group by Party_Code
答案 1 :(得分:0)
尝试此查询,它将在单个查询中返回您需要的结果
SELECT
a.Trade_Qty AS BuyQuant,
a.Market_Rate AS BuyRate,
a.Trade_Qty * a.Market_Rate AS BuyAmt,
b.Trade_Qty AS SellQuant,
b.Market_Rate AS SellRate,
b.Trade_Qty * b.Market_Rate AS SellAmt
FROM
tradeFile a, tradeFile b
WHERE
a.Party_Code = b.Party_Code AND
a.Party_Code = '036L09' AND
a.Buy_Sell = 1 AND
b.Buy_Sell = 2;
对于通过1 party_code进行多次买卖的情况,请尝试此查询
SELECT
a.Trade_Qty AS BuyQuant,
a.Amt,
b.Trade_Qty AS SellQuant,
b.Amt
FROM
(SELECT
Party_Code
sum(Trade_Qty) As 'Trade_Qty',
sum(Trade_Qty * Market_Rate) As 'Amt'
FROM tradeFile
WHERE Party_Code = '036L09' AND
Buy_Sell = 1
GROUP BY Party_Code
) a,
(SELECT
Party_Code
sum(Trade_Qty) As 'Trade_Qty',
sum(Trade_Qty * Market_Rate) As 'Amt'
FROM tradeFile
WHERE Party_Code = '036L09' AND
Buy_Sell = 2
GROUP BY Party_Code
) b
WHERE
a.Party_Code = b.Party_Code;
答案 2 :(得分:0)
使用Group By查询,如下所示,这将为您提供单个数据集的结果。
select sum(Trade_qty) as Qty, sum(Market_Rate) as Amt, (case when Buy_Sell = 1 then 'Buy' Else 'Sale' end) as TransactionType from tradeFile where Party_Code='036L09'
group by Buy_Sell
答案 3 :(得分:0)
让我们尝试这个查询:)
Select sum(t1.Trade_qty) as BuyQty, sum(t1.Market_Rate) as BuyAmt,
sum(t2.Trade_qty) as SellQty, sum(t2.Market_Rate) as SellAmt FROM tradeFile
INNER join tradeFile t2
ON t1.Party_Code=t2.Party_Code and t1.Buy_Sell='1' and t1.Party_Code='036L09'
and t2.Buy_Sell='2'
答案 4 :(得分:0)
首先,您的查询完全错误,就像您所说的那样。
select sum(Trade_qty) as BuyQty, sum(Market_Rate) as BuyAmt from tradeFile where Buy_Sell='1' and Party_Code='036L09'
select sum(Trade_qty) as SellQty, sum(Market_Rate) as SellAmt from tradeFile where Buy_Sell='2' and Party_Code='036L09'
尝试此查询
SELECT sum(a.Trade_qty) as BuyQty, a.Market_rate as BuyRate, a.Market_rate * sum(a.Trade_qty) as BuyAmt,
sum(b.Trade_qty) as SellQty, b.Market_rate as SellRate, b.Market_rate * sum(b.Trade_qty) as SellAmt
FROM tradeFile a
INNER JOIN tradeFile b on b.id= a.id
GROUP BY a.Market_rate,b.Market_rate
WHERE (a.Buy_Sell ='1' and a.Party_Code='036L09') or (b.Buy_Sell='2'' and.Party_Code='036L09')
但这仍然是通用的,你需要制作两张桌子,1张桌子用于出售,1张桌子用于购买,所以可以加入那两张桌子。
关于你的数据源,这是错的,你覆盖了
gv.DataSource = ds.Tables[0];
进入
gv.DataSource = dsSell.Tables[0];
这就是显示最后查询的原因。