I've the following scenario:
Table 1
group_id
location
Table 2
group_id
empname
I need the following output:
group_id, location, empname
1 ABC NULL
1 ABC XYZ
1 ABC PQR
so the first row is the master row and the rest of the rows are all detail rows for that master.
How can i get this output?
TIA
Bo
SELECT t1.group_id,t1.location,NULL AS empname FROM Table1 t1
UNION
SELECT t1.group_id,t1.location,t2.empname
FROM Table1 t1 INNER JOIN Table2 t2 ON t1.group_id=t2.group_id
ORDER BY 1,3
EDIT:
Just alias the 3rd column of the first SQL Statement as empname.
Related
I have a table with two columns and following data
ID NAME
1 ALPHA
1 ALPHA
2 BETA
1 BETA
First three rows are correct data, but in the last row someone accidentally entered ID 1 instead of ID 2, can anyone help me with a query to fetch multiple rows of ID for distinct names. I have tried the query below but its not yielding the correct result
SELECT F1.ID FROM myTable F1 WHERE F1.Name in
(SELECT DISTINCT F2.Name FROM myTable F2)
Actually, you need names that have multiple IDs; right?
For sample data:
SQL> select * From test;
ID NAME
---------- -----
1 alpha
1 alpha
2 beta
1 beta
Query, using group by and having clauses:
SQL> select name
2 from test
3 group by name
4 having count(distinct id) > 1;
NAME
-----
beta
SQL>
Suppose firstly my table1 is:
id name
----------
1 abc
2 bcd
3 cde
In table2 i store
id table1_id
------------
1 1,2
2 1,3
3 1,2,3
I want output like this
id table1_value
----------------
1 abc,bcd
2 abc,cde
3 abc,bcd,cde
Please help me in with mysql query..
Below is a query which should meet your requirements. As #GurV commented above, you should avoid storing CSV data in your table, which is not normalized, and which thwarts much of the power which MySQL has as a relational database.
SELECT t2.id,
GROUP_CONCAT(t1.name ORDER BY t1.name) AS table1_value
FROM table2 t2
INNER JOIN table1 t1
ON FIND_IN_SET(t1.id, t2.table1_id) > 0
GROUP BY t2.id
Output:
Demo here:
Rextester
id name count
------------
1 abc
2 xyz
3 xyz
4 xyz
The following query "select count(name) from temp group by name;" gives me:
count(name)
--------
1
3
I want this result to be updated to the column 'count'. To be precise I want my table to look like :
id name count
------------
1 abc 1
2 xyz 3
3 xyz 3
4 xyz 3
You can get those values with a COUNT / GROUP BY. You can do an UPDATE statement which joins your table with the sub query:-
UPDATE temp a
INNER JOIN
(
SELECT name, COUNT(*) AS name_count
FROM temp
GROUP BY name
) b
ON a.name = b.name
SET a.name_count = b.name_count;
For the following table, is it possible to get the result using a self-join?
Table:
id pId type
-------------
1 1000 1
2 1001 1
3 1002 1
4 1000 3
Expected result:
id pId type
-------------
2 1001 1
3 1002 1
In other words, I want all the rows which has type 1, but does not have type 3.
Thank you in advance.
UPDATE
this is a question in the context of a performance testing.
in other words, there are many rows like 1000 and 1001, 1002.
i'm trying to improve the performance using the current table structure.
but i understand that probably the table is not well-designed as well.
You don't need any joins - just a subselect - something like this:
select * from mytable t1
where not exists (select id from mytable t2 where t1.pid=t2.pid and type=3)
If you want to use a self join, you could use this query:
SELECT t1.id, t1.pId, t1.type
FROM
tablename t1 LEFT JOIN tablename t2
ON t1.pid = t2.pid AND t2.type=3
WHERE
t1.type=1 AND
t2.type IS NULL
Please see fiddle here.
My SQL Skills are next to none. After looking around for the past 2 hours trying to figure this out I need some help please.
I have 2 tables as below
Table1 Table2
ID | Name Status_id
----------- ----------
1 | Open 1
2 | Closed 2
3 | On-Hold 1
What I would like to do is count the status_id in table 2 and group by the status_id. Then add the Name where the ID matches in the first column.
What I have at the moment is
SELECT status_id, COUNT(*) AS 'num' FROM table2 GROUP BY status_id
This is great so far and returns
1 | 2
2 | 1
What I need to return is
Open | 2
Closed | 1
I hope that is clear. Can anyone help?
Many thanks!
SELECT a.name, COUNT(*) AS num FROM table2 b
INNER JOIN table1 a
ON b.status_id=a.id
GROUP BY status_id
In the case that you want to also have Zero for On-Hold you'd need to do a LEFT join and count the a column from table2 instead of *
SELECT t1.name,
Count(t2.Status_id) AS num
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.Status_id
GROUP BY t1.name;
DEMO