MySQL - Help me construct this query - mysql

I have a relational DB that I can't think of how to form this query.
Here's the info
Table1
id name
1 Mike
Table2
id table_1_id value setting
1 1 something setting1
2 1 something2 setting2
2 1 something3 setting3
Currently, this is my sql query
SELECT * FROM Table1
JOIN Table2 on Table2.table_1_id = Table1.id
What this outputs is something like this
id name table_1_id value setting
1 Mike 1 something1 setting1
1 Mike 1 something2 setting2
1 Mike 1 something3 setting3
Is it possible to construct this in such a way to return these results so I can export it to a CSV file?
id name table_1_id something1 something2 something3
1 Mike 1 setting1 setting2 setting3

SELECT
Table1.*,
something1Table.setting AS something1,
something2Table.setting AS something2,
something3Table.setting AS something3
FROM Table1
JOIN Table2 AS something1Table ON something1Table.table_1_id = Table1.id AND something1Table.value = 'something'
JOIN Table2 AS something2Table ON something2Table.table_1_id = Table1.id AND something2Table.value = 'something2'
JOIN Table2 AS something3Table ON something3Table.table_1_id = Table1.id AND something3Table.value = 'something3'

You need a conditional aggregation:
select table1.id, table1.name,
max(case when value = 'something1' then setting end) as setting1,
max(case when value = 'something2' then setting end) as setting2,
max(case when value = 'something3' then setting end) as setting3
from table1 join
table2
on table1.id = table2.id
group by table1.id, table1.name

This type of data transformation is known an a pivot but MySQL does not have a pivot function. So you will want to replicate it using an aggregate function with a CASE expression.
If you know the the number of values ahead of time, then you can hard-code your query similar to this:
select t1.id,
t1.name,
max(case when t2.value = 'something' then t2.setting end) as setting1,
max(case when t2.value = 'something2' then t2.setting end) as setting2,
max(case when t2.value = 'something3' then t2.setting end) as setting3
from table1 t1
left join table2 t2
on t1.id = t2.table_1_id
group by t1.id, t1.name;
See SQL Fiddle with Demo
But if you have an unknown number of values that you want to transform into columns, then you can use a prepared statement to generate dynamic sql.
The query would be similar to this:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when t2.value = ''',
value,
''' then t2.setting end) AS `',
value, '`'
)
) INTO #sql
FROM table2;
SET #sql = CONCAT('SELECT t1.id,
t1.name, ', #sql, '
FROM table1 t1
left join table2 t2
on t1.id = t2.table_1_id
group by t1.id, t1.name');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
See SQL Fiddle with Demo
The result of both versions is:
| ID | NAME | SOMETHING | SOMETHING2 | SOMETHING3 |
---------------------------------------------------
| 1 | Mike | setting1 | setting2 | setting3 |

GROUP_CONCAT may be of use. It doesn't give you exactly what you want, because it would put the concatenated values into a single field. But depending on what you're actually trying to accomplish, perhaps you can work around that. The advantage of the GROUP_CONCAT is that it can handle any number of table2 rows per table1 row, whereas the conditional aggregation above hardwires having three entries (which may well be what you want).
SELECT table1.*,
GROUP_CONCAT(value) AS value_group,
GROUP_CONCAT(setting) AS setting_group
FROM table1
INNER JOIN table2
ON table2.table_1_id = table1.id
returns
id,person,value_group,setting_group
1,Mike,"something1,something2,something3","setting1,setting2,setting3"

Related

Correlated subquery & conditional where clause in Sequelize

I have a correlated subquery that is validated based on the column value. Even though I read through the sequelize docs and examples on GitHub, I did not really get any working solutions.
Query I am trying to execute
select count(*) from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=132)
else 1=1 end;
Any references or suggestions are appreciated.
Update:
The goal is to fetch all records from table1, but if deciding_col value is true for a record, then I need to check if that particular record exist in table2.
Example:
Table1 Data:
id | name | deciding_col
1 abcd 1
2 edfg 0
3 xyz 0
Table2 Data:
id | second_condition
1 132
Output:
1.
select t1.* from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=132)
else 1=1 end;
id | name | deciding_col
1 abcd 1
2 edfg 0
3 xyz 0
2.
select t1.* from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=133)
else 1=1 end;
id | name | deciding_col
2 edfg 0
3 xyz 0

Using CASE in Mysql to set value to an Alias field

