How to concatenate column values aggregated out of a group by? - mysql

I have this MySQL query that works almost perfectly:
select ConfirmationNumber, ReservationDate, ifnull(CASE TableNumber WHEN 0 THEN 'UNASSIGNED' ELSE TableNumber END,'UNASSIGNED') AS 'Table', CONCAT(LastName, ', ', FirstName) AS 'Customer', Email, Phone, PublicNotes, sum(SleighSeats) + sum(CabSeats) AS Seats
from Reservations
where ReservationDate = '2018-1-25'
and ConfirmationNumber IS NOT NULL
and CancelDate IS NULL
group by TableNumber, Customer
order by TableNumber
It produces this result:
Focus on customer Corbosie... above.
Because there is grouping going on, there are PublicNotes data that I'm missing, because some records are being compressed out of the query. I simply want to concatenate the PublicNotes from all records included in the query, including those being aggregated out.
For example, when I query this way:
select ConfirmationNumber, ReservationDate, ifnull(CASE TableNumber WHEN 0 THEN 'UNASSIGNED' ELSE TableNumber END,'UNASSIGNED') AS 'Table', CONCAT(LastName, ', ', FirstName) AS 'Customer', Email, Phone, PublicNotes, SleighSeats + CabSeats AS Seats
from Reservations
where ConfirmationNumber in ('092550', '764352', '661800')
...it shows you 3 records that got compressed into 1 record (due to grouping) in the first query:
The 2 PublicNotes circled in red are missing because they got aggregated out. How can I maintain the grouping of my first query, while concatenating in the missing PublicNotes with their grouped record?

I think you want group_concat():
select ConfirmationNumber, ReservationDate,
(CASE TableNumber WHEN 0 THEN 'UNASSIGNED' ELSE TableNumber END) AS "Table",
CONCAT(LastName, ', ', FirstName) AS Customer,
Email, Phone,
GROUP_CONCAT(PublicNotes, '|'),
(SUM(SleighSeats) + SUM(CabSeats)) AS Seats
from Reservations
where ReservationDate = '2018-1-25' and
ConfirmationNumber IS NOT NULL and
CancelDate IS NULL
group by ConfirmationNumber, ReservationDate, TableNumber, Customer, email, phone
order by TableNumber;
Notes:
I don't think TableNumber can be NULL, based on the logic, so I removed the IFNULL(). If it can be, then add it back in (or use COALESCE().
I added the additional unaggregated columns to the GROUP BY. This is a good habit for you to use.
The separator for the different notes is a vertical bar. You can choose whatever you like.

Related

Calculating consecutive occurences in MySQL

I have a quick question in relation to windowing in MySQL
SELECT
Client,
User,
Date,
Flag,
lag(Date) over (partition by Client,User order by Date asc) as last_date,
lag(Flag) over (partition by Client,User order by Date asc) as last_flag,
case when Flag = 1 and last_flag = 1 then 1 else 0 end as consecutive
FROM db.tbl
This query returns something like the below. I am trying to work out the number of consecutive times that the Flag column was 1 for each user most recently, if they had 11110000111 then we should take the final three occurences of 1 to determine that they had a consecutive flag of 3 times.
I need to extract the start and end date for the consecutive flag.
How would I go about doing this, can anyone help me :)
If we use the example of 11110000111 then we should extract only 111 and therefore the 3 most recent dates for that customer. So in the below, we would need to take 10.01.2023 as the first date and 24.01.2023 as the last date. The consecutive count should be 3
Output:
Use aggregation and string functions:
WITH cte AS (
SELECT Client, User,
GROUP_CONCAT(CASE WHEN Flag THEN Date END ORDER BY Date) AS dates,
CHAR_LENGTH(SUBSTRING_INDEX(GROUP_CONCAT(Flag ORDER BY Date SEPARATOR ''), '0', '-1')) AS consecutive
FROM tablename
GROUP BY Client, User
)
SELECT Client, User,
NULLIF(SUBSTRING_INDEX(SUBSTRING_INDEX(dates, ',', -consecutive), ',', 1), '') AS first_date,
CASE WHEN consecutive > 0 THEN SUBSTRING_INDEX(dates, ',', -1) END AS last_date,
consecutive
FROM cte;
Another solution with window functions and conditional aggregation:
WITH
cte1 AS (SELECT *, SUM(NOT Flag) OVER (PARTITION BY Client, User ORDER BY Date) AS grp FROM tablename),
cte2 AS (SELECT *, MAX(grp) OVER (PARTITION BY Client, User) AS max_grp FROM cte1)
SELECT Client, User,
MIN(CASE WHEN Flag THEN Date END) AS first_date,
MAX(CASE WHEN Flag THEN Date END) AS last_date,
SUM(Flag) AS consecutive
FROM cte2
WHERE grp = max_grp
GROUP BY Client, User;
See the demo.
Made an attempt to get the result with more simpler queries and here is my approach taking advantage of lastDate and lastFlag column too.
Run here
WITH eTT
AS
( SELECT Client, User, NULLIF(MAX(Date),
(SELECT MAX(Date) FROM tt t2 WHERE t1.Client=t2.Client AND t1.User=t2.User)) as endDate
FROM tt t1 WHERE LastFlag=0 OR LastFlag IS NULL GROUP BY Client, User
)
SELECT Client, User,
(CASE WHEN MAX(endDate) IS NULL THEN NULL ELSE MIN(Date) END) as first_date,
(CASE WHEN MAX(endDate) IS NULL THEN NULL ELSE MAX(Date) END) as last_date,
(CASE WHEN MAX(endDate) IS NULL THEN NULL ELSE COUNT(endDate) END) as consecutive
FROM tt LEFT JOIN eTT USING (Client, User)
WHERE Date >= endDate OR endDate IS null GROUP BY Client, User;
EDIT
The original table doesn't have LastDate and LastFlag columns and were created using OP's initial query.
Since the method used is not apparantly supported but I get an impression that OP somehow manages to do that on their side.
Hence another cte called tt can be added before eTT containing that query.

