I have bee at this all day. I have played around with a lot of MySQL code and I am either inept, or not very good at coding..
I have a table
name id type amount
=================================
apple 21 cars 67
apple 21 bikes 85
apple 21 skates 557
apple 21 pajs 56
orange 34 bikes 345
orange 34 disks 678
orange 34 cars 234
orange 34 pajs 5678
I want to write a query that will bring back the table in this form
name id cars bikes skates pajas disks
=========================================
apple 21 67 85 557 56 0
orange 34 234 345 0 5678 678
I really just have no clue where to start. Sorry if this is noobie question but MySQL is really hard to conceptualize sometimes.
Try using PreparedStatement
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(ID),MAX(case when type = ''',
type,
''' then amount ELSE NULL end) AS ',
type
)
) INTO #sql
FROM table1;
SET #sql = CONCAT('SELECT name, ', #sql, '
FROM table1 GROUP BY name');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SQLFiddle Demo
Alternatively,
SELECT name,
MAX(ID) ID,
MAX(case when type = 'cars' THEN amount ELSE NULL end) AS cars,
MAX(case when type = 'bikes' THEN amount ELSE NULL end) AS bikes,
MAX(case when type = 'skates' THEN amount ELSE NULL end) AS skates,
MAX(case when type = 'pajas' THEN amount ELSE NULL end) AS pajas,
MAX(case when type = 'disks' THEN amount ELSE NULL end) AS disks
FROM table1
GROUP BY name;
SQLFiddle Demo
Related
This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
Closed 8 years ago.
I have one table like below here I need to get subject row as column and attendance as rows
sid name subject attendance
1 jhon sub1 1
2 toni sub1 0
3 danial sub1 1
4 jafer sub1 1
5 jhon sub2 1
6 toni sub2 1
7 danial sub2 1
8 jafer sub2 0
According to my requirements I need to get the output like the following table :
name sub1 sub2
jhon 1 1
toni 0 1
danial 1 1
jafer 1 0
I am new at writing queries in mysql.
Thanks in advance for your help!
You can use the following technique to achieve this, note that it only works when you know the total variations in your case its just sub1 and sub2 , if there are more you need to add them in the query or generate a complete dynamic query
select
name,
max(case when subject='sub1' then attendance end ) as 'sub1',
max(case when subject='sub2' then attendance end ) as 'sub2'
from test
group by name
DEMO
To make it dynamic its a bit complicated, google it about mysql dynamic pivoting and you will get some tutorial to understand how it works.
For your case you can have like below -
set #d_sql = null;
select
group_concat(distinct
concat(
'max(case when subject = ''',
subject,
''' then attendance else null end) as ',
concat('`',subject, '`')
)
) into #d_sql
from test;
set #d_sql = concat('SELECT name,', #d_sql, ' from test group by name');
PREPARE stmt from #d_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
DEMO
I have a MYSQL table I use from another plugin. The table has two fields called element_value and element_label.
element_value contains the name, address, city, state and zip of 2500 people for which I want to create a report. But, I want to take each row and create it's own column.
For example, the table looks like this:
**element_value**
John Smith
100 Elm Street
Columbus
Ohio
13579
element_label is used as an index. element_label 1 relates to the name, 2 the address, 3 the city, etc.
I want to be able to diplay the data in columns:
Name Address City State Zip
John Smith 100 Elm Street Columbus Ohio 13579
Can anyone think of how to use a MYSQL statement to do this?
This type of transformation from rows to columns is called a pivot. MySQL does not have an pivot function but this can be replicated using an aggregate function with a CASE expression.
You can use the following:
select
max(case when element_label = 'Name' then element_value end) Name,
max(case when element_label = 'Address' then element_value end) Address,
max(case when element_label = 'State' then element_value end) State,
max(case when element_label = 'City' then element_value end) City,
max(case when element_label = 'Zip' then element_value end) Zip
from yt
See SQL Fiddle with Demo.
If you had an unknown number of values that you want to convert to columns, then you can use a prepared statement to generate dynamic sql:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(CASE WHEN element_label = ''',
element_label,
''' THEN element_value else null END) AS `',
element_label, '`'
)
) INTO #sql
FROM yt;
SET #sql
= CONCAT('SELECT ', #sql, '
from yt
');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
See SQL Fiddle with Demo. Both queries give the result:
| NAME | ADDRESS | CITY | STATE | ZIP |
----------------------------------------------------------
| John Smith | 100 Elm Street | Columbus | Ohio | 13579 |
Typically, you will have a column that you will group by as well. For example, if you have a record_id column, then you will add a GROUP BY record_id, so you will return more than one row of results.
I have two tables Triples and Tags
Triples Table has the following Columns
id PostID TagID Value
1 1 1 Murder
2 1 2 New Brunswick
3 2 1 Theft
4 2 3 Gun
Tags Table has the following Columns
id TagName
1 Incident
2 Location
3 Weapon
I am trying to write sql to create a Pivot Table with Dynamic Headers
Output should be like this
PostID Incident Location Weapon
1 Murder New Brunswick
2 Theft Gun
Any help in writing the SQL would be appreciated. I have seen examples online but could not figure out this one
In order to pivot the data in MySQL, you will need to use both an aggregate function and a CASE expression.
If you have a known number of columns, then you can hard-code the query:
select p.postid,
max(case when t.tagname = 'Incident' then p.value end) Incident,
max(case when t.tagname = 'Location' then p.value end) Location,
max(case when t.tagname = 'Weapon' then p.value end) Weapon
from triples p
left join tags t
on p.tagid = t.id
group by p.postid;
See SQL Fiddle with Demo
But if you have an unknown number of columns, then you will need to use a prepared statement to generate dynamic SQL:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(CASE WHEN TagName = ''',
TagName,
''' THEN p.value END) AS `',
TagName, '`'
)
) INTO #sql
FROM tags;
SET #sql
= CONCAT('SELECT p.postid, ', #sql, '
from triples p
left join tags t
on p.tagid = t.id
group by p.postid');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
See SQL Fiddle with Demo.
Both will give the result:
| POSTID | INCIDENT | LOCATION | WEAPON |
----------------------------------------------
| 1 | Murder | New Brunswick | (null) |
| 2 | Theft | (null) | Gun |
I have a situation where I need to create columns depending on their content.
For instance, here is SQLFiddle - http://sqlfiddle.com/#!2/0ec7a/1.
I need to get a result like this:
--------------------------
| CITY | MALES | FEMALES |
--------------------------
| NY | 5 | 2 |
--------------------------
| DC | 2 | 1 |
--------------------------
How do I go about this?
I'm looking at CASE WHEN statements and IF statements from the MySQL Manual, but a clearer explanation would be very useful.
You don't even need CASE!
SELECT
city,
sum(gender = 'm') as males,
sum(gender = 'f') as females
FROM Population
group by city
See this working in SQLFiddle.
The reason this works is that in mysql, true is 1 and false is 0, so summing a condition counts how many times it was true!
For (most?) other databases, you must use the boring case inside the sum: sum(case when gender = 'm' then 1 else 0 end) etc
This type of data layout is called a "pivot". Some databases, like Oracle, support it natively through specific extensions to its flavour of SQL, but in mysql you have to "roll your own".
SELECT CITY,
SUM(CASE WHEN GENDER = 'M' THEN 1 ELSE 0 END) MALE,
SUM(CASE WHEN GENDER = 'F' THEN 1 ELSE 0 END) FEMALE
FROM Population
GROUP BY City
SQLFiddle Demo
You can also do prepared statement
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(CASE WHEN GENDER = ''',
GENDER,
''' then 1 ELSE 0 end) AS ',
GENDER
)
) INTO #sql
FROM Population;
SET #sql = CONCAT('SELECT CITY, ', #sql, '
FROM Population
GROUP BY City');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SQLFiddle Demo
Thank you in advance for anyone who helps with this. I know i've seen this done before without too much pain but cant seem to find the solution
My database looks something like:
`tbl_user:
----------
id ( pkey )
email
fName
lName
tbl_userSparseType:
-------------------
id ( pkey )
varName
displayName
tbl_userSparse:
---------------
id ( pkey )
value ( Value of Sparse Type )
user_id ( => tbl_user.id )
userSparseType_id ( => tbl_userSparseType.id )
with sample data:
tbl_user:
(id, email, fName, lName)
1 Bob#example.com Billy Bob
2 Betty#example.com Betty Sue
3 Beam#example.com Jim Beam
tbl_userSparseType:
(id, varName, displayName)
1 fullName Full Name
2 dayPhone Day Phone
3 nightPhone Night Phone
4 cellPhone Cell Phone
5 homeAddr Home Address
tbl_userSparse:
(id, value, user_id, userSparseType_id)
1 Billy John Bob 1 1
2 James B. Beam 3 1
3 123-234-3456 1 2
4 234-345-4567 1 4
5 24 Best st. 2 5
6 456-567-6789 3 3
I tried doing two left joins, but this gave me a tbl_user row for each sparse entry like:
(id, email, fName, lName, displayName, value)
1,"Bob#example.com","Billy","Bob","Full Name","Billy John Bob"
1,"Bob#example.com","Billy","Bob","Day Phone","123-234-3456"
1,"Bob#example.com","Billy","Bob","Cell Phone","234-345-4567"
And despite a few 45 or so minute sessions of looking, I cant find a way to get something more like the following without explicitly naming the columns, I need a dynamic way to only pull all display names that apply to the subset of tbl_user rows being queried:
WHERE tbl_user.id IN (1,2)
id | email | fName | lName | Full Name, | Day Phone | Cell Phone |
Home Address
-------------------------------------------------------------------------------------------------------
1 | Bob#example.com | Billy | Bob | Billy John Bob | 123-234-3456 | 234-345-4567 |
2 | Betty#example.com | Betty | Sue | | | | 24 Best St.
Thanks again in advance, I'm hoping this can be done without too much fuss. :\
Unfortunately MySQL does not have a PIVOT function which is basically what you are trying to do. So you will need to use an aggregate function with a CASE statement. If you know the number of columns, then you can hard-code the values:
select u.id,
u.email,
u.fname,
u.lname,
max(case when t.displayname = 'Full Name' then us.value end) FullName,
max(case when t.displayname = 'Day Phone' then us.value end) DayPhone,
max(case when t.displayname = 'Cell Phone' then us.value end) CellPhone,
max(case when t.displayname = 'Home Address' then us.value end) HOmeAddress
from tbl_user u
left join tbl_userSparse us
on u.id = us.user_id
left join tbl_userSparseType t
on us.userSparseType_id = t.id
where u.id in (1, 2)
group by u.id, u.email, u.fname,u.lname;
See SQL Fiddle With Demo
Now if you want to perform this dynamically, meaning you do not know ahead of time the columns to transpose, then you should review the following article:
Dynamic pivot tables (transform rows to columns)
Your code would look like this:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when t.displayname = ''',
t.displayname,
''' then us.value end) AS ',
replace(t.displayname, ' ', '')
)
) INTO #sql
FROM tbl_userSparse us
left join tbl_userSparseType t
on us.userSparseType_id = t.id;
SET #sql = CONCAT('SELECT u.id, u.email, u.fname, u.lname, ', #sql, '
from tbl_user u
left join tbl_userSparse us
on u.id = us.user_id
left join tbl_userSparseType t
on us.userSparseType_id = t.id
where u.id in (1, 2)
group by u.id, u.email, u.fname, u.lname');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
See SQL Fiddle with Demo