5
select content_type_code_id 
    , ABS(price) AS price
    , SUM(case when price >= 0 THEN 1 ELSE 0 END) AS debits
    , SUM(case when price < 0 THEN 1 ELSE 0 END) AS credits
from dbo.transaction_unrated  
where transaction_date >= '2012/05/01'
      and transaction_date < '2012/06/01'
      and content_provider_code_id in (1)
group by content_type_code_id, ABS(price) 
ORDER BY ABS(price) ASC  

上記のクエリは、次の出力を生成します。

content_type_code_id    price   debits  credits
1                      0.00      317    0
1                      0.99      178    1
1                      1.99      786    1

しかし、私はこのようなものが欲しいです:

content_type_code_id    price   debits  credits NetCount
    1                      0.00      317    0       317 
    1                      0.99      178    1       177 
    1                      1.99      786    1       785

ここで、NetCount =(借方-貸方)

そのために別の列を作成しようとすると、エラーが発生します。

4

3 に答える 3

8

追加するだけです:

SUM(case when price >= 0 THEN 1 ELSE 0 END) - 
    SUM(case when price < 0 THEN 1 ELSE 0 END) AS NetCount

あなたの最後のステートメントとして、あなたはこれで終わるでしょう:

select content_type_code_id 
    , ABS(price) AS price
    , SUM(case when price >= 0 THEN 1 ELSE 0 END) AS debits
    , SUM(case when price < 0 THEN 1 ELSE 0 END) AS credits
    , SUM(case when price >= 0 THEN 1 ELSE 0 END) - 
        SUM(case when price < 0 THEN 1 ELSE 0 END) AS NetCount
from dbo.transaction_unrated  
where transaction_date >= '2012/05/01'
      and transaction_date < '2012/06/01'
      and content_provider_code_id in (1)
group by content_type_code_id, ABS(price) 
ORDER BY ABS(price) ASC  

Lamakの派生テーブルバージョン:

派生テーブルを使用して、コードを少しすっきりさせることもできます。

select content_type_code_id,
    price, debits, credits, (debits - credits) as NetCount
from (
    select content_type_code_id 
        , ABS(price) AS price
        , SUM(case when price >= 0 THEN 1 ELSE 0 END) AS debits
        , SUM(case when price < 0 THEN 1 ELSE 0 END) AS credits
    from dbo.transaction_unrated  
    where transaction_date >= '2012/05/01'
          and transaction_date < '2012/06/01'
          and content_provider_code_id in (1)
    group by content_type_code_id, ABS(price) 
) YourDerivedTable
ORDER BY price ASC  
于 2012-07-23T19:53:49.043 に答える
1
WITH tbl AS 
(
select content_type_code_id 
    , ABS(price) AS price
    , SUM(case when price >= 0 THEN 1 ELSE 0 END) AS debits
    , SUM(case when price < 0 THEN 1 ELSE 0 END) AS credits
from dbo.transaction_unrated  
where transaction_date >= '2012/05/01'
      and transaction_date < '2012/06/01'
      and content_provider_code_id in (1)
group by content_type_code_id, ABS(price) 
ORDER BY ABS(price) ASC  
)

SELECT content_type_code_id, proce, debits, credits, (debits - credits) netcount from tbl
于 2012-07-23T19:55:48.527 に答える
1

こんばんは。同様のタスクがあり、列を追加して更新するだけで非常に短いことがわかりました。

これは、コードがデータベースを生成した後に実行できると思います。それがあなたの状況でうまくいかない場合は、それについてのフィードバックをいただければ幸いです。

ここでのテーブルのALTERTABLE ADDNetCountinteger ;

ここでテーブルの名前を更新しますSETNetCount = Debits-Credits ;

ノート:

  • 最初の行は、整数型のNetCountという列を追加します
  • 2行目は、借方と貸方の差になるように更新します
  • SQLコマンドは大文字で、特定の情報はイタリック体で示しています

幸運を!

于 2014-07-09T15:29:04.830 に答える