Divide one table by another table created in the same query

I have the following SQL query:
SELECT `NeighbourhoodName`,
count(NAME) as `Number of Parks`,
sum(CASE
WHEN `parks`.`Advisories` = 'Y' THEN 1
ELSE 0
END) as Advisories,
FROM parks
GROUP BY `NeighbourhoodName`;
In the second line of the code, I create a column called "Number of Parks". I would like all the values in the next column (Advisories) to be divided by the values in "Number of parks". However, when I try to insert the division statement after the column like this:
SELECT `NeighbourhoodName`,
count(NAME) as `Number of Parks`,
sum(CASE
WHEN `parks`.`Advisories` = 'Y' THEN 1
ELSE 0
END)/`Number of Parks` as Advisories
FROM parks
GROUP BY `NeighbourhoodName`;
I get the following error:
Unknown column, `Number of Parks` in field list.
How can I perform this division while still keeping it in one query?
You need to use an outer query to do the division.
SELECT `NeighbourhoodName`,
`Number of Parks`,
Advisories/`Number of Parks` as Advisories
FROM ( SELECT `NeighbourhoodName`,
count(NAME) as `Number of Parks`,
sum( CASE WHEN `parks`.`Advisories` = 'Y' THEN 1 ELSE 0 END ) as Advisories
FROM parks
GROUP BY `NeighbourhoodName`
) as tbl;
Problems with Column Aliases
An alias can be used in a query select list to give a column a
different name. You can use the alias in GROUP BY, ORDER BY, or HAVING
clauses to refer to the column.
Or use the expression of count(Name) instead of Number of Parks:
select NeighbourhoodName,
count(Name) as `Number of Parks`,
sum(case when Advisories='Y' then 1 else 0 end)
/count(Name) as Advisories
from parks
group by NeighbourhoodName;

How to fetch attendance report from attendance table through mysql query?

I create an attendances table. I insert data success fully but when fetch the data from attendances table, data not show of my needs attendance table.
in_out column keeps reference for in and out time. Value "1" is for in time and value "2" for out time.
This is my query.
SELECT t.person_id,
t.date,
Substring_index(t.in_out, '#', 1) am_in,
Substring_index(Substring_index(t.in_out, '#', 2), '#', -1) am_out
FROM (SELECT h.person_id,
h.date,
Group_concat(h.timedata ORDER BY h.in_out SEPARATOR '#') in_out
FROM attendances h
GROUP BY h.person_id,
h.date) t
When person out time dose not enter the vale of in time show in time_out column
I want to show null column instead of same repeating time.
I want this result.
You can try using CASE WHEN Expression
select t.person_id,t.date,
substring_index(t.in_out,'#',1) am_in,
case when substring_index(t.in_out,'#',1)=substring_index(substring_index(t.in_out,'#',2),'#',-1) then 'Can not scan' else substring_index(substring_index(t.in_out,'#',2),'#',-1) end as am_out
from
(
select h.person_id,h.date,group_concat(h.timedata order by h.in_out separator '#') in_out from attendances h group by h.person_id,h.date
) t

Joining column into MySQL query as a delimited list

