第一个问题,希望有人可以在这里提供帮助。我一直试图找出如何做到这一点,并认为是时候要求协助。请注意我对My SQL查询很新,而且我的代码可能不像它那样优雅!
我正在使用两个配置相同的数据库。每个数据库代表区域数据,但它们之间的所有表和列都是相同的。从本质上讲,我希望每天都能获得特定个人的活动。此外,我需要收集的数据是在多个表上,所以请耐心等我在这里写下我的符号:两个数据库的D_USA和D_CAN,我加入的两个表的T1和T2。这是我现有的代码和输出:
SELECT
"USA" AS REGION,
LEFT(FROM_UNIXTIME(D_USA.T1.date),10) as DATE,
D_USA.T1.owner AS OWNER, COUNT(DISTINCT D_USA.T1.id) AS GROUPS,
COUNT(DISTINCT D_USA.T2.id) AS UNITS
FROM
D_USA.T1
JOIN D_USA.T2 ON D_USA.T1.id = D_USA.T2.parent_id
WHERE
D_USA.T1.complete='Y'
AND D_USA.T1.owner = 'XYZ'
AND D_USA.T2.status = 'P'
AND LEFT(FROM_UNIXTIME(D_USA.T1.date),10) >= '2012-01-01'
GROUP BY DATE
UNION ALL
SELECT
"CAN" AS REGION,
LEFT(FROM_UNIXTIME(D_CAN.T1.date),10) as DATE,
D_CAN.T1.owner AS OWNER,
COUNT(DISTINCT D_CAN.T1.id) AS GROUPS,
COUNT(DISTINCT D_CAN.T2.id) AS UNITS
FROM
D_CAN.T1
JOIN D_CAN.T2 ON D_CAN.T1.id = D_CAN.T2.parent_id
WHERE
D_CAN.T1.complete='Y'
AND D_CAN.T1.owner = 'XYZ'
AND D_CAN.T2.status = 'P'
AND LEFT(FROM_UNIXTIME(D_CAN.T1.date),10) >= '2012-01-01'
GROUP BY DATE
ORDER BY DATE ASC;
当前输出:
REGION|DATE|OWNER|GROUPS|UNITS
USA|2012-01-01|XYZ|10|100
CAN|2012-01-01|XYZ|5|100
USA|2012-01-02|XYZ|10|75
USA|2012-01-03|XYZ|5|50
CAN|2012-01-03|XYZ|10|50
CAN|2012-01-04|XYZ|10|100
期望输出(基于以上)
DATE|OWNER|GROUPS|UNITS
2012-01-01|XYZ|15|200
2012-01-02|XYZ|10|75
2012-01-03|XYZ|15|100
2012-01-04|XYZ|10|100
非常感谢任何帮助!
答案 0 :(得分:0)
只需在
中包围一个选择选择日期,所有者,组,总和(单位)作为单位 从(在此处插入您的查询)somedummyName 按日期,所有者,组
分组