首先让我说我不是sql专家或jasper-reports专家,但我正在尝试生成一份报告,显示每月由公司和客户登录我的项目的人。我也知道我正在设计查询以查看未来1个月,以便我有数据来处理A公司注册的机会创建客户端或2然后不登录或做任何事情。
这就是我想要的返回数据:
Date Company Client Client_ID Authentications Unique_Users
2016-may Company-A client-1 1 24 1
2016-may Company-A client-2 2 10 2
2016-may Company-A client-3 3 0 0
2016-June Company-A client-1 1 0 0
2016-June Company-A client-2 2 0 0
2016-June Company-A client-3 3 0 0
我有这个查询,它会在发生操作的月份内返回日期,公司名称,客户名称,客户端ID,身份验证和唯一身份用户。此查询会遗漏没有身份验证的行。
SELECT
DATE_FORMAT(rq.requestTime, '%Y-%M') AS Date,
company.name AS Company,
client.name AS Client,
client.id AS client_id,
COUNT(rq.id) AS Authentications,
COUNT(DISTINCT rq.personguid) AS Unique_Users
FROM
company JOIN CLIENT ON company.id = client.company_id
LEFT JOIN request_queue rq ON rq.clientid = client.id
WHERE
company.id = 19
AND rq.status = 'complete' AND rq.request_type LIKE "%authorize%"
AND MONTH(rq.requestTime) >= MONTH("2016-05-01")
AND MONTH(rq.requestTime) <= MONTH("2016-06-01")
GROUP BY
client_id, Date
ORDER BY Date ASC, client_id ASC;
这会返回类似这样的内容,因为有些客户端在5月份没有登录,6月份还没有记录,基本上没有request_queue数据没有行:
Date Company Client Client_ID Authentications Unique_Users
2016-may Company-A client-1 1 24 1
2016-may Company-A client-2 2 10 2
此时我想出了这个。
SELECT
dateTable.mydate AS Date,
clientTable.ClientName AS Client,
clientTable.CompanyName AS Company,
clientTable.client_id AS Client_ID
FROM
(SELECT
client.name AS ClientName,
client.id AS client_id,
company.name AS CompanyName
FROM
company JOIN CLIENT ON company.id = client.company_id
WHERE company.id = 19) clientTable
JOIN
(SELECT
DATE_FORMAT(temp.thedate,'%Y-%M') AS mydate,
temp.thedate AS theDate FROM (
SELECT
DATE_ADD("2016-05-01", INTERVAL u.i MONTH) AS thedate
FROM i AS u ORDER BY thedate) temp
WHERE temp.thedate <= "2016-06-01") dateTable;
这给了我一张表,其中包含日期,公司名称,客户名称和没有request_queue数据的客户ID。
此表如下所示:
Date Company Client Client_ID
2016-may Company-A client-1 1
2016-may Company-A client-2 2
2016-may Company-A client-3 3
2016-June Company-A client-1 1
2016-June Company-A client-2 2
2016-June Company-A client-3 3
有没有办法合并这些数据以获得位于此帖子顶部的图表?
其他信息: 目标是将我的查询结果传递给jasper报表设计器ireport。我正在使用这些数据创建一个交叉表,其中包含顶部的日期和左侧的客户端,并将认证放在交叉行中。
我从大学几年前就没有触及sql。任何想法或指导将不胜感激。
答案 0 :(得分:1)
我从你上次的查询中解脱出来,这似乎正在产生你需要的东西:
SELECT
dateTable.mydate AS Date,
clientTable.ClientName AS Client,
clientTable.CompanyName AS Company,
clientTable.client_id AS Client_ID,
COUNT(rq.id) AS Authentications,
COUNT(DISTINCT rq.personguid) AS Unique_Users
FROM
(SELECT
client.name AS ClientName,
client.id AS client_id,
company.name AS CompanyName
FROM company JOIN CLIENT ON company.id = client.company_id
WHERE company.id = 19) clientTable
JOIN (SELECT
DATE_FORMAT(temp.thedate,'%Y-%M') AS mydate,
temp.thedate AS theDate
FROM (SELECT
DATE_ADD("2016-05-01", INTERVAL u.i MONTH) AS thedate
FROM i AS u ORDER BY thedate) temp
WHERE temp.thedate <= "2016-06-01") dateTable
LEFT JOIN request_queue rq on (rq.clientid = clientTable.client_ID and DATE_FORMAT(rq.requestTime,'%Y-%M') = dateTable.mydate)
GROUP BY Client_ID, Date
ORDER BY Date ASC, Client_ID ASC;
当然,这是一个不考虑请求类型和状态的简化版本,但我认为您可以从这里轻松地进行操作。