对于管理员我执行某种类型的SQL操作。记录属于以下类型。
date number of failed login
13/02/2017 1
12/02/2017 2
11/02/2017 0
10/02/2017 9
09/02/2017 0
我希望单行中的0,9,0,2,1。
我尝试过以下查询。但默认情况下未能获得零。 用户必须在哪个用户日执行零操作。
select LISTAGG(cnt, ', ')
from (select count(*) as cnt
from OPERATIONINFO
where OPERATIONTYPE='FAILEDLOGIN'
and CUSTID = 123
and date(UPDATEDDATEOFLOGIN) > (current date - 5 day)
group by YEAR(date), MONTH(date), DAY(date)
请帮忙。
答案 0 :(得分:0)
您需要使用解码来转动数据。看看这个带有这个
的例子INSERT INTO Sales VALUES
(2004, 1, 20),
(2004, 2, 30),
(2004, 3, 15),
(2004, 4, 10),
(2005, 1, 18),
(2005, 2, 40),
(2005, 3, 12),
(2005, 4, 27);
到这个
Year Q1 Q2 Q3 Q4
---- -- -- -- --
2004 20 30 15 10
2005 18 40 12 27
SELECT Year,
MAX(DECODE(Quarter, 1, Results)) AS Q1,
MAX(DECODE(Quarter, 2, Results)) AS Q2,
MAX(DECODE(Quarter, 3, Results)) AS Q3,
MAX(DECODE(Quarter, 4, Results)) AS Q4
FROM Sales
GROUP BY Year;
答案 1 :(得分:0)
你需要一个日期表......我在这里使用递归动态制作 - 不是最好的方式......
WITH DATES_TABLE AS
(
SELECT CURRENT DATE - 5 DAYS AS D FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT D + 1 DAY AS D FROM DATES_TABLE WHERE D < CURRENT DATE
), THE_DATA AS
(
select D.D, SUM(CASE WHEN O.UPDATEDDATEOFLOGIN IS NOT NULL THEN 1 ELSE 0 END) as cnt
from DATES_TABLE D
LEFT JOIN OPERATIONINFO O ON O.UPDATEDDATEOFLOGIN = D.D
where O.OPERATIONTYPE='FAILEDLOGIN'
and O.CUSTID = 123
group by D.D
)
select LISTAGG(cnt, ', ')
from THE_DATA