first question, hoping someone can help here. I’ve been trying to track down how to do this for a bit and figured it was time to ask for an assist. Please note I’m very new to My SQL queries and my code is probably as inelegant as it gets!
I’m working with two databases with identical configurations. Each database represents regional data but all tables and columns are identical between them. In essence I’m looking to get a daily count of activities performed by a specific individual. Also, the data I need to gather is on multiple tables so bear with me s I make my notations here: D_USA and D_CAN for the two databases, T1 and T2 for the two tables I’m joining. Here is my existing code and the output I get:
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;
Current Output:
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
Desired Output (based on above)
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
Any assistance will be greatly appreciated!
Just wrap a select round it as in
Select Date,Owner,Groups, Sum(Units) as Units
From (insert your query in here) somedummyName
Group By Date,Owner,Groups