coverting column to row using my sql - mysql

i have following mysql query:
SELECT
a.id, a.sbu_name, b.month,b.week, b.DateRange, b.planned
FROM
(SELECT
id, sbu_name
FROM
tbl_sbu) a
LEFT JOIN
(SELECT
sbu_id,
month,
WEEK(month) week,
CONCAT(DATE_FORMAT(DATE_ADD(month, INTERVAL (1 - DAYOFWEEK(month)) DAY), '%Y-%m-%e'), ' TO ', DATE_FORMAT(DATE_ADD(month, INTERVAL (7 - DAYOFWEEK(month)) DAY), '%Y-%m-%e')) AS DateRange,
SUM(amount) / 100000 AS planned
FROM
tbl_collection_planed
WHERE
DATE_FORMAT(month, '%Y-%m') = '2016-08'
GROUP BY DateRange) b ON b.sbu_id = a.id order by week asc
The output of above query is:
I want my output as follows:

This is a table pivot question, if just for this case, you can try following query;)
SELECT
sbu_name,
IF(DateRange = '2016-08-7 TO 2016-08-13', planned, '') AS `2016-08-7 TO 2016-08-13`,
IF(DateRange = '2016-08-14 TO 2016-08-20', planned, '') AS `2016-08-14 TO 2016-08-20`,
IF(DateRange = '2016-08-21 TO 2016-08-27', planned, '') AS `2016-08-21 TO 2016-08-27`,
IF(DateRange = '2016-08-28 TO 2016-09-3', planned, '') AS `2016-08-28 TO 2016-09-3`
FROM (
SELECT
a.id, a.sbu_name, b.month,b.week, b.DateRange, b.planned
FROM
(SELECT
id, sbu_name
FROM
tbl_sbu) a
LEFT JOIN
(SELECT
sbu_id,
month,
WEEK(month) week,
CONCAT(DATE_FORMAT(DATE_ADD(month, INTERVAL (1 - DAYOFWEEK(month)) DAY), '%Y-%m-%e'), ' TO ', DATE_FORMAT(DATE_ADD(month, INTERVAL (7 - DAYOFWEEK(month)) DAY), '%Y-%m-%e')) AS DateRange,
SUM(amount) / 100000 AS planned
FROM
tbl_collection_planed
WHERE
DATE_FORMAT(month, '%Y-%m') = '2016-08'
GROUP BY DateRange) b ON b.sbu_id = a.id order by week asc
) t
GROUP BY sub_name
If this DateRange has dynamic values, you have to do this with a dynamic sql.
EDITED(dynamic sql)
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(DateRange = ''',
CONCAT(DATE_FORMAT(DATE_ADD(month, INTERVAL (1 - DAYOFWEEK(month)) DAY), '%Y-%m-%e'), ' TO ', DATE_FORMAT(DATE_ADD(month, INTERVAL (7 - DAYOFWEEK(month)) DAY), '%Y-%m-%e')),
''', planned, '')) AS `',
CONCAT(DATE_FORMAT(DATE_ADD(month, INTERVAL (1 - DAYOFWEEK(month)) DAY), '%Y-%m-%e'), ' TO ', DATE_FORMAT(DATE_ADD(month, INTERVAL (7 - DAYOFWEEK(month)) DAY), '%Y-%m-%e')), '`'
)
) INTO #sql
FROM tbl_collection_planed
WHERE DATE_FORMAT(month, '%Y-%m') = '2016-08'
;
SET #sql = CONCAT('SELECT sbu_name, ', #sql, ' FROM (
SELECT
a.id, a.sbu_name, b.month,b.week, b.DateRange, b.planned
FROM
(SELECT
id, sbu_name
FROM
tbl_sbu) a
LEFT JOIN
(SELECT
sbu_id,
month,
WEEK(month) week,
CONCAT(DATE_FORMAT(DATE_ADD(month, INTERVAL (1 - DAYOFWEEK(month)) DAY), \'%Y-%m-%e\'), \' TO \', DATE_FORMAT(DATE_ADD(month, INTERVAL (7 - DAYOFWEEK(month)) DAY), \'%Y-%m-%e\')) AS DateRange,
SUM(amount) / 100000 AS planned
FROM
tbl_collection_planed
WHERE
DATE_FORMAT(month, \'%Y-%m\') = \'2016-08\'
GROUP BY DateRange) b ON b.sbu_id = a.id order by week asc
) t
GROUP BY sub_name');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Try this
**select * from (**
SELECT
a.id, a.sbu_name, b.month,b.week, b.DateRange, b.planned
FROM
(SELECT
id, sbu_name
FROM
tbl_sbu) a
LEFT JOIN
(SELECT
sbu_id,
month,
WEEK(month) week,
CONCAT(DATE_FORMAT(DATE_ADD(month, INTERVAL (1 - DAYOFWEEK(month)) DAY), '%Y-%m-%e'), ' TO ', DATE_FORMAT(DATE_ADD(month, INTERVAL (7 - DAYOFWEEK(month)) DAY), '%Y-%m-%e')) AS DateRange,
SUM(amount) / 100000 AS planned
FROM
tbl_collection_planed
WHERE
DATE_FORMAT(month, '%Y-%m') = '2016-08'
GROUP BY DateRange) b ON b.sbu_id = a.id **) as b group by DateRange**

Related

QOQ growth Mysql

i have query
select a.`2021`,
b.`2022`,
a.product,
concat(ceil((b.`2022`-a.`2021`)/ a.`2021` * 100), '%') as growth
from ( SELECT SUM(total) as `2021`,
product,
sum
FROM table
WHERE YEAR(month) = 2021
AND case when day(CURRENT_DATE()) > 10
then QUARTER(month) = QUARTER(CURRENT_DATE() - INTERVAL 1 MONTH)
else QUARTER(month) = QUARTER(CURRENT_DATE() - INTERVAL 3 MONTH)
end
GROUP BY Product ,
YEAR(month) )a
JOIN ( SELECT SUM(total) as `2022`,
Product
FROM table
WHERE YEAR(month) = 2022
AND case when day(CURRENT_DATE()) > 10
then QUARTER(month) = QUARTER(CURRENT_DATE() - INTERVAL 1 MONTH)
else QUARTER(month) = QUARTER(CURRENT_DATE() - INTERVAL 3 MONTH)
end
GROUP BY Product ,
YEAR(month) ) b on a.Product=b.Product;
if the current date is not the end of the quarter then the data that appears is the data in the previous quarter period

Creating a table of workers with first shift last week

I am trying to extract a list of workers who had their first shift during last week.
Right now, my SQL works if I put a specific worker with a first shift in last week in my where clause (WHERE c.temp_nr ='XXXX'), but when I remove that clause, it just timeout
SELECT MIN(p.start_date), CONCAT(c.first_name, ' ', c.last_name) AS 'c.fullname', c.temp_nr
FROM contactstable c
LEFT JOIN projectlines p on c.temp_nr = p.candidate_number
WHERE c.temp_nr ='XXXX' and c.contact_type = 'Candidate'
HAVING MIN(p.start_date) between DATE_SUB( CURDATE( ) , INTERVAL (dayofweek(CURDATE())+6)
DAY )
AND DATE_SUB( CURDATE( ) , INTERVAL (dayofweek(CURDATE()))
DAY )
Use GROUP BY c.temp_nr
SELECT MIN(p.start_date), CONCAT(c.first_name, ' ', c.last_name) AS 'c.fullname', c.temp_nr
FROM contactstable c
LEFT JOIN projectlines p on c.temp_nr = p.candidate_number
WHERE c.contact_type = 'Candidate'
GROUP BY c.temp_nr
HAVING
MIN(p.start_date)
between
DATE_SUB(CURDATE(), INTERVAL (dayofweek(CURDATE())+6) DAY)
AND DATE_SUB(CURDATE() , INTERVAL (dayofweek(CURDATE())) DAY)

count not working inside group_concat Mysql

I am trying to concat count of id and 1 inside group_concat. Below is my query.
MYSQL:
SELECT
months.name AS NAME,
CONCAT(
'[',
GROUP_CONCAT(
CONCAT(
'[\"',
COUNT(p_c_n_details.JPN_ID),
'\",',
1,
']'
)
),
']'
) AS DATA
FROM
p_c_n_details
INNER JOIN in_e_s_s__p_c_ns RIGHT OUTER JOIN months ON months.id =
MONTH(p_c_n_details.created_at) AND p_c_n_details.type IN('Process Change',
'Design Change') AND p_c_n_details.JPN_ID =
in_e_s_s__p_c_ns.juniperinternalpcnid AND p_c_n_details.created_at >=
last_day(NOW()) + INTERVAL 1 DAY - INTERVAL 3 MONTH
WHERE
months.name IN(
MONTHNAME(
DATE_SUB(CURDATE(), INTERVAL 0 MONTH)),
MONTHNAME(
DATE_SUB(CURDATE(), INTERVAL 1 MONTH)),
MONTHNAME(
DATE_SUB(CURDATE(), INTERVAL 2 MONTH))
)
GROUP BY
months.id
Expected Output:
Name | DATA
-------------------------
July [20,1]
August [33,1]
Table months contains month names.
But I am getting error #1111 - Invalid use of group function. I tried this link
mysql group_concat with a count inside?
but I am facing error when using in my query.
This is a guess.
Try:
SELECT months.name AS NAME,
CONCAT('{',COUNT(p_c_n_details.JPN_ID),',1}' AS DATA
in place of the SELECT you now have.
Your example result doesn't show a need for GROUP_CONCAT().

How to select same column of a table twice as two different columns seperated date wise

i have the following mysql query , but its taking approx 5 minutes to execute, how can i decrease its execution time
select
revenue_center_group.description as rc_group,
revenue_center.description as revenue_center,
count(ytd.members) as total_ytd_members,
count(lytd.members) as total_lytd_members
from revenue_center
left join revenue_center_group on revenue_center_group.id = revenue_center.revenue_center_group_id
left join (
select membership.id as members,
membership.rc_id as rc_id
from membership
where membership.status =1
and DATE(membership.join_date)>= (SELECT DATE_FORMAT((SELECT DATE_FORMAT(date(now()) - INTERVAL 1 MONTH, '%Y-%m-%d')) ,'%Y-01-01 00:00:00'))
and DATE(membership.join_date)<= (SELECT DATE_FORMAT(LAST_DAY(date(now()) - INTERVAL 1 MONTH), '%Y-%m-%d 23:59:59'))ytd on revenue_center.id = ytd.rc_id
left join (
select
membership.id as members,
membership.rc_id as rc_id
from membership
where membership.status =1
and DATE(membership.join_date)>= (SELECT DATE_FORMAT((SELECT DATE_FORMAT((SELECT DATE_FORMAT(date(now()) - INTERVAL 1 MONTH, '%Y-%m-%d'))
- INTERVAL 1 YEAR, '%Y-%m-%d')) ,'%Y-01-01 00:00:00'))
and DATE(membership.join_date)<= (SELECT DATE_FORMAT((select DATE_FORMAT(LAST_DAY(date(now()) - INTERVAL 1 MONTH), '%Y-%m-%d')
- INTERVAL 1 YEAR),'%Y-%m-%d 23:59:59'))lytd on revenue_center.id = lytd.rc_id
group by revenue_center_group.description,revenue_center.description with rollup;

mysql, transpose/pivot row to column, variable selects

Hello again and thank you in advance for your help.
I've checked a few prior questions and couldn't find this exact situation.
I'm trying to transpose/pivot a row to column, but the results are based on a date function in the where clause, making my selects somewhat variable.
SELECT
DATE_FORMAT(packet_details.installDate,'%m-%d-%Y') as Install_Date,
Count(packet_details.installDate)
FROM
packet_details
WHERE
packet_details.installDate >= CURRENT_DATE - INTERVAL 7 DAY
AND packet_details.installDate "*lessthan*" CURRENT_DATE + INTERVAL 7 DAY
GROUP BY installDate
*lessthan symbol wont show here on Stack & i don't know how to fix it
Not sure if that makes sense so I included a fiddle: http://sqlfiddle.com/#!2/5b235/3/0
So something like this:
INSTALL_DATE COUNT
1/24/2013 2
1/25/2013 2
1/26/2013 2
1/27/2013 2
1/28/2013 2
1/29/2013 1
2/3/2013 1
2/4/2013 1
2/5/2013 5
2/6/2013 4
Turned into:
INSTALL_DATE 1/24/2013 1/25/2013 1/26/2013 1/27/2013 1/28/2013....
COUNT 2 2 2 2 2 1
SELECT Install_DATE,
MAX(CASE WHEN Install_DATE = '01-24-2013' THEN totalCount END) `01-24-2013`,
MAX(CASE WHEN Install_DATE = '01-25-2013' THEN totalCount END) `01-25-2013`,
MAX(CASE WHEN Install_DATE = '01-26-2013' THEN totalCount END) `01-26-2013`,
.......
FROM
(
SELECT DATE_FORMAT(packet_details.installDate,'%m-%d-%Y') as Install_Date,
Count(packet_details.installDate) totalCount
FROM packet_details
WHERE packet_details.installDate >= CURRENT_DATE - INTERVAL 7 DAY AND
packet_details.installDate < CURRENT_DATE + INTERVAL 7 DAY
GROUP BY installDate
) s
SQLFiddle Demo
For unknown number of Install_Date, a Dynamic Query is much preferred,
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(CASE WHEN Install_DATE = ''',
Install_Date,
''' then totalCount end) AS `', Install_Date, '`' )
) INTO #sql
FROM
(
SELECT DATE_FORMAT(packet_details.installDate,'%m-%d-%Y') as Install_Date,
Count(packet_details.installDate) totalCount
FROM packet_details
WHERE packet_details.installDate >= CURRENT_DATE - INTERVAL 7 DAY AND
packet_details.installDate < CURRENT_DATE + INTERVAL 7 DAY
GROUP BY installDate
) s;
SET #sql = CONCAT('SELECT Install_DATE, ', #sql, '
FROM
(
SELECT DATE_FORMAT(packet_details.installDate,''%m-%d-%Y'') as Install_Date,
Count(packet_details.installDate) totalCount
FROM packet_details
WHERE packet_details.installDate >= CURRENT_DATE - INTERVAL 7 DAY AND
packet_details.installDate < CURRENT_DATE + INTERVAL 7 DAY
GROUP BY installDate
) s');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SQLFiddle Demo