0

我有一张如下表>

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);
            }

但它只从最后一个数据源获取数据。

我该怎么做?

4

5 回答 5

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
于 2013-03-15T07:21:48.913 回答
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;
于 2013-03-15T06:05:50.393 回答
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
于 2013-03-15T06:59:17.137 回答
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' 
于 2013-03-15T07:00:10.850 回答
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')

但这仍然是通用的,你需要制作两张桌子,一张卖一张桌子,一张买一张桌子,这样就可以加入那两张桌子。

关于您的数据源,这是错误的,您忽略了

gv.DataSource = ds.Tables[0];

进入

gv.DataSource = dsSell.Tables[0];

这就是显示最后一个查询的原因。

于 2013-03-15T07:37:36.223 回答