27,580
社区成员
发帖
与我相关
我的任务
分享
USE [XXXX]
SELECT
a.product,
a.station_id,
a.test_count,
b.ng_count,
c.ng_code_count,
c.ng_code,
cast(cast(round(c.ng_code_count/cast(b.ng_count as decimal(6, 1))*100, 2) as numeric(9, 2)) as varchar(10))+'%' as 'percentage'
FROM
(
SELECT
product,
station_id,
count(1) 'test_count'
FROM
[statistics_table]
WHERE
result is not NULL
--and product = 'P_001'
--and station_id = 'S_001'
GROUP BY
product,
station_id
) a
inner join
(
SELECT
product,
station_id,
count(1) 'ng_count'
FROM
[statistics_table]
WHERE
result = 'FALSE'
GROUP BY
product,
station_id
) b
ON
a.product = b.product
and a.station_id = b.station_id
inner join
(
SELECT
product,
station_id,
ng_code,
count(1) 'ng_code_count'
FROM
[statistics_table]
WHERE
result = 'FALSE'
GROUP BY
product,
station_id,
ng_code
) c
ON
a.product = c.product
and a.station_id = c.station_id
WHERE
result is not NULL
--and product = 'P_001'
select a.product,a.station_id,a.test_count,
b.ng_count,b.ng_code
from
(select product,
station_id,
count(1) 'test_count'
from [表名]
group by product,station_id) a
inner join
(select product,station_id,ng_code,count(1) 'ng_count'
from [表名]
where result='FALSE'
group by product,station_id,ng_code) b
on a.product=b.product and a.station_id=b.station_id
select
[product], [station_id],
count()over(partition by station_id) as test_count ,
count(1) as ng_count ,ng_code
from
[statistics_table]
group by
[product],[station_id],[ng_code]
--TRY--SELECT
-- [product],
-- [station_id],
-- [ng_code]
--FROM
-- [statistics_table]
--WHERE
-- [product] = 'P_001'
-- and [station_id] = 'S_001'
-- GROUP BY [product],[station_id],[ng_code]
SELECT
[product],
[station_id]
FROM
[statistics_table]
WHERE
[product] = 'P_001'
and [station_id] = 'S_001'
GROUP BY [product],[station_id]