1

結果セットのすべての行の各列の合計を取得するにはどうすればよいですか? つまり、合計行を結果セットの最後の行として表示したいということです。

私のクエリは次のようになります。

select f.filename,  
count(distinct case when v.rUser like '%bike%' then 1 else null end) as bikeUser, 
count(distinct case when v.rUser like '%Pedestrian%' then 1 else null end) as pedestrianUser,  
count(distinct case when d.weather like '%clear%' then 1 else null end) as clearWeather, 
count(case when m.extras like '%hat%' then 1 else null end) as hatExtras 
from VMdata v  
inner join files f on v.id = f.id 
inner join DMdata d on f.id = d.id 
inner join MultiFiledata m on f.id = m.id 
where f.filename in (X,Y,Z) group by f.filename; 

「group by」句の後にロールアップを使用すると、「group by句」によって生成されたグループの合計(水平合計)が得られますが、最後に各列の垂直合計が必要です。

何か案は?

4

3 に答える 3

2

あなたの ROLLUP はすでに正しいです。実際にCOUNT(CASE WHEN Field = 'Blah' THEN 1 ELSE 0 END)は、クエリが機能しない主な原因です

次のいずれかを行う必要があります。

COUNT(CASE WHEN Field = 'Blah' THEN 1 END)

また

SUM(CASE WHEN Field = 'Blah' THEN 1 ELSE 0 END)

しかし、MySQL は C 言語のようにブール型と整数型の間に二重性があるため、次のようにすることもできます。

SUM(Field = 'Blah')

これは間違ったクエリです (間違った結果): http://www.sqlfiddle.com/#!2/70187/1

create table ProductInventory(
  ProductCode varchar(10) not null,
  Location varchar(50) not null
);


insert into ProductInventory(ProductCode,Location) values('CPU','US');
insert into ProductInventory(ProductCode,Location) values('CPU','US');
insert into ProductInventory(ProductCode,Location) values('CPU','CN');
insert into ProductInventory(ProductCode,Location) values('KB','CN');
insert into ProductInventory(ProductCode,Location) values('KB','JP');
insert into ProductInventory(ProductCode,Location) values('KB','US');
insert into ProductInventory(ProductCode,Location) values('MOUSE','US');
insert into ProductInventory(ProductCode,Location) values('MOUSE','CN');

間違った出力:

PRODUCTCODE USQTY   CHINAQTY
CPU         3       3
KB          3       3
MOUSE       2       2
            8       8

これは正しいクエリです: http://www.sqlfiddle.com/#!2/70187/2

select ProductCode, 
    COUNT(CASE WHEN Location = 'US' THEN 1 END) as UsQty,
    COUNT(CASE WHEN Location = 'CN' THEN 1 END) as ChinaQty
from ProductInventory
group by ProductCode with rollup

正しい出力:

PRODUCTCODE     USQTY   CHINAQTY
CPU             2       1
KB              1       1
MOUSE           1       1
                4       3

これが正しいと主張しないでください。これは非常に間違っています。

COUNT(CASE WHEN Location = 'US' THEN 1 ELSE 0 END) AS UsQty

これを行う必要があります(正しい):

COUNT(CASE WHEN Location = 'US' THEN 1 END) AS UsQty

またはこれ(正しい):http://www.sqlfiddle.com/#!2/70187/5

SUM(CASE WHEN Location = 'US' THEN 1 ELSE 0 END) AS UsQty

またはこれ(正しい):http://www.sqlfiddle.com/#!2/70187/6

SUM(CASE WHEN Location = 'US' THEN 1 END) AS UsQty

または、MySql にはブール値と整数の間に二重性があるという事実を利用してみてください (正しい): http://www.sqlfiddle.com/#!2/70187/4

SUM(Location = 'US') AS UsQty

ボトムライン

これを使用しないでください (間違っています): http://www.sqlfiddle.com/#!2/70187/3

COUNT(Location = 'US') as UsQty

また、これも使用しないでください (クエリと同様に間違っています): http://www.sqlfiddle.com/#!2/70187/1

COUNT(CASE WHEN Location = 'US' THEN 1 ELSE 0 END) as UsQty

ちなみに、これも機能します。理由はあなた次第です ;-)

COUNT(CASE WHEN Location = 'US' THEN 1976 END) AS UsQty

アップデート

これがあなたが必要としているものだと私は感じています:

create table Product
(
  ProductCode varchar(10) not null primary key,
  ProductName varchar(100) not null 
);


insert into Product(ProductCode,ProductName) values
('CPU','Central Processing Unit'),
('KB','Keyboard'),
('MSE','Mouse'),
('RAM', 'Random Access Memory');


create table ProductInventory(
  ProductCode varchar(10) not null,
  Location varchar(50) not null
);


insert into ProductInventory(ProductCode,Location) values
('CPU','US'),
('CPU','PH'),
('CPU','PH'),
('KB','PH'),
('KB','US'),
('KB','US'),
('MSE','US'),
('MSE','JP');

