0

私は次のように1つのテーブルを持っています>

Party_Code | Buy_Sell | Trade_Qty | Market_Rate

 036L09         1         350           20

 036L09         2         300           30

そのデータに次のように1つのグリッドを表示しようとしています>

BuyQty | BuyRate | BuyAmt|SellQty | SellRate | SellAmt  

 350       20       7000    300        30        9000 

このために私は2つのクエリを行いました>>

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

このクエリは、特定の 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

以下のようなGroupByクエリを使用すると、単一のデータセットになります。

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

そもそも、あなたの質問はあなたが言ったように完全に間違っています。

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

ただし、これはまだ一般的なものです。2 つのテーブルを作成する必要があります。1 つのテーブルは売り用で、1 つのテーブルは買い用なので、その 2 つのテーブルを結合できます。

あなたのデータソースについて、それは間違っていました。

gv.DataSource = ds.Tables[0];

の中へ

gv.DataSource = dsSell.Tables[0];

そのため、最後のクエリが表示されます。

于 2013-03-15T07:37:36.223 に答える