0

次のクエリでテーブルをクエリしました

select content_type_code_id
    , price
    , count(price) AS PRICECOUNT 
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, price 
ORDER BY price ASC

次の結果セットが生成されます

content_type_code_id   price    PRICECOUNT
1                     -1.99     1
1                     -0.99     1
1                      0.99     178
1                      1.99     786

しかし、私は次のような結果セットが必要です。

content_type_code_id    price   Debits Credits
1                      0.99     178      1
1                      1.99     786      1

(貸方としての負の価格と借方としての正の価格)

4

2 に答える 2

0

これを試してみてください

select content_type_code_id
    , ABS(price)
    , count(IF(price >= 0,1,null)) AS debits,
    , count(IF(price < 0,1,null)) 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 price ASC
于 2012-07-23T19:09:07.710 に答える
0

これを試して:

SELECT  content_type_code_id
     ,  price * -1
     ,  COUNT(price) AS PRICECOUNT
     ,  (
          SELECT  COUNT (deb.price)
            FROM  dbo.transaction_unrated deb
            WHERE deb.transaction_date >= '2012/05/01'
            AND   deb.transaction_date < '2012/06/01'
            AND   deb.content_provider_code_id IN (1)
            AND   deb.price = ( dbo.transaction_unrated.price * -1 )
        )
  FROM  dbo.transaction_unrated
  WHERE transaction_date >= '2012/05/01'
  AND   transaction_date < '2012/06/01'
  AND   content_provider_code_id IN (1)
  AND   price < 0
  GROUP BY content_type_code_id
         , price
         , 4
  ORDER BY price ASC
于 2012-07-23T19:18:45.563 に答える