select p.ProductCode, 
    coalesce(SUM(i.Location = 'US'),0) as UsQty,
    coalesce(SUM(i.Location = 'PH'),0) as PhilippinesQty
from Product p
left join ProductInventory i on i.ProductCode = p.ProductCode
group by p.ProductCode with rollup

出力:

ProductCode     UsQty           PhilippinesQty
CPU             1               2
KB              2               1
MSE             1               0
RAM             0               0
                4               4

ライブ テスト: http://www.sqlfiddle.com/#!2/2bb09/1

于 2012-04-27T11:27:53.880 に答える
1

評価を使用している場合は、カウントに COUNT を使用しないでください。名前にもかかわらず、SUM は条件に基づいてカウントするための正しい結果を生成します。

これを考えると:http://www.sqlfiddle.com/#!2/79375/1

create table ProductInventory(
  ProductCode varchar(10) not null,
  Location varchar(50) not null
);


insert into ProductInventory(ProductCode,Location) values('CPU','US');
insert into ProductInventory(ProductCode,Location) values('CPU','US');
insert into ProductInventory(ProductCode,Location) values('CPU','ARM');
insert into ProductInventory(ProductCode,Location) values('KB','CN');
insert into ProductInventory(ProductCode,Location) values('KB','PH');
insert into ProductInventory(ProductCode,Location) values('KB','US');
insert into ProductInventory(ProductCode,Location) values('MOUSE','AA');
insert into ProductInventory(ProductCode,Location) values('MOUSE','BB');

select ProductCode, COUNT(CASE WHEN Location = 'US' THEN 1 ELSE 0 END) as Qty
from ProductInventory
group by ProductCode
order by ProductCode

それは間違った結果をもたらします:

PRODUCTCODE QTY
CPU         3
KB          3
MOUSE       2

代わりに SUM を使用してください。正しい結果: http://www.sqlfiddle.com/#!2/79375/3

select ProductCode, SUM(Location = 'US') as Qty
from ProductInventory
group by ProductCode
order by ProductCode

その結果、次のようになります。

PRODUCTCODE QTY
CPU         2
KB          1
MOUSE       0

COUNT は、値または式の非 null 性をカウントすることによって機能します


それでも COUNT を使用する場合は、null 以外の値を に渡しますCOUNT。を使用しないELSE NULL ENDでください。クエリは退屈に見えます:-) http://www.sqlfiddle.com/#!2/79375/4

select ProductCode, COUNT(CASE WHEN Location = 'US' THEN Location END) as Qty
from ProductInventory
group by ProductCode
order by ProductCode

出力:

PRODUCTCODE QTY
CPU         2
KB          1
MOUSE       0
于 2012-04-27T08:37:10.433 に答える
0

一時テーブル (#mytemptable) を作成し、合計を最後のエントリとして追加してから、作成した一時テーブルの選択を返すことができます。しかし、これはストアドプロシージャでのみ機能していると思います。

select f.filename,  
count(case when v.rUser like '%bike%' then 1 else 0 end) as bikeUser, 
count(case when v.rUser like '%Pedestrian%' then 1 else 0 end) as pedestrianUser,  
count(case when d.weather like '%clear%' then 1 else 0 end) as clearWeather, 
count(case when m.extras like '%hat%' then 1 else 0 end) as hatExtras 
INTO #myTempTable from VMdata v  
inner join files f on v.id = f.id 
inner join DMdata d on f.id = d.id 
inner join MultiFiledata m on f.id = m.id 
where f.filename in (X,Y,Z) group by f.filename; 

INSERT INTO #myTempTable select(create the sum here but care that you have the same columns)

SELECT * FROM #myTempTable

たった1回の選択で、コマンド「UNION」でそれを行うことができます

*編集: 27.04. 11:55

select f.filename,  
count(case when v.rUser like '%bike%' then 1 else 0 end) as bikeUser, 
count(case when v.rUser like '%Pedestrian%' then 1 else 0 end) as pedestrianUser,  
count(case when d.weather like '%clear%' then 1 else 0 end) as clearWeather, 
count(case when m.extras like '%hat%' then 1 else 0 end) as hatExtras 
from VMdata v  
inner join files f on v.id = f.id 
inner join DMdata d on f.id = d.id 
inner join MultiFiledata m on f.id = m.id 
where f.filename in (X,Y,Z) group by f.filename;
UNION
SELECT SUM(bikeUser), SUM(pedestrianUser), SUM(clearWeather), SUM(hatExtras)
FROM (
select f.filename,  
count(case when v.rUser like '%bike%' then 1 else 0 end) as bikeUser, 
count(case when v.rUser like '%Pedestrian%' then 1 else 0 end) as pedestrianUser,  
count(case when d.weather like '%clear%' then 1 else 0 end) as clearWeather, 
count(case when m.extras like '%hat%' then 1 else 0 end) as hatExtras 
from VMdata v  
inner join files f on v.id = f.id 
inner join DMdata d on f.id = d.id 
inner join MultiFiledata m on f.id = m.id 
where f.filename in (X,Y,Z) group by f.filename) as summary

お役に立てれば :)

于 2012-04-27T08:31:37.947 に答える