I have 2 tables in Mysql which looks as follows
Table 1
ID YEARMONTH
------------------
1 201210
2 201211
3 201212
Table2
ID YEARMONTH GRADE DESIG
--------------------------------
1 201210 G1 NULL
2 201210 G1 D2
3 201212 G1 D1
I am trying to set a new field (named FLAG) in table1 which will be Y if rows exist for t1.YEARMONTH in T2 else N
I want the following Result in T1
ID YEARMONTH FLAG
---------------------
1 201210 Y
2 201211 N
3 201212 Y
I have tried the following Query
SELECT
T1.ID,
T1.YEARMONTH,
CASE COUNT(T2.ID)
WHEN (SELECT
COUNT(T2.ID)
FROM TABLE2 T2)
> 0 THEN 'Y'
ELSE 'N'
END AS flag
FROM TABLE1 T1,
table2 T2;
But it produces the following output
ID YEARMONTH FLAG
---------------------------
1 201210 N
I don't know where my mistake lies. Any help would be appreciated.
Try using the below query:
SELECT
T1.ID, T1.YEARMONTH,
(CASE
WHEN COUNT(T2.ID) > 0 THEN 'Y'
ELSE 'N'
END) AS FLAG
FROM TABLE1 T1
LEFT JOIN TABLE2 T2
ON T1.YEARMONTH = T2.YEARMONTH
GROUP BY T1.ID, T1.YEARMONTH
You can see the required output here http://sqlfiddle.com/#!9/162855/12
TRY THIS: Using CASE with LEFT JOIN you can check the existence of t1.yearmonth in t2.yearmonth and generate the flag column accordingly
SELECT DISTINCT t1.id,
t1.yearmonth,
(CASE WHEN t2.yearmonth IS NOT NULL THEN 'y' ELSE 'n' END) flag
FROM table1 t1
LEFT JOIN table2 t2 ON t1.yearmonth = t2.yearmonth

How can i avoid duplication while joining two tables

