i have 2 tables 1st table contain 3 columns id, name and dept_id and 2nd table have 2 columns dept and id i want output in 2 columns 1st is "DEPT_NAME" and 2nd column "NAME" and all name show in 1 row which are related to particular department and name separated by comma like if dept_name is HR then name should be like rick, marsh
select Dept, STUFF((SELECT ','+ Name
FROM Table1 T1
WHERE T1.Dept_Id=T2.ID for xml path('')),1,1,'')Name
FROM Table2 T2
Related
I have 2 tables called Table1(Id,Name,Surname) and Table2(Id,Pincode)
Table 1 -
Id Name Surname
1 Ram Charan
2 Shyam Mane
3 Priya Dhawal
4 Ram Charan
Table 2 -
Id Pincode
1 445502
2 885934
3 485502
4 445502
I wan to join Table1 and Table2 on field 'Id' and create column Name|Surname|Pincode by concatenating columns Name,Surname,Pincode and finally apply GroupBy() on Name|Surname|Pincode.
Expected output :-
Name|Surname|Pincode Count of Ids
Ram|Charan|445502 2
Shyam|Mane|885934 1
Priya|Dhawal|485502 1
SELECT CONCAT_WS('|', Name, Surname, Pincode) `Name|Surname|Pincode`,
COUNT(*) `Count of Ids`
FROM table1
JOIN table2 USING (Id)
GROUP BY `Name|Surname|Pincode`;
You can try below simple query -
SELECT CONCAT(Name, '|', Surname, '|', Pincode) Name|Surname|Pincode, COUNT(*)
FROM TABLE_1 T1
JOIN TABLE_2 T2 ON T1.Id = T2.Id
GROUP BY CONCAT(Name, '|', Surname, '|', Pincode);
I have 2 tables: one table with many rows and a second table with one row. The tables have no fields in common. Is it possible to combine them into one table with many rows?
I've checked UNION, but MSDN says:
The following are basic rules for combining the result sets of two queries by using UNION:
Each SELECT statement within UNION must have the same number of columns.
The columns must also have similar data types.
The columns in each SELECT statement must also be in the same order.
Example
This is what my tables look like right now:
Table 1 Table 2
Column1 Column2 Column4 Column5 Column3
------- ------- ------- ------- -------
A 1 E 10 a
B 2
C 3
D 4
And this is what I'm trying to achieve as a result:
CONSOLIDATED_Table 3
Column1 Column2 Column3 Column4 Column5
------- ------- ------- ------- -------
A 1 E 10 a
B 2 E 10 a
C 3 E 10 a
D 4 E 10 a
You can add additional columns like this:
select tid, t_name, null as s_name
from teachers t
union all
select sid, null, s_name
from students s;
yes definitely you can use INNER JOIN . its a easy way to get data from multiple table. either you can use sub query and get data in Row format
We use joins to combine columns of multiple tables, whereas we use Union to join rows of multiple tables given that the columns types and nber of columns are the same in all the select queries with union.
Since you want to show all the rows, we can use Union.
Select tid as id, t_name as name from teachers
union all
Select sid as id, s_name as name from students;
Teachers and students
select * from (
select 'teacher' as rowtype, tid as id , t_name as name
from teachers
union all
select 'student', sid, s_name
from students) t
order by name;
This approach produces a cartesian product, but since you have only 1 row in table 2, it should work for your specific use case.
select * from table_1, table_2;
I have a table with products: a foreign key(fk id), name and more data. There are duplicates in name, but there are for example customers related to the fk id
Therefore I want to update the duplicates with the first matching fk id
Example:
fk id name
1 abc
2 abc
3 abc
67 abc
Table after update:
fk id name
1 abc
1 abc
1 abc
1 abc
So far I got a query to put them together in a comma separated list, but I am missing the Update:
SELECT
count(*) as amount,
group_concat(name) as names,
group_concat(id) as ids
FROM db.product
GROUP BY name
HAVING amount> 1;
In mysql you can use joins in update statements. I would create a subquery that returns the lowest id (min()) for each name that appears multiple times. Since in mysql you cannot select from the same table that is being updated, therefore an extra layer of subselect is added on the top of the subselect:
UPDATE db.product
INNER JOIN (SELECT * FROM
(SELECT name, min(id) as minid
FROM db.product
GROUP BY name
HAVING count(*)> 1) t2 ) t on t.name=db.product.name
SET db.product.id=t.minid;
I have a table named 'Student' with columns 'Name' and 'Roll Number'
name Roll_Number
A 1
A 2
A 1
B 2
B 2
C 3
C 2
D 4
I want to delete all the rows from this table having same name but different Roll_number and insert those rows in new table 'Temp'. So after operation Both tables should be like this
Table Student
name Roll_Number
B 2
B 2
D 4
Table Temp
name Roll_Number
A 1
A 2
A 1
C 3
C 2
Because A and C, both have different values for Roll_Number column, So we delete all the entries of A and C from Student table and add it to Temp Table
So how this can be done through mysql query?
Try something like:
For Student table:
SELECT name, Roll_Number
FROM Student
GROUP BY name
HAVING COUNT(DISTINCT Roll_Number) = 1
For Temp table:
SELECT name, Roll_Number
FROM Student
GROUP BY name
HAVING COUNT(DISTINCT Roll_Number) > 1
Here is the query that work for me, created from SMA and Tim answers.
For inserting into Temp Table
INSERT INTO Temp (name, Roll_Number)
SELECT name, Roll_Number
FROM Student
WHERE name IN (SELECT name
FROM Student
GROUP BY name
HAVING COUNT(DISTINCT Roll_Number) > 1);
Deleting record query is same as mentioned by Tim
DELETE FROM Student
WHERE name IN (SELECT name FROM Temp)
Thanks to both of them
Here is a query to insert all records which, for a given name have more than one Roll_Number, into a new temporary table Temp:
INSERT INTO Temp (name, Roll_Number)
SELECT name, Roll_Number
FROM Student
WHERE name IN (SELECT name
FROM Student
GROUP BY name
HAVING COUNT(DISTINCT Roll_Number) > 1)
And here is a query to delete these same records from Student:
DELETE FROM Student
WHERE name IN (SELECT name FROM Temp)
Note that I did the insertion into the new table first before deleting them from the old table (for obvious reasons).
I have a mySQL database with a couple of identical tables. I need to join all the tables and sum up the views and hits every time the id1 and id2 are equal in at least 2 tables, or simply show the row if not.
Please see below the tables structure:
Table1:
id..id2...views...hits
1...102...55......12
2...103...12......22
Table2:
id..id2...views...hits
1...123...512......13
2...103...123......43
Table3:
id..id2...views...hits
1...102...232......43
2...103...100......70
The end result should be the following table:
id...id2...views...hits
1....102...287....65 <-- This one is the result of adding 1st row of table1 and 2nd row of table 2
1....123...512....13 <-- This is the 1st row of table2 as there's no other id2 = 123
2....103...235....135 <-- This is the sum of 2nd row in table1 + 2nd row in table2 + 2nd row in table3
I hope this makes sense and someone can help with it.
Thanks !
Put the rows of all three tables together with a union, then group and sum like usual:
SELECT id, id2, SUM(views), SUM(hits)
FROM
(
SELECT id, id2, views, hits
FROM Table1
UNION ALL
SELECT id, id2, views, hits
FROM Table2
UNION ALL
SELECT id, id2, views, hits
FROM Table3
) x
GROUP BY id, id2