I have table user_completed
CREATE TABLE IF NOT EXISTS `user_completed` (
`rowId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`designer_id` int(10) unsigned NOT NULL,
`status` varchar(54) DEFAULT NULL,
PRIMARY KEY (`rowId`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
INSERT INTO `user_completed` (`rowId`, `designer_id`, `status`) VALUES
(1, 1, accept),
(2, 1, reject),
(3, 1, accept),
(4, 1, reject),
(5, 1, overtime),
(6, 2, accept)
(7, 2, accept)
(8, 3, accept)
(9, 2, reject);
Which look like:
rowId designer_id status
1 1 accept
2 1 reject
3 1 accept
4 1 reject
5 1 overtime
6 2 accept
7 2 accept
8 3 accept
9 2 reject
I want to get result below:
designer_id accept overtime reject
1 2 1 2
2 2 0 1
3 1 0 0
But I have no idea how to group designer_id then count distinct status and each into columns like above.
Try this
SELECT designer_id,
SUM(IF(status = 'accept',1,0)) as 'Accept',
SUM(IF(status = 'reject',1,0)) as 'Reject',
SUM(IF(status = 'overtime',1,0)) as 'Overtime'
FROM
user_completed
Group By designer_id
Fiddle Demo
As Jack said It's simply workig with this
SELECT designer_id,
SUM(status = 'accept') as 'Accept',
SUM(status = 'reject') as 'Reject',
SUM(status = 'overtime') as 'Overtime'
FROM
user_completed
Group By designer_id
Fiddle Demo
Try this one it wil work
select designer_id,
count(case status when 'accept'then 1 else null end)as accept,
count(case status when 'reject'then 1 else null end)as reject,
count(case status when 'overtime'then 1 else null end)as overtime
from user_completed group by designer_id
If you don't know how many distinct status you have then you can check this link for solution
Mysql query to dynamically convert rows to columns
e.g you can use something like below
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(IF(`status` = "', `status`, '",1,0)) AS ', `status`)
) INTO #sql
FROM user_completed;
SET #sql = CONCAT('SELECT designer_id, ', #sql, '
FROM user_completed
GROUP BY designer_id');
SELECT #sql;
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Related
i have 3 tables like this
create table order_match
(
id int(10) PRIMARY KEY not null,
order_status_id int(10) not null
);
create table order_match_detail
(
id int(10) PRIMARY KEY not null,
order_match_id int(10) not null,
product_id int(10) NOT NULL
);
create table product
(
id int(10) PRIMARY KEY not null,
name varchar(255) not null
);
Insert into order_match (id, order_status_id)
select 1, 6 union all
select 2, 7 union all
select 3, 6 union all
select 4, 6;
Insert into order_match_detail (id, order_match_id, product_id)
select 1, 1, 147 union all
select 2, 2, 148 union all
select 3, 3, 147 union all
select 4, 4, 149 union all
select 5, 4, 147;
Insert into product (id, name)
select 147, 'orange' union all
select 148, 'carrot' union all
select 149, 'Apple';
with order_match.id = order_match_detail.order_match_id
and order_match_detail.product_id = product.id
i want to make the data where order_status_id not in 7 then it's success transaction and from that success transaction, if the transaction buy apple, then the column of apple contain 1 else if not buying, then 0 and this is my expected results, i want to make this data to analyze in
id (in order_match) | Orange | Carrot | Apple
1 1 0 0
3 1 0 0
4 1 0 1
the real problem is i've just fit in whether the version of mysql, and fit in with the same data type for every table, i dont really know why in my fiddle it's work, but on mysql it's contain
error code :1064.you have an error in your sql syntax near ' '' at
line 1
this is my Fiddle
new UPDATE
after take a look with the data, i assume the product name in field have format that cant be acceted mysql. so i've made the new query like this
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(case when name = ''',
name,
''' then 1 end) AS ',
concat('product', id)
)
) INTO #sql
from product;
SET #sql1 = CONCAT('SELECT omd.order_match_id, ', #sql, ' from order_match_detail omd
left join order_match om
on omd.order_match_id = om.id
left join product p
on omd.product_id = p.id
where om.order_status_id in (4, 5, 6, 8)
group by omd.order_match_id');
PREPARE stmt FROM #sql1;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
This is the new fiddle
but in my new mysql it's said
Error Code: 1064. You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'from order_match_detail omd left join order_match
om on omd.order_match_id = o' at line 1
this was almost same cases in this case MySQL pivot row into dynamic number of columns but the results was different and it make me confuse.
let's say i have 3 tables
create table order_match
(
id int(10) PRIMARY KEY not null,
order_status_id int(10) not null
);
create table order_match_detail
(
id int(10) PRIMARY KEY not null,
order_match_id int(10) not null,
product_id int(10) NOT NULL
);
create table product
(
id int(10) PRIMARY KEY not null,
name varchar(255) not null
);
Insert into order_match (id, order_status_id)
select 1, 6 union all
select 2, 7 union all
select 3, 6 union all
select 4, 6;
Insert into order_match_detail (id, order_match_id, product_id)
select 1, 1, 147 union all
select 2, 2, 148 union all
select 3, 3, 147 union all
select 4, 4, 149 union all
select 5, 4, 147;
Insert into product (id, name)
select 147, 'orange' union all
select 148, 'carrot' union all
select 149, 'Apple';
with order_match.id = order_match_detail.order_match_id
and order_match_detail.product_id = product.id
so like the previous case in MySQL pivot row into dynamic number of columns i want to input the product name with the transaction in order_status_id not in 7 (because 7 is expired transaction and denied)
the expected results was like this :
id (in order_match) | Orange | Carrot | Apple
1 1 0 0
3 1 0 0
4 1 0 1
based on solution in previous cases, i used this
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(case when product.name = ''',
product.name,
''' then 1 end) AS ',
replace(product.name, ' ', '')
)
) INTO #sql
from product;
SET #sql = CONCAT('SELECT omd.order_match_id, ', #sql, ' from order_match_detail omd
left join order_match om
on omd.order_match_id = om.id
left join product p
on omd.product_id = p.id
where om.order_status_id in (4, 5, 6, 8)
group by omd.order_match_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
but idk why return 0 and it's no way
this is the fiddle https://www.db-fiddle.com/f/nDe3oQ3VdtfS5QDokieHN4/6
For your GROUP_CONCAT query; in your case stmt, you are referring to your product table as product itself. But in your join query, you are referring to product table as alias p. Since the first group_concat query is a part of the join query, you need to keep the table aliases same.(made changes at line 5)
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(case when p.name = ''',
product.name,
''' then 1 end) AS ',
replace(product.name, ' ', '')
)
) INTO #pivotsql
from product;
SET #sql = CONCAT('SELECT omd.order_match_id, ', #pivotsql, ' from order_match_detail omd
left join order_match om
on omd.order_match_id = om.id
left join product p
on omd.product_id = p.id
where om.order_status_id in (4, 5, 6, 8)
group by omd.order_match_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
i have 3 tables like this
create table order_match
(
id int(10) PRIMARY KEY not null,
order_status_id int(10) not null
);
create table order_match_detail
(
id int(10) PRIMARY KEY not null,
order_match_id int(10) not null,
product_id int(10) NOT NULL
);
create table product
(
id int(10) PRIMARY KEY not null,
name varchar(255) not null
);
Insert into order_match (id, order_status_id)
select 1, 6 union all
select 2, 7 union all
select 3, 6 union all
select 4, 6;
Insert into order_match_detail (id, order_match_id, product_id)
select 1, 1, 147 union all
select 2, 2, 148 union all
select 3, 3, 147 union all
select 4, 4, 149 union all
select 5, 4, 147;
Insert into product (id, name)
select 147, 'orange' union all
select 148, 'carrot' union all
select 149, 'Apple';
with order_match.id = order_match_detail.order_match_id
and order_match_detail.product_id = product.id
i want to make the data where order_status_id not in 7 then it's success transaction and from that success transaction, if the transaction buy apple, then the column of apple contain 1 else if not buying, then 0 and this is my expected results, i want to make this data to analyze in
id (in order_match) | Orange | Carrot | Apple
1 1 0 0
3 1 0 0
4 1 0 1
with that problem i can solve it with this query
select om.id,
count(DISTINCT case when omd.product_id = 147 THEN 1 END) Orange,
count(DISTINCT case when omd.product_id = 148 THEN 1 END) Carrot,
count(DISTINCT case when omd.product_id = 149 THEN 1 END) Apple
from order_match om
left join order_match_detail omd
on om.id = omd.order_match_id
where om.order_status_id in (4, 5, 6, 8)
group by om.id
the real problem is, in my real database, it's contain 1550 product_id, how to make it automatically so no need to input manual the product_id untill 1550 product_id
this is the fiddle https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=c0eb7fe1b012ab1c909d37e325a8d434
i've tried the new queries like this and it still wrong
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(case when product.name = ''',
product.name,
''' then 1 end) AS ',
replace(product.name, ' ', '')
)
) INTO #sql
from product;
SET #sql = CONCAT('SELECT omd.order_match_id, ', #sql, ' from order_match_detail omd
left join order_match om
on omd.order_match_id = om.id
left join product p
on omd.product_id = p.id
where om.order_status_id in (4, 5, 6, 8)
group by omd.order_match_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
your query is almost correct you were missing on alias instead of p you should use 'product' in second query, it would work
SET group_concat_max_len = 1000000;
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(case when product.name = ''',
product.name,
''' then 1 end) AS ',
'"',replace(product.name, ' ', ''),'"'
)
) INTO #sql
from product;
SET #sql = CONCAT('SELECT omd.order_match_id as id, ', #sql, ' from order_match_detail omd
left join order_match om
on omd.order_match_id = om.id
left join product product
on omd.product_id = product.id
where om.order_status_id in (4, 5, 6, 8)
group by omd.order_match_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
You can remove DISTINCT from GROUP_CONCAT(DISTINCT if you are sure that product id's is having unique names for each id's
Then i would get the exact result as you mentioned in question else just you will see, Apple column first and Orange column last, overall result is as expected.
I'm trying to get a single mySQL query that returns the count of unique values, grouped by months.
I have a table created based on data similar to this:
CREATE TABLE `animals` (
`timestamp` datetime NOT NULL,
`animal` tinytext NOT NULL,
`comment` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `animals` (`timestamp`, `animal`, `comment`) VALUES
('2019-06-03 09:09:00', 'dog', 'good'),
('2019-06-03 12:00:00', 'cat', ''),
('2019-06-03 19:00:00', 'cat', ''),
('2019-07-04 09:00:00', 'cat', ''),
('2019-07-04 12:00:00', 'cat', 'feisty'),
('2019-07-04 18:51:00', 'dog', ''),
('2019-08-05 09:00:00', 'cat', ''),
('2019-08-05 12:00:00', 'cat', ''),
('2019-08-05 19:00:00', 'cat', ''),
('2019-09-06 09:00:00', 'cat', ' evil'),
('2019-09-06 12:00:00', 'cat', ''),
('2019-09-06 19:00:00', 'cat', '')
I've managed to write a query that at least gives me the count per month (as long as it is more than zero), but the query just returns the count for "cat", "dog" or anything I explicitly ask for.
My goal is to get a response similar to the following:
month | dog | cat
-------------------
2019-06 | 1 | 2
2019-07 | 1 | 2
2019-08 | 0 | 3
2019-09 | 0 | 3
How do I writhe such a query?
Is it possible to write a query that automatically counts any new values in the animal column too?
Thanks
You can use the following code, to get flexible columns from the animal column
, that does the counting for you.
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'Sum(`animal` = ''',
col,
''') as `',
col, '`')
) INTO #sql
FROM
(
select animal col
from animals
)d;
SET #sql = CONCAT('SELECT date_format(`timestamp`, "%Y-%m") `month`, ', #sql, '
from `animals`
group by `month`
order by `month`');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Se dbfiddle example https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=09d0f26087d66452fde1a22e91de7b3a
You can have
SELECT date_format(`timestamp`, '%Y-%m') AS month, animal, COUNT(*) as count
FROM animals
GROUP BY 1, 2
but this won't give you dynamically more columns. For more columns, I guess you need to build a dynamic SQL command looping over the distinct animals you have. If you really need this, you should consider to build that gives you the SQL string or directly the result.
You want conditional aggregation:
select
date_format(`timestamp`, '%Y-%m') `month`,
sum(`animal` = 'dog') dog,
sum(`animal` = 'cat') cat
from `animals`
group by `month`
order by `month`
Demo on DB Fiddle:
month | dog | cat
:------ | --: | --:
2019-06 | 1 | 2
2019-07 | 1 | 2
2019-08 | 0 | 3
2019-09 | 0 | 3
If you want to handle dynamically the column list, then you have to go for dynamic sql:
set #sql = null;
select
concat(
'select ',
'date_format(`timestamp`, ''%Y-%m'') `month`, ',
group_concat(
concat(
'sum(`animal` = ''',
`animal`,
''') ',
`animal`
)
order by `animal`
separator ', '
),
' from `animals` ',
'group by `month` '
'order by `month`'
)
into #sql
from (
select distinct `animal` from `animals`
) t;
select #sql;
prepare stmt from #sql;
execute stmt;
deallocate prepare stmt;
I'm working on an old DB and I have to create new columns based on old ones. This is a log table, each lines is an event. So uid is a User ID and a user can make several validation requests.
Here's my table
event_id uid event
1 1 REQUEST
2 1 VALIDATION
3 2 REQUEST
4 3 REQUEST
5 3 VALIDATION
6 2 VALIDATION
7 1 REQUEST
8 1 VALIDATION
Here's what I'd like to have
event_id uid event last_event_id request_nb
1 1 REQUEST 8 1
2 1 VALIDATION 8 1
3 2 REQUEST 6 1
4 3 REQUEST 5 1
5 3 VALIDATION 5 1
6 2 VALIDATION 6 1
7 1 REQUEST 8 2
8 1 VALIDATION 8 2
I assume that I need a group by on uid, a sum like 1 + SUM(CASE WHEN EVENT = 'VALIDATION' THEN 1 ELSE 0 END) AS request_nb. And a MAX somewhere for last_event_id. But I'm not familiar with these kinds of join.
Here's my sample data set:
CREATE TABLE IF NOT EXISTS `docs` (
`event_id` int(6) unsigned NOT NULL,
`uid` int(3) unsigned NOT NULL,
`event` varchar(200) NOT NULL,
PRIMARY KEY (`event_id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `docs` (`event_id`, `uid`, `event`) VALUES
('1', '1', 'REQUEST'),
('2', '1', 'VALIDATION'),
('3', '2', 'REQUEST'),
('4', '3', 'REQUEST'),
('5', '3', 'VALIDATION'),
('6', '2', 'VALIDATION'),
('7', '1', 'REQUEST'),
('8', '1', 'VALIDATION');
and fiddle of same: http://sqlfiddle.com/#!9/404dcf/1
Does anyone have an idea ?
Thanks!
E.g.:
SELECT a.event_id
, a.uid
, a.event
, a.i
, b.last_event_id
FROM
( SELECT x.*
, CASE WHEN #prev_uid = uid THEN CASE WHEN #prev_event = event THEN #i:=#i+1 ELSE #i:=1 END ELSE #i:=1 END i
, #prev_uid := uid prev_uid
, #prev_event := event prev_event FROM docs x
, (SELECT #prev_uid := null, #prev_event := null, #i:=1) vars
ORDER
BY uid
, event
, event_id
) a
JOIN
( SELECT uid, MAX(event_id) last_event_id FROM docs GROUP BY uid ) b
ON b.uid = a.uid
ORDER
BY a.event_id;
You can use ANSI-standard window functions:
select t.*,
max(event_id) over (partition by uid) as last_event_id,
row_number() over (partition by uid, event order by event_id) as request_nb
from t;