I have a table in MySQL that looks like so:
date | name | status
03-13-2014,Bob Jones,Pending
03-13-2014,George Manuel,Pending
03-13-2014,Tom Grables,Pending
03-13-2014,Frankie Avalon,Approved
03-13-2014,Robert Garcia,Approved
03-14-2014,John Doe,Pending
03-14-2014,John Die,Approved
03-14-2014,John Zoe,Approved
What I am trying to do is grab date, name and status for all rows that say approved. However, I am also wanting to join this query in such a way that each row I grab also has a semicolon delimited list containing the names of those whose status is pending and falls on the same day as an approved. Thus, the output of my query would look something like this:
03-13-2014,Frankie Avalon,Approved,Bob Jones;George Manuel;Tom Grables
03-13-2014,Robert Garcia,Approved,Bob Jones;George Manuel;Tom Grables
Notice that I grabbed the approved requests for 03-13-2014 and added the requests whose status is pending and whose date matches the approved request as a column. I've been messing around with the join statement, and done my Google homework, but have yet to find a way to do this.
Any ideas would be greatly appreciated.
Look into group_concat()
SELECT
GROUP_CONCAT('name` SEPARATOR ',') as NameList
FROM
`tablename`
GROUP BY
`date`
I think this is the logic you want:
select `date`,
group_concat( (case when status = 'Approved' then name end) separator ';') as Approveds,
group_concat( (case when status = 'Pending' then name end) separator ';') as Pendings
from t
group by `date`;
If you really do want pending only on dates where there are no approved, then you need an additional filter:
select `date`,
group_concat( (case when status = 'Approved' then name end) separator ';') as Approveds,
group_concat( (case when status = 'Pending' then name end) separator ';') as Pendings
from t
group by `date`
having `date` in (select `date` from t where status = 'Approved')

PHP MySQL Group By question

I have a column inside my table: tbl_customers that distinguishes a customer record as either a LEAD or a CUS.
The column is simply: recordtype, with is a char(1). I populate it with either C, or L.
Obviously C = customer, while L = lead.
I want to run a query that groups by the day the record was created, so I have a column called: datecreated.
Here's where I get confused with the grouping.
I want to display a result (in one query) the COUNT of customers and the COUNT of leads for a particular day, or date range. I'm successful with only pulling the number for either recordtype:C or recordtype:L , but that takes 2 queries.
Here's what I have so far:
SELECT COUNT(customerid) AS `count`, datecreated
FROM `tbl_customers`
WHERE `datecreated` BETWEEN '$startdate."' AND '".$enddate."'
AND `recordtype` = 'C'
GROUP BY `datecreated` ASC
As expected, this displays 2 columns (the count of customer records and the datecreated).
Is there a way to display both in one query, while still grouping by the datecreated column?
You can do a group by with over multiple columns.
SELECT COUNT(customerid) AS `count`, datecreated, `recordtype`
FROM `tbl_customers`
WHERE `datecreated` BETWEEN '$startdate."' AND '".$enddate."'
GROUP BY `datecreated` ASC, `recordtype`
SELECT COUNT(customerid) AS `count`,
datecreated,
SUM(`recordtype` = 'C') AS CountOfC,
SUM(`recordtype` = 'L') AS CountOfL
FROM `tbl_customers`
WHERE `datecreated` BETWEEN '$startdate."' AND '".$enddate."'
GROUP BY `datecreated` ASC
See Is it possible to count two columns in the same query
There are two solutions, depending on whether you want the two counts in separate rows or in separate columns.
In separate rows:
SELECT datecreated, recordtype, COUNT(*)
FROM tbl_customers
WHERE datecreated BETWEEN '...' AND '...'
GROUP BY datecreated, recordtype
In separate colums (this is called pivoting the table)
SELECT datecreated,
SUM(recordtype = 'C') AS count_customers,
SUM(recordtype = 'L') AS count_leads
FROM tbl_customers
WHERE datecreated BETWEEN '...' AND '...'
GROUP BY datecreated
Use:
$query = sprintf("SELECT COUNT(c.customerid) AS count,
c.datecreated,
SUM(CASE WHEN c.recordtype = 'C' THEN 1 ELSE 0 END) AS CountOfC,
SUM(CASE WHEN c.recordtype = 'L' THEN 1 ELSE 0 END) AS CountOfL
FROM tbl_customers c
WHERE c.datecreated BETWEEN STR_TO_DATE('%s', '%Y-%m-%d %H:%i')
AND STR_TO_DATE('%s', '%Y-%m-%d %H:%i')
GROUP BY c.datecreated",
$startdate, $enddate);
You need to fill out the date format - see STR_TO_DATE for details.