I have this query
select count(*) as COUNT_A, datepart(yyyy, [Tgl_Perolehan]) as [year]
from [table_name]
where Sertifikat_Nomor IS NOT NULL
group by datepart(yyyy, [Tgl_Perolehan])
and this
select count(*) as COUNT_B, datepart(yyyy, [Tgl_Perolehan]) as [year]
from [table_name]
where Sertifikat_Nomor IS NULL
group by datepart(yyyy, [Tgl_Perolehan])
The difference is only IS NOT NULL
and IS NULL
and the result is
+---------+--------+ | COUNT_A | year | +----------+--------+ | 12 | 1991 | | 15 | 1993 | | 24 | 1998 | +----------+--------+
I want make like this
+---------+---------+--------+ | COUNT_A | COUNT_B | year | +----------+---------+--------+ | 12 | 23 | 1991 | | 15 | 33 | 1993 | | 24 | 13 | 1998 | +----------+---------+--------+
and I try like this
select (select count(*) as COUNT_A, datepart(yyyy, [Tgl_Perolehan]) as [year]
from [table_name]
where Sertifikat_Nomor IS NOT NULL
group by datepart(yyyy, [Tgl_Perolehan])),
(select count(*) as COUNT_B, datepart(yyyy, [Tgl_Perolehan]) as [year]
from [table_name]
where Sertifikat_Nomor IS NULL
group by datepart(yyyy, [Tgl_Perolehan]))
But didn't fix my problem. I have use CASE WHEN
, but still didn't fix the problem. How to merge and fix their? Thanks.
Try this-
SELECT
SUM(CASE WHEN ertifikat_Nomor IS NOT NULL THEN 1 ELSE 0 END) AS COUNT_A,
SUM(CASE WHEN Sertifikat_Nomor IS NULL THEN 1 ELSE 0 END)AS COUNT_B,
DATEPART(yyyy, [Tgl_Perolehan]) AS [year]
FROM[table_name]
GROUP BY DATEPART(yyyy, [Tgl_Perolehan])
Try this using co-related subquery
SELECT Datepart(yyyy, o.[Tgl_Perolehan]) AS [Tgl_Perolehan] ,
(
SELECT Count(*)
FROM [table_name] i
WHERE i.<somid> = o.<somid>
AND i.sertifikat_nomor IS NOT NULL ) AS AS count_a ,
(
SELECT count(*)
FROM [table_name] ii
WHERE ii.<somid> = o.<somid>
AND ii.sertifikat_nomor IS NULL) AS AS count_b
FROM [table_name] o
GROUP BY datepart(yyyy,o.[Tgl_Perolehan])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.