I am stucked with this problem from long time. Though you will find this is big question, but it's not............
I have following tables, with dummy data, so you can also try it.
users Table with Columns
emp_id emp_Name Joining_Date state_id
1 Lauren 11-07-2011 123
2 John 01-08-2012 234
3 Smith 02-09-2011 234
____________________________________________________
CREATE TABLE users
( emp_id int
, emp_Name varchar(20)
, Joining_Date DATE
, state_id int
);
INSERT INTO users (emp_id, emp_Name, Joining_Date, state_id)
VALUES
(1, 'John', '2011-08-10', 123) ,
(2, 'Smith', '2011-09-11', 234) ;
__________________________________________________________________
cl_doctor_call Table
Subject Call_Date call_Done_By(emp_id)
Call 15-01-2012 1
CA 21-02-2012 2
___________________________________________________________________
CREATE TABLE cl_doctor_call
( Subject VARCHAR(10)
, Call_Date DATE
, call_Done_By INT
) ;
INSERT INTO cl_doctor_call (Subject, Call_Date,call_Done_By)
VALUES
('sub1', '2011-08-15', 1) ,
('sub2', '2011-09-16', 2) ;
___________________________________________________________________
cl_ chemist Table
Subject Call_Date call_Done_By(emp_id)
Chemist 1-02-2012 2
Texo 21-03-2012 1
____________________________________________________________________________________
CREATE TABLE cl_chemist ( Subject VARCHAR(10), Call_Date DATE, call_Done_By INT );
INSERT INTO cl_chemist (Subject, Call_Date,call_Done_By) VALUES ('sub3','2011-08-19',2),('sub5','2011-09-25',1);
___________________________________________________________
cl_Stock Table
Subject Call_Date call_Done_By(emp_id)
Sub1 1-02-2012 1
Sub2 21-03-2012 3
____________________________________________________________
CREATE TABLE cl_Stock
( Subject VARCHAR(10)
, Call_Date DATE
, call_Done_By INT
);
INSERT INTO cl_Stock (Subject, Call_Date,call_Done_By)
VALUES
('ABC', '2011-10-13', 1) ,
('sub5', '2011-11-17', 2) ;
______________________________________________________
Meetings Table
Subject Meeting_Date call_Done_By(emp_id)
Sub1 11-02-2012 1
Sub2 23-03-2012 2 _____________________________________________________________________________________
CREATE TABLE Meetings ( Subject VARCHAR(10), meet_Date DATE, meet_Done_By INT );
INSERT INTO Meetings (Subject, meet_Date,meet_Done_By)
VALUES
('Planning','2011-11-01',2),
('Deploy','2011-12-15',1);
______________________________________________________________________________________
Leave Table
Subject from_Date to_date Requested_By(emp_id) status
Sub1 01-02-2012 03-02-2012 2 Declined
Sub2 21-03-2012 22-03-2012 1 Approved
Holiday Table
Holiday_Name Holiday_Date States_Id
New Year 01-01-2012 123
Independence Day 15-08-2012 234
______________________________________________________________________________________
CREATE TABLE Holiday ( Holiday_Name VARCHAR(10),Holiday_Date DATE, State_Id_c INT );
INSERT INTO Holiday (Holiday_Name,Holiday_Date,State_Id_c) VALUES ('NEW_YEAR','2012-01-01',123), ('CHRISHMAS','2011-12-25',234);
______________________________________________________________________________________
Replace with date
for stock='Y'
for chemist='Y'
for doctor='Y'
for leave='L'
for Sunday='S'
for meeting ='M'
for Holiday='H'
Hope so still you are with me.
Now Admin will select 'month' and 'year' and according to that attendance report for
employee will displayed.
Output :
Employee Year Month 1 2 3 4 5 6 7.... for all 31 days
John 2011 Nov Y Y Y H Y L S....
Smith 2011 Nov Y Y Y H Y M S.....
. . . ................
. . . ................
&so on &so on &so on & so on
here in output 1,2,3.... are days from 0-31 for a month which we can write using 'case'
Consider if employee is present on day show its status as 'Y' ,if he is on leave show it's status as 'L; if there is Sunday show status 'S' for holiday show 'H' and so on.
So basically I want a write a query to retrieve all these details.
So pl z suggest me ways here, that How can I display this attendance Report by a query or procedure.
I have tried so far this .....
DELIMITER $$
USE `leosatyen_claris`$$
DROP PROCEDURE IF EXISTS `Test`$$
CREATE DEFINER=`leosatyen_claris`#`%` PROCEDURE `Test`(IN month_last_date DATE)
BEGIN
SELECT
users.id AS Id,
users.first_name AS Employee,
CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='01' THEN main.task
WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 1 DAY),'%W')='SUNDAY' THEN 0
WHEN main.cdate IS NULL THEN -1
ELSE -2 END)
WHEN -1 THEN 'NA'
WHEN 0 THEN 'S'
WHEN 1 THEN 'Y'
WHEN 2 THEN 'C'
WHEN 3 THEN 'S1'
WHEN 4 THEN 'P'
WHEN 5 THEN 'M'
END Day1,
CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='02' THEN main.task
WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 2 DAY),'%W') ='SUNDAY' THEN 0
WHEN main.cdate IS NULL THEN -1
ELSE -2 END)
WHEN -1 THEN 'NA'
WHEN 0 THEN 'S'
WHEN 1 THEN 'Y'
WHEN 2 THEN 'C'
WHEN 3 THEN 'S1'
WHEN 4 THEN 'P'
WHEN 5 THEN 'M'
END Day2,
.
.
. ## UP TO 31 DAYS
CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='31' THEN main.task
WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 31 DAY),'%W')='SUNDAY' THEN 0
WHEN main.cdate IS NULL THEN -1
ELSE -2 END)
WHEN -1 THEN 'NA'
WHEN 0 THEN 'S'
WHEN 1 THEN 'Y'
WHEN 2 THEN 'C'
WHEN 3 THEN 'S1'
WHEN 4 THEN 'P'
WHEN 5 THEN 'M'
END Day31
FROM
users
LEFT JOIN
(
SELECT DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')AS cdate,
cl_doctor_call.created_by AS emp,
1 task
FROM
cl_doctor_call
WHERE
DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')>month_last_date AND
DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)
UNION ALL
SELECT
DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d') AS cdate,
cl_chemist_call.created_by AS emp,
2 task
FROM
cl_chemist_call
WHERE
DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d')>month_last_date AND
DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)
UNION ALL
SELECT
DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d') AS cdate,
cl_stockist_call.created_by AS emp,
3 task
FROM
cl_stockist_call LEFT JOIN cl_stockist_call_cstm
ON cl_stockist_call.id=cl_stockist_call_cstm.id_c
WHERE
DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d')> month_last_date AND
DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)
UNION ALL
SELECT DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d') AS cdate,
cl_product_pramotional_call.created_by AS emp,
4 task
FROM cl_product_pramotional_call
WHERE
DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d')>month_last_date AND
DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)
UNION ALL
SELECT DATE_FORMAT(meetings.date_start,'%Y-%m-%d') AS cdate,
meetings.created_by AS emp,
5 task
FROM meetings
WHERE
DATE_FORMAT(meetings.date_start,'%Y-%m-%d')> month_last_date AND
DATE_FORMAT(meetings.date_start,'%Y-%m-%d') <= DATE_ADD(month_last_date, INTERVAL 30 DAY)
)AS main
ON main.emp = users.id
WHERE DATE_FORMAT(users.date_entered,'%Y-%m-%d')<= month_last_date
GROUP BY users.id ;
END$$
DELIMITER ;
Here I am getting all Sunday's of that month AND All customer calls like Doctor_call,
chemist call, stockist call and meetings entries correctly.
Now I just need to add 'Leave' and Holiday Entries. So anybody can assist me to achieve holidays and leave in my existing procedure?
any suggestions will be helpful for me....
Finally I achieved answer for Attendance Report.
We need to execute this lengthy stored procedure to get monthly attendance of all Employees.
To execute this procedure we need to pass IN parameter as last date of previous month like if we need to see December 2011 attendance Report pass '2011-11-30' AS IN parameter.
DELIMITER $$
USE `Sample`$$
DROP PROCEDURE IF EXISTS `Test`$$
CREATE DEFINER=`Sample`#`%` PROCEDURE `Test`(IN month_last_date DATE)
BEGIN
SELECT
users.id AS Id,DATE_FORMAT(month_last_date,'%Y')AS YEAR,DATE_FORMAT(month_last_date,'%M')AS MONTH,
CONCAT(users.first_name,users.last_name) AS Employee,cl_territories.name AS Territory,
CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='01' THEN main.task
WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 1 DAY),'%W')='SUNDAY' THEN 0
WHEN main.cdate IS NULL THEN -1
ELSE -2 END)
WHEN -1 THEN 'NA'
WHEN 0 THEN 'S'
WHEN 1 THEN 'Y'
WHEN 2 THEN 'C'
WHEN 3 THEN 'S1'
WHEN 4 THEN 'P'
WHEN 5 THEN 'M'
WHEN 6 THEN 'H'
WHEN 7 THEN 'L'
END Day1,
CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='02' THEN main.task
WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 2 DAY),'%W') ='SUNDAY' THEN 0
WHEN main.cdate IS NULL THEN -1
ELSE -2 END)
WHEN -1 THEN 'NA'
WHEN 0 THEN 'S'
WHEN 1 THEN 'Y'
WHEN 2 THEN 'C'
WHEN 3 THEN 'S1'
WHEN 4 THEN 'P'
WHEN 5 THEN 'M'
WHEN 6 THEN 'H'
WHEN 7 THEN 'L'
END Day2,
.
.
. # upto 31 days
CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='31' THEN main.task
WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 31 DAY),'%W')='SUNDAY' THEN 0
WHEN main.cdate IS NULL THEN -1
ELSE -2 END)
WHEN -1 THEN 'NA'
WHEN 0 THEN 'S'
WHEN 1 THEN 'Y'
WHEN 2 THEN 'C'
WHEN 3 THEN 'S1'
WHEN 4 THEN 'P'
WHEN 5 THEN 'M'
WHEN 6 THEN 'H'
WHEN 7 THEN 'L'
END Day31
FROM
users LEFT JOIN users_cstm
ON users.id=users_cstm.id_c
LEFT JOIN cl_territories
ON users_cstm.cl_territories_id1_c=cl_territories.id
LEFT JOIN
(
SELECT DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')AS cdate,
cl_doctor_call.created_by AS emp,
1 task
FROM
cl_doctor_call
WHERE
DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')>month_last_date AND
DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)
UNION ALL
SELECT
DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d') AS cdate,
cl_chemist_call.created_by AS emp,
2 task
FROM
cl_chemist_call
WHERE
DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d')>month_last_date AND
DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)
UNION ALL
SELECT
DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d') AS cdate,
cl_stockist_call.created_by AS emp,
3 task
FROM
cl_stockist_call LEFT JOIN cl_stockist_call_cstm
ON cl_stockist_call.id=cl_stockist_call_cstm.id_c
WHERE
DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d')> month_last_date AND
DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)
UNION ALL
SELECT DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d') AS cdate,
cl_product_pramotional_call.created_by AS emp,
4 task
FROM cl_product_pramotional_call
WHERE
DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d')>month_last_date AND
DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)
UNION ALL
SELECT DATE_FORMAT(meetings.date_start,'%Y-%m-%d') AS cdate,
meetings.created_by AS emp,
5 task
FROM meetings
WHERE
DATE_FORMAT(meetings.date_start,'%Y-%m-%d')> month_last_date AND
DATE_FORMAT(meetings.date_start,'%Y-%m-%d') <= DATE_ADD(month_last_date, INTERVAL 30 DAY)
UNION ALL
SELECT DATE_FORMAT(cl_holidays_structures.holiday_date,'%Y-%m-%d') AS cdate,
users.id AS emp,
6 task
FROM users LEFT JOIN users_cstm
ON users.id=users_cstm.id_c
LEFT JOIN cl_states
ON users_cstm.cl_states_id1_c=cl_states.id
LEFT JOIN cl_holidays_structures
ON cl_holidays_structures.cl_states_id_c=users_cstm.cl_states_id1_c
WHERE
DATE_FORMAT(cl_holidays_structures.holiday_date,'%Y-%m-%d')> month_last_date AND
DATE_FORMAT(cl_holidays_structures.holiday_date,'%Y-%m-%d') <= DATE_ADD(month_last_date, INTERVAL 30 DAY)
UNION ALL
SELECT cl_leave_management_cstm.from_date_c+ INTERVAL td.days DAY cdate,
users.id AS emp,
7 task
FROM
cl_leave_management LEFT JOIN cl_leave_management_cstm
ON cl_leave_management.id=cl_leave_management_cstm.id_c
LEFT JOIN users
ON cl_leave_management.created_by = users.id
JOIN temp_days td
ON DATEDIFF(cl_leave_management_cstm.to_date_c, cl_leave_management_cstm.from_date_c) >= td.days
WHERE DATE_FORMAT(cl_leave_management_cstm.from_date_c,'%Y-%m-%d')> month_last_date AND
DATE_FORMAT(cl_leave_management_cstm.from_date_c,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY) AND
cl_leave_management_cstm.status_c='Approved'
)AS main
ON main.emp = users.id
WHERE DATE_FORMAT(users.date_entered,'%Y-%m-%d')<= month_last_date
GROUP BY users.id ;
END$$
DELIMITER ;
Thanks to all who guide me to get this result.
Related
Is ther any clear way of doing the following in one SQL (PL) code? :)
What I have in the tables is the following:
CONTACT_TBL (HEADER)
CONTRACT_ID
BEGIN_DATE
END_DATE
TOT_AMOUNT
123
13-MAY-16
12-MAY-34
100
456
13-JAN-14
12-DEC-25
300
789
13-SEP-14
12-OCT-34
700
CONTRACT_ACTIVTY (DETAIL)
CONTRACT_ID
SEQNUM
END_DATE
AMOUNT
COMMENTS
123
1
12-MAY-19
25
25 - Initial Expiration
123
2
12-MAY-24
25
25
123
3
12-MAY-29
25
25
123
4
12-MAY-34
25
25 - End of Contract
What we need is to populate for each row:
BEGIN_DATE
For SEQNUM #1 - BEGIN_DATE is always CONTACT_TBL.BEGIN_DATE (in this case 13-MAY-15, a 3 year span)
For SEQNUM #2 to N - BEGIN_DATE is 1 day more than previous row's END_DATE (in this case 12-MAY-19 + 1 = 13-MAY-19, all 5 year spans)
Mark the row 'Active' if the SYSDATE is between BEGIN_DATE and END_DATE (in this case it's Row #2)
CONTRACT_ID
SEQNUM
BEGIN_DATE
END_DATE
AMOUNT
STATUS
123
1
13-MAY-16
12-MAY-19
25
123
2
13-MAY-19
12-MAY-24
25
Active
123
3
13-MAY-24
12-MAY-29
25
123
4
13-MAY-29
12-MAY-34
25
N.B. This answer assumes the db is Oracle
This can be done by using LAG to find the date from the previous row.
LAG can accept 3 parameters, the first of which is the column whose value you want to find from the previous row, the second is how many rows to go back each time (default is 1), and the third is what to display if there is no previous row found.
In your case, you want to default to the begin_date from the contact_tbl if there is no previous row to the first row in the contract_activity table, so you need to join the two tables as well, like so:
WITH contact_tbl AS (SELECT 123 contract_id, to_date('13/05/2016', 'dd/mm/yyyy') begin_date, to_date('12/05/2034', 'dd/mm/yyyy') end_date, 100 tot_amount FROM dual UNION ALL
SELECT 456 contract_id, to_date('13/05/2016', 'dd/mm/yyyy') begin_date, to_date('12/05/2034', 'dd/mm/yyyy') end_date, 100 tot_amount FROM dual UNION ALL
SELECT 789 contract_id, to_date('13/05/2016', 'dd/mm/yyyy') begin_date, to_date('12/05/2034', 'dd/mm/yyyy') end_date, 100 tot_amount FROM dual),
contract_activity AS (SELECT 123 contract_id, 1 seqnum, to_date('12/05/2019', 'dd/mm/yyyy') end_date, 25 amount, '25 - Initial Expiration' comments FROM dual UNION ALL
SELECT 123 contract_id, 2 seqnum, to_date('12/05/2024', 'dd/mm/yyyy') end_date, 25 amount, '25' comments FROM dual UNION ALL
SELECT 123 contract_id, 3 seqnum, to_date('12/05/2029', 'dd/mm/yyyy') end_date, 25 amount, '25' comments FROM dual UNION ALL
SELECT 123 contract_id, 4 seqnum, to_date('12/05/2034', 'dd/mm/yyyy') end_date, 25 amount, '25 - End of Contract' comments FROM dual),
-- end of creating subqueries to mimic your tables
contract_details AS (SELECT ca.contract_id,
ca.seqnum,
LAG(ca.end_date + 1, 1, c.begin_date) OVER (PARTITION BY ca.contract_id ORDER BY seqnum) begin_date,
ca.end_date,
ca.amount
FROM contact_tbl c
INNER JOIN contract_activity ca ON c.contract_id = ca.contract_id)
SELECT contract_id,
seqnum,
begin_date,
end_date,
amount,
CASE WHEN TRUNC(SYSDATE) BETWEEN begin_date AND end_date THEN 'Active' END status
FROM contract_details;
CONTRACT_ID SEQNUM BEGIN_DATE END_DATE AMOUNT STATUS
----------- ---------- ----------- ----------- ---------- ------
123 1 13/05/2016 12/05/2019 25
123 2 13/05/2019 12/05/2024 25 Active
123 3 13/05/2024 12/05/2029 25
123 4 13/05/2029 12/05/2034 25
Your status column is a simple case expression, but since it refers to the calculated begin_date, rather than repeating the calculation, I did the begin_date calculation in one subquery, and then referenced that in the outer query.
Note how I used TRUNC(sysdate) instead of just sysdate in the case expression for the status - DATEs in Oracle have a time component (which defaults to midnight if you don't specify the time), so if you ran the query any time between 12/05/2024 00:00:01 and 12/05/2024 23:59:59, your query wouldn't return a status of Active for the second row if you used sysdate. You need to explicitly truncate sysdate to midnight in order for the comparison to work.
Hope below snippet serve the purpose.
DECLARE
CURSOR cur_contracts IS
SELECT
*
FROM
CONTACT_TBL
ORDER BY
CONTRACT_ID;
CURSOR cur_contracts_act(contact_id NUMBER) IS
SELECT
*
FROM
CONTRACT_ACTIVTY
ORDER BY
CONTRACT_ID,
SEQNUM;
l_ca_begin_date DATE;
l_ca_status VARCHAR2(10);
l_prev_rec_end_date DATE;
BEGIN
FOR i IN cur_contracts
LOOP
contract_activity_seq := 0;
FOR j IN cur_contracts_act (i.CONTRACT_ID)
LOOP
contract_activity_seq := contract_activity_seq + 1;
IF (contract_activity_seq = 1) THEN
l_ca_begin_date := i.begin_date;
ELSE
l_ca_begin_date := l_prev_rec_end_date + 1;
END IF;
IF SYSDATE BETWEEN l_ca_begin_date AND j.end_date THEN
l_ca_status := 'Active';
ELSE
l_ca_status := NULL;
END IF;
l_prev_rec_end_date := j.end_date;
UPDATE CONTRACT_ACTIVTY
SET BEGIN_DATE = l_ca_begin_date,
status = l_ca_status
WHERE CURRENT OF cur_contracts_act;
END LOOP;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN raise_application_error(
-20001,
'An error was encountered - ' || SQLCODE || ' -ERROR- ' || SQLERRM
);
END;
/
I have a table like this two
Table A
date amount B_id
'2020-1-01' 3000000 1
'2019-8-01' 15012 1
'2019-6-21' 90909 1
'2020-1-15' 84562 1
--------
Table B
id type
1 7
2 5
I have to show sum of amount until the last date of each month for the last 12 month.
The query i have prepared is like this..
SELECT num2.last_dates,
(SELECT SUM(amount) FROM A
INNER JOIN B ON A.B_id = B.id
WHERE B.type = 7 AND A.date<=num2.last_dates
),
(SELECT SUM(amount) FROM A
INNER JOIN B ON A.B_id = B.id
WHERE B.type = 5 AND A.date<=num2.last_dates)
FROM
(SELECT last_dates
FROM (
SELECT LAST_DAY(CURDATE() - INTERVAL CUSTOM_MONTH MONTH) last_dates
FROM(
SELECT 1 CUSTOM_MONTH UNION
SELECT 0 UNION
SELECT 2 UNION
SELECT 3 UNION
SELECT 4 UNION
SELECT 5 UNION
SELECT 6 UNION
SELECT 7 UNION
SELECT 8 UNION
SELECT 9 UNION
SELECT 10 UNION
SELECT 11 UNION
SELECT 12 )num
) num1
)num2
ORDER BY num2.last_dates
This gives me the result like this which is exactly how i need it. I need this query to execute faster. Is there any better way to do what i am trying to do?
2019-05-31 33488.69 109.127800
2019-06-30 263.690 1248932.227800
2019-07-31 274.690 131.827800
2019-08-31 627.690 13.687800
2019-09-30 1533.370000 08.347800
2019-10-31 1444.370000 01.327800
2019-11-30 5448.370000 247.227800
2019-12-31 61971.370000 016.990450
2020-01-31 19550.370000 2535.185450
2020-02-29 986.370000 405.123300
2020-03-31 1152.370000 26.793300
2020-04-30 9404.370000 11894.683300
2020-05-31 3404.370000 17894.683300
I'd use conditional aggregation, and pre-aggregate the monthly totals in one pass, instead of doing twenty-six individual passes repeatedly through the same data.
I'd start with something like this:
SELECT CASE WHEN A.date < DATE(NOW()) + INTERVAL -14 MONTH
THEN LAST_DAY( DATE(NOW()) + INTERVAL -14 MONTH )
ELSE LAST_DAY( A.date )
END AS _month_end
, SUM(IF( B.type = 5 , B.amount , NULL)) AS tot_type_5
, SUM(IF( B.type = 7 , B.amount , NULL)) AS tot_type_7
FROM A
JOIN B
ON B.id = A.B_id
WHERE B.type IN (5,7)
GROUP
BY _month_end
(column amount isn't qualified in original query, so just guessing here which table that is from. adjust as necessary. best practice is to qualify all column references.
That gets us the subtotals for each month, in a single pass through A and B.
We can get that query tested and tuned.
Then we can incorporate that as an inline view in an outer query which adds up those monthly totals. (I'd do an outer join, just in case rows are missing, sow we don't wind up omitting rows.)
Something like this:
SELECT d.dt + INTERVAL -i.n MONTH + INTERVAL -1 DAY AS last_date
, SUM(IFNULL(t.tot_type_5,0)) AS rt_type_5
, SUM(IFNULL(t.tot_type_7,0)) AS rt_type_7
FROM ( -- first day of next month
SELECT DATE(NOW()) + INTERVAL -DAY(DATE(NOW()))+1 DAY + INTERVAL 1 MONTH AS dt
) d
CROSS
JOIN ( -- thirteen integers, integers 0 thru 12
SELECT 0 AS n
UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
) i
LEFT
JOIN ( -- totals by month
SELECT CASE WHEN A.date < DATE(NOW()) + INTERVAL -14 MONTH
THEN LAST_DAY( DATE(NOW()) + INTERVAL -14 MONTH )
ELSE LAST_DAY( A.date )
END AS _month_end
, SUM(IF( B.type = 5 , B.amount , NULL)) AS tot_type_5
, SUM(IF( B.type = 7 , B.amount , NULL)) AS tot_type_7
FROM A
JOIN B
ON B.id = A.B_id
WHERE B.type IN (5,7)
GROUP
BY _month_end
) t
ON t._month_end < d.dt
GROUP BY d.dt + INTERVAL -i.n MONTH + INTERVAL -1 DAY
ORDER BY d.dt + INTERVAL -i.n MONTH + INTERVAL -1 DAY DESC
The design is meant to do one swoop through the A JOIN B set. We're expecting to get about 14 rows back. And we're doing a semi-join, duplicating the oldest months multiple times, so approx . 14 x 13 / 2 = 91 rows, that get collapsed into 13 rows.
The big rock in terms of performance is going to be materializing that inline view query.
This is how I'd probably approach this in MySQL 8 with SUM OVER:
Get the last 12 months.
Use these months to add empty month rows to the original data, as MySQL doesn't support full outer joins.
Get the running totals for all months.
Show only the last twelve months.
The query:
with months (date) as
(
select last_day(current_date - interval 1 month) union all
select last_day(current_date - interval 2 month) union all
select last_day(current_date - interval 3 month) union all
select last_day(current_date - interval 4 month) union all
select last_day(current_date - interval 5 month) union all
select last_day(current_date - interval 6 month) union all
select last_day(current_date - interval 7 month) union all
select last_day(current_date - interval 8 month) union all
select last_day(current_date - interval 9 month) union all
select last_day(current_date - interval 10 month) union all
select last_day(current_date - interval 11 month) union all
select last_day(current_date - interval 12 month)
)
, data (date, amount, type) as
(
select last_day(a.date), a.amount, b.type
from a
join b on b.id = a.b_id
where b.type in (5, 7)
union all
select date, null, null from months
)
select
date,
sum(sum(case when type = 5 then amount end)) over (order by date) as t5,
sum(sum(case when type = 7 then amount end)) over (order by date) as t7
from data
group by date
order by date
limit 12;
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=ddeb3ab3e086bfc182f0503615fba74b
I don't know whether this is faster than your own query or not. Just give it a try. (You'd get my query much faster by adding a generated column for last_day(date) to your table and use this. If you need this often, this may be an option.)
You are getting some complicated answers. I think it is easier. Start with knowing we can easily sum for each month:
SELECT SUM(amount) as monthtotal,
type,
MONTH(date) as month,
YEAR(date) as year
FROM A LEFT JOIN B on A.B_id=B.id
GROUP BY type,month,year
From that data, we can use a variable to get running total. Best to do by initializing the variable, but not necessary. We can get the data necessary like this
SET #running := 0;
SELECT (#running := #running + monthtotal) as running, type, LAST_DAY(CONCAT(year,'-',month,'-',1))
FROM
(SELECT SUM(amount) as monthtotal,type,MONTH(date) as month,YEAR(date) as year FROM A LEFT JOIN B on A.B_id=B.id GROUP BY type,month,year) AS totals
ORDER BY year,month
You really need to have a connector that supports multiple statements, or make multiple calls to initialize the variable. Although you can null check the variable and default to 0, you still have an issue if you run the query a second time.
Last thing, if you really want the types to be summed separately:
SET #running5 := 0;
SET #running7 := 0;
SELECT
LAST_DAY(CONCAT(year,'-',month,'-',1)),
(#running5 := #running5 + (CASE WHEN type=5 THEN monthtotal ELSE 0 END)) as running5,
(#running7 := #running7 + (CASE WHEN type=7 THEN monthtotal ELSE 0 END)) as running7
FROM
(SELECT SUM(amount) as monthtotal,type,MONTH(date) as month,YEAR(date) as year FROM A LEFT JOIN B on A.B_id=B.id GROUP BY type,month,year) AS totals
ORDER BY year,month
We still don't show months where there is no data. I'm not sure that is a requirement. But this should only need one pass of table A.
Also, make sure the id on table B is indexed.
First question on here, so i will try my best to be clear.
I have 2 tables:
"TABLE1" which contains a record for each stock code and a list of attributes.
In TABLE 1 there is just one record for each stock_code
"TABLE2" which contains a log of changes to attributes of products, over time.
"TABLE2" contains the following fields:.
stock_code
stock_attribute
old_value
new_value
change_date
change_time
TABLE 2 has multiple entries ofr each stock_code.
Every time a stock item is change, another entry is made in Table2, with the attribute that has changed, the change date, time, old value and new value.
I want to create a query which will result in a table that has one record for each stock_code (from TABLE 1), and a column for each week over past year, with the value in each field being the last recorded "new_val" for that week (From TABLE 2)
I have tried
SELECT a.`stcode`, b.`week1`, b.`week2`, b.`week3`, b.`week4` etc. etc.
from (SELECT stcode, )as a
LEFT JOIN (SELECT stcode,
(CASE WHEN chngdate BETWEEN DATE_SUB(CURDATE(),INTERVAL 363 DAY) AND DATE_SUB(CURDATE(),INTERVAL 357 DAY) THEN newval END)week1,
(CASE WHEN chngdate BETWEEN DATE_SUB(CURDATE(),INTERVAL 356 DAY) AND DATE_SUB(CURDATE(),INTERVAL 350 DAY) THEN newval END)week2,
(CASE WHEN chngdate BETWEEN DATE_SUB(CURDATE(),INTERVAL 349 DAY) AND DATE_SUB(CURDATE(),INTERVAL 343 DAY) THEN newval END)week3,
(CASE WHEN chngdate BETWEEN DATE_SUB(CURDATE(),INTERVAL 342 DAY) AND DATE_SUB(CURDATE(),INTERVAL 336 DAY) THEN newval END)week4,
(etc
etc
etc
FROM (SELECT * from TABLE 2 ORDER BY "chngdate" DESC, "chngtime" DESC )as sub) as b ON b.stcode = s.stcode
ORDER BY stcode ASC
The trouble is with this, i get multiple lines for a stock_code which has mutliple entries....
for example, for stock_code abc123 the result i get is
STCODE WEEK1 WEEK2 WEEK3 WEEK4 WEEK5 WEEK6
abc123 null null 4 null null null
abc123 2 null null null null null
abc123 null null null null 3 null
what i WANT is this:
STCODE WEEK1 WEEK2 WEEK3 WEEK4 WEEK5 WEEK6
abc123 2 null 4 null 3 null
I have also tried the following, but teh query took so long, it never finished (there were 52 derived tables!)
SELECT a.`stcode`, w1.`new_value`, w2.`new_value`, w3.`new_value`, w4.`new_value` etc. etc.
from (SELECT stcode, )as a
LEFT JOIN (SELECT stcode,
LEFT JOIN (SELECT stcode, depot, fieldname, chngdate, chngtime, newval from STDepotAmendmentsLog WHERE chngdate BETWEEN DATE_SUB(CURDATE(),INTERVAL 363 DAY) AND DATE_SUB(CURDATE(),INTERVAL 357 DAY) ORDER BY "chngdate" DESC, "chngtime" DESC)as w1 on s.stcode = w1.stcode
etc for week 2, 3, 4 etc etc
You could do the following:
Find the greatest date for each "week"
Find the rows corresponding to those dates
Use conditional aggregation to convert rows into columns
Here is a rough outline of the code. It assumes that e.g. if today is 2020-03-03 then week 52 is from 2020-02-26 to 2020-03-03. Adjust if necessary:
SELECT t.stock_code
, MAX(CASE WHEN weeknum = 51 THEN new_value END) AS week01
, MAX(CASE WHEN weeknum = 50 THEN new_value END) AS week02
, MAX(CASE WHEN weeknum = 1 THEN new_value END) AS week51
, MAX(CASE WHEN weeknum = 0 THEN new_value END) AS week52
FROM table2 AS t
JOIN (
SELECT stock_code
, DATEDIFF(CURRENT_DATE, change_date) div 7 AS weeknum -- count multiples of 7
, MAX(change_date) AS greatest_date
GROUP BY stock_code, weeknum
FROM table2
) AS a ON t.stock_code = a.stock_code AND t.change_date = a.greatest_date
GROUP BY t.stock_code
I have 2 tables with the following data
inviteTable
inviteDate | contractAmount | status
2014-01-01 1500 awarded
2015-01-01 2000 awarded
2015-01-02 4000 closed
2015-02-01 6000 awarded
2015-02-02 8000 awarded
2015-03-01 8500 awarded
quoteTable
quoteDate | quoteAmount | status
2014-03-01 1500 awarded
2015-01-02 2000 awarded
2015-01-03 4000 sent
2015-01-04 6000 awarded
2015-02-03 8000 awarded
2015-02-10 8500 sent
2015-02-11 9000 awarded
2015-03-01 9500 awarded
I want to make ONE query that gives me the following data structure
month | quotedTotal | quotedCount | awardedTotal | awardedCount
January 8000 2 2000 1
February 17000 2 14000 2
March 9500 1 8500 1
currently i have written this code...but it doesn't work
SELECT
MONTHNAME(t1.due_date) as month,
(select sum(`amount`) from quoteTable where awarded= 1 ) as estimatedAmount,
sum(t1.contactAmount) AS sumContactAmount,
COUNT(*) as `noOfAwarded`
FROM inviteTable t1
where
t1.status = "Awarded" and
t1.inviteDate between '2014-11-01' and '2015-10-31'
GROUP BY MONTH(due_date)
;
basically I want to have ONE query that gives me the following:
volume that got awarded - sum of contractAmount and status awarded
count of how many awarded - count of inviteTable.status=awarded
volume that got quoted - sum of quoteAmount and status awarded
count of how many quoted - count of quotedTable.status=awarded
all grouped by month and in a date range
my query doesn't work. Your help would be highly appreciated as I am stuck!
Your schema is unfortunate; it would be easier if you have just one table with an extra column to record what type of record you have. However, if you first collect the two tables into one table via union all, you can use a fairly simple query to produce the results to want:
select
monthname(_date) month,
sum(quoteAwarded * amount) quotedTotal,
sum(quoteAwarded) quotedCount,
sum(contractAwarded * amount) awardedTotal,
sum(contractAwarded) awardedCount
from (select inviteDate _date, contractAmount amount, status = 'awarded' contractAwarded, 0 quoteAwarded from inviteTable
union all
select quoteDate, quoteAmount, 0, status = 'awarded' from quoteTable) x
where _date between '2014-11-01' and '2015-10-31'
group by 1
order by month(_date)
See SQLFiddle using your sample data, and producing you expected output.
At the core of this query is the convenient fact that (in mysql) "true" is 1 and "false" is 0 - allowing the use of sum() instead of count(), and more importantly avoiding the use of case by using sum() over some simple math.
Here is a way you can do it, note that you have dates in both tables and you need to use join preferably left join , so this will ensure that the dates present in the left table is always returned in this case the month name.
select
t1.month,
t1.awardedTotal,
t1.awardedCount,
t2.quotedTotal,
t2.quotedCount
from(
select
monthname(t1.inviteDate) as month,
month(t1.inviteDate) as m,
sum(
case
when t1.status = 'awarded' then t1.contractAmount else 0
end
) as awardedTotal,
sum(
case
when t1.status = 'awarded' then 1 else 0
end
) as awardedCount
from inviteTable t1
where t1.inviteDate between '2014-11-01' and '2015-10-31'
group by month
)t1
left join(
select
monthname(t2.quoteDate) as month,
sum(
case
when t2.status = 'awarded' then t2.quoteAmount else 0
end
) as quotedTotal,
sum(
case
when t2.status = 'awarded' then 1 else 0
end
) as quotedCount
from quoteTable t2
where t2.quoteDate between '2014-11-01' and '2015-10-31'
group by month
)t2 on
t1.month = t2.month
order by t1.m
http://sqlfiddle.com/#!9/a863a/10
UPDATE:
Yes since you want dates present in both tables it can not be done with the above process since it always consider one table as left table and will get all the dates from that table. For getting data for all the dates across both tables you first need to get the dates from both tables and then using left join get the calculation part something as
select
t1.month,
coalesce(t2.awardedTotal,0) as awardedTotal,
coalesce(t2.awardedCount,0) as awardedCount,
coalesce(t3.quotedTotal,0) as quotedTotal,
coalesce(t3.quotedCount,0) as quotedCount
from(
select month(inviteDate) as m,monthname(inviteDate) as month
from inviteTable where inviteDate between '2014-11-01' and '2015-10-31'
union
select month(quoteDate) as m,monthname(quoteDate) as month
from quoteTable where quoteDate between '2014-11-01' and '2015-10-31'
)t1
left join(
select
monthname(inviteDate) as month,
sum(
case
when status = 'awarded' then contractAmount else 0
end
) as awardedTotal,
sum(
case
when status = 'awarded' then 1 else 0
end
) as awardedCount
from inviteTable
group by month
)t2 on t1.month = t2.month
left join(
select
monthname(quoteDate) as month,
sum(
case
when status = 'awarded' then quoteAmount else 0
end
) as quotedTotal,
sum(
case
when status = 'awarded' then 1 else 0
end
) as quotedCount
from quoteTable
group by month
)t3 on t1.month=t3.month
order by t1.m
http://sqlfiddle.com/#!9/ddaac/14
I have a table with the following data.
serial date flag
1 1 aug 1
2 2 aug 1
3 3 aug 0
4 4 aug 0
5 5 aug 1
6 6 aug 1
7 7 aug 1
What I need is this result set:
from to flag
1-aug 2-aug 1
3-Aug 4-Aug 0
5-Aug 7-Aug 1
When I group on flag all 1 values are combined. Any suggestions?
an old question, i know, but this may still help someone...
to solve this, i have assumed that the date will be sequential. i have then joined the data, first with 'yesterday's' data and then with 'tomorrow's' data and taken those rows where the flag value has changed - this marks the start & end dates of a given series. to match the start dates with the end dates, i've ordered each series, assigned a counter and joined on this counter.
here's the code:
SET #cnt1 := 0;
SET #cnt2 := 0;
select firstdate, lastdate, startDates.flag from
(
select #cnt2 := #cnt2 + 1 as i, table1.date as firstdate, table1.flag from table1
left join (
select DATE_ADD(date, INTERVAL 1 DAY) as dateplus1, date, flag from table1
) plus1 on table1.date = plus1.dateplus1
where table1.flag <> IFNull(plus1.flag,-1)
order by table1.date
) startDates
inner join
(
select #cnt1 := #cnt1 + 1 as i, table1.date as lastdate, table1.flag from table1
left join (
select DATE_ADD(date, INTERVAL -1 DAY) as dateminus1, date, flag from table1
) minus1 on table1.date = minus1.dateminus1
where table1.flag <> IFNull(minus1.flag,-1)
order by table1.date
) endDates on startDates.i = endDates.i
;
sql fiddle here: http://sqlfiddle.com/#!2/25594/1/2