i have two tables
1. test 1
2. test 2
First table has
**id** - **name**
1 - kerala
2 - Tamilnadu
Second table
**name** - **jid**
value 1 - 1
value 2 - 1
value 3 - 1
value 4 - 1
value 5 - 2
My Query --
SELECT t1.name, t2.name
FROM test1 t1
INNER JOIN test2 t2
WHERE t1.id = t2.jid
now i get this result
**name** - **name**
Kerala - value 1
kerala - value 2
kerala - value 3
kerala - value 4
But i need a result like this
Kerala - value 1
- value 2
- value 3
- value 4
the value ' Kerala ' should not be repeated .
you can user Group concat method.Pls check below query
SELECT t1.name,GROUP_CONCAT(t2.name) FROM test1 t1 INNER JOIN test2 t2 WHERE t1.id = t2.jid
You can use the following query:
SELECT CASE
WHEN t2.name = t3.firstName THEN t1.name
ELSE ''
END AS name,
t2.name
FROM test1 t1
INNER JOIN test2 t2 ON t1.id = t2.jid
INNER JOIN (
SELECT jid, MIN(name) AS firstName
FROM test2
GROUP BY jid) AS t3 ON t2.jid = t3.jid
This will produce the required result as long as there is a single record having MIN(name) per jid in test2 table.
Demo here
try this
SELECT IF (#oldname = name1,'',name1),
name2,
#oldname:=name1 AS oldname FROM
(
SELECT t1.name AS name1, t2.name AS name2
FROM test1 t1
INNER JOIN test2 t2
WHERE t1.id = t2.jid
) t,
(SELECT #oldname:='' ) tmp;
I don't think it's possible - you can't have empty values inside returned values.

select when record change value

I'm trying to select just records which changed values compared to previous record,
my table is looking like this
Id(int) | value(boolean) |
-------------------------
1 | 0 |
-------------------------
2 | 1 |
-------------------------
3 | 1 |
-------------------------
4 | 1 |
-------------------------
5 | 0 |
-------------------------
6 | 0 |
-------------------------
I must get id:2,5
thanks for your help
I did a self-join, but instead of joining identical ids, I join t1.id = t2.id-1 and then compare the values:
select t2.id
from thetable t1
inner join thetable t2 on t1.id = t2.id-1
where t1.value != t2.value
sqlfiddle: http://sqlfiddle.com/#!2/6d626/4
Edit to add:
Thanks to #Ravinder, I figured out a way to do this even if the ids aren't sequential.
I used this related question.
SET #a :=0;
SET #b :=1;
SELECT t1.id, t1.rownum
FROM
(SELECT if(#a, #a:=#a+1, #a:=1) as rownum, id, value FROM thetable) AS t1
LEFT JOIN
(SELECT if(#b, #b:=#b+1, #b:=1) as rownum, id, value FROM thetable) AS t2
ON t1.rownum = t2.rownum
where t2.value != t1.value
SQLFiddle with non-sequential ids
Basically if you don't have sequential ids you create your own. I called them rownum.
You can use mysql variable in query to check if its new or unchanged
SELECT
`Id`,
`value`,
(CASE when #test=value then 'unchanged'
else 'changed' end) occurance,
#test:=`value` testvar
FROM
Table1
,(SELECT #test:='new') t
HAVING occurance='changed'
ORDER BY `Id`
See fiddle demo

Combine multiple rows into one row MySQL

Say I have two tables in a MySQL Database.
Table 1:
ID Name
1 Jim
2 Bob
3 John
Table 2:
ID key value
1 address "X Street"
1 city "NY"
1 region "NY"
1 country "USA"
1 postal_code ""
1 phone "123456789"
When selecting rows from the database, is there any way to join rows from the second table as columns to the first table?
The desired result right from the MySQL query is:
ID Name address city region country postal_code phone
1 Jim X Street NY NY USA NULL 123456789
2 Bob NULL NULL NULL NULL NULL NULL
3 John NULL NULL NULL NULL NULL NULL
Thanks for any help!
This type of data transformation is known as a PIVOT. MySQL doesn't have a pivot function but you can replicate it using an aggregate function with a CASE expression:
select t1.id,
t1.name,
max(case when t2.`key` = 'address' then t2.value end) address,
max(case when t2.`key` = 'city' then t2.value end) city,
max(case when t2.`key` = 'region' then t2.value end) region,
max(case when t2.`key` = 'country' then t2.value end) country,
max(case when t2.`key` = 'postal_code' then t2.value end) postal_code,
max(case when t2.`key` = 'phone' then t2.value end) phone
from table1 t1
left join table2 t2
on t1.id = t2.id
group by t1.id, t1.name
See SQL Fiddle with Demo.
This could also be written using multiple joins on your table2 and you would include a filter on the join for each key:
select t1.id,
t1.name,
t2a.value address,
t2c.value city,
t2r.value region,
t2y.value country,
t2pc.value postal_code,
t2p.value phone
from table1 t1
left join table2 t2a
on t1.id = t2a.id
and t2a.`key` = 'address'
left join table2 t2c
on t1.id = t2c.id
and t2c.`key` = 'city'
left join table2 t2r
on t1.id = t2r.id
and t2c.`key` = 'region'
left join table2 t2y
on t1.id = t2y.id
and t2c.`key` = 'country'
left join table2 t2pc
on t1.id = t2pc.id
and t2pc.`key` = 'postal_code'
left join table2 t2p
on t1.id = t2p.id
and t2p.`key` = 'phone';
See SQL Fiddle with Demo.
The above two versions will work great if you have a limited number of key values. If you have an unknown number of values, then you will want to look at using a prepared statement to generate dynamic SQL:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when t2.`key` = ''',
`key`,
''' then t2.value end) AS `',
`key`, '`'
)
) INTO #sql
from Table2;
SET #sql
= CONCAT('SELECT t1.id, t1.name, ', #sql, '
from table1 t1
left join table2 t2
on t1.id = t2.id
group by t1.id, t1.name;');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
See SQL Fiddle with Demo
All versions will give a result:
| ID | NAME | ADDRESS | CITY | REGION | COUNTRY | POSTAL_CODE | PHONE |
|----|------|----------|--------|--------|---------|-------------|-----------|
| 1 | Jim | X Street | NY | (null) | (null) | (null) | 123456789 |
| 2 | Bob | (null) | (null) | (null) | (null) | (null) | (null) |
| 3 | John | (null) | (null) | (null) | (null) | (null) | (null) |
You have a structure called entity-attribute-value in the second table. There are two ways to do the combination. I think the aggregation method is the easier to express:
select t1.name,
max(case when `key` = 'address' then value end) as address,
max(case when `key` = 'city' then value end) as city,
max(case when `key` = 'region' then value end) as region,
max(case when `key` = 'country' then value end) as country,
max(case when `key` = 'postal_code' then value end) as postal_code,
max(case when `key` = 'phone' then value end) as phone
from table1 t1 left join
table2 t2
on t1.id = t2.id
group by t1.name;
The second method is to do separate joins for each value:
select t1.name, address.value, city.value, . . .
from table1 t1 left join
table2 address
on t1.id = address.id and address.`key` = 'Address' left join
table2 city
on t1.id = city.id and city.`key` = 'City' . . .
Depending on the structure of the data, the join method can actually be faster in MySQL when it uses appropriate indexing. (Other databases have been algorithms for aggregation, so the group by method often works well in other databases.)