-1

列名を持つテーブルがあります:

カテゴリ1、カテゴリ2、カテゴリ3、値

テーブルから次の組み合わせを取得したい:

---> category1,count(*)

---> category1, category2, count(*)

---> category2, category3, count(*)

1 つのクエリでこれを行う方法はありますか、それとも 3 つの個別のクエリを書く必要がありますか? 以下のようなデザインを考えています。

Category1, Category2, Category3, CountNumber

ケース 1、カテゴリ 2、およびカテゴリ 3 の列は空白になり、ケース 2 のカテゴリ 3 列は空白になります。

--------EXAMPLE---------------------------------------------------------
Cat1           Cat2          Cat3        Value
a              NULL           d1           13
b              e1             d1           13
a              e2             d1           13
c              NULL           d2           13
a              e1             d1           13
a              NULL           d1           13
--------DESIRED OUTPUT -------------------------------------------------
Cat1           Cat2           Cat3           CountNumber
a              NULL           NULL           4
b              NULL           NULL           1
c              NULL           NULL           1
a              e1             NULL           1
c              e1             NULL           0
NULL           e1             d1             2

など ありがとう

4

1 に答える 1

1

これを試して

Select category1, null Category2, null category3, count(*)
from table
group by category1
UNION ALL
Select category1, category2, null category3, count(*)
from table
group by category1, category2
UNION ALL
Select null  category1,category2, category3, count(*)
from table
group by category2, category3

tmpテーブルに行を挿入するには

SELECT * INTO TmpTable FROM (
Select category1, null Category2, null category3, count(*)
from table
group by category1
UNION ALL
Select category1, category2, null category3, count(*)
from table
group by category1, category2
UNION ALL
Select null  category1,category2, category3, count(*)
from table
group by category2, category3) x
于 2013-01-17T18:50:26.307 に答える