Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Jabez
Starting Member
19 Posts |
Posted - 2007-03-07 : 00:34:51
|
| Hi folks,Please help me out in writing a query.QuestionTable 1 : EmployeeTable 2 : MagazinesTable 3 : EmployeeMagazinesTable 1 : EmployeeEmpId EmpName----- ------- 1 Sailesh 2 AmitTable 2: MagazinesMg_ID Mg_Name----- ------- 1 PC-World 2 Digit 3 Linux for YouTable 3: EmployeeMagazinesEmpId Mg_ID----- ----- 1 1 1 2 2 3Now, How do I write a query to get the results as shown below:EmpName Magazine1 Magazine2------- --------- ---------Sailesh PC-World DigitAmit Linux for You Thanks,RegardsJabez |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-07 : 01:11:17
|
| SELECT e.EmpName, MIN(m.Mg_Name) AS Magazine1, NULLIF(MAX(m.Mg_Name), MIN(m.Mg_Name)) AS Magazine2FROM Employees AS eINNER JOIN EmployeeMagazines AS em ON em.EmpID = e.EmpIDINNER JOIN Magazines AS m ON m.Mg_ID = em.Mg_IDGROUP BY e.EmpID, e.EmpNamePeter LarssonHelsingborg, Sweden |
 |
|
|
Jabez
Starting Member
19 Posts |
Posted - 2007-03-08 : 05:19:04
|
| Thanks Peter for the reply. It shows the data asEmpName Magazine1 Magazine2------- --------- ---------sailesh Digit PC-Worldamit LFY NULLBut I need the data asEmpName Magazine1 Magazine2------- --------- ---------sailesh PC-World Digitamit LFY NULLPlease help me.Thanking you in anticipation. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 05:23:24
|
| Just replace MIN vs MAX and MAX vs MIN...Peter LarssonHelsingborg, Sweden |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-08 : 05:25:01
|
just change MAX to MIN, MIN to MAXSELECT e.EmpName, MAX(m.Mg_Name) AS Magazine1, NULLIF(MIN(m.Mg_Name), MAX(m.Mg_Name)) AS Magazine2FROM Employees AS eINNER JOIN EmployeeMagazines AS em ON em.EmpID = e.EmpIDINNER JOIN Magazines AS m ON m.Mg_ID = em.Mg_IDGROUP BY e.EmpID, e.EmpName refresh refresh refresh KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 05:48:33
|
| Reading this post again, makes me think he wants the magazines in Mag_ID order...Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 05:55:45
|
| [code]-- Prepare sample dataDECLARE @Employees TABLE (EmpId INT, EmpName VARCHAR(20))INSERT @EmployeesSELECT 1, 'Sailesh' UNION ALLSELECT 2, 'Amit'DECLARE @Magazines TABLE (Mg_ID INT, Mg_Name VARCHAR(20))INSERT @MagazinesSELECT 1, 'PC-World' UNION ALLSELECT 2, 'Digit' UNION ALLSELECT 3, 'Linux for You'DECLARE @EmployeesMagazines TABLE (EmpId INT, Mg_ID INT)INSERT @EmployeesMagazinesSELECT 1, 1 UNION ALLSELECT 1, 2 UNION ALLSELECT 2, 3-- Show the expected resultSELECT e.EmpName AS EmpName, m1.Mg_Name AS Magazine1, m2.Mg_Name AS Magazine2FROM ( SELECT e.EmpID, MIN(em.Mg_ID) AS MinMagID, NULLIF(MAX(em.Mg_ID), MIN(em.Mg_ID)) AS MaxMagID FROM @Employees AS e INNER JOIN @EmployeesMagazines AS em ON em.EmpID = e.EmpID GROUP BY e.EmpID ) AS xINNER JOIN @Employees AS e ON e.EmpID = x.EmpIDLEFT JOIN @Magazines AS m1 ON m1.Mg_ID = x.MinMagIDLEFT JOIN @Magazines AS m2 ON m2.Mg_ID = x.MaxMagIDORDER BY x.EmpID[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
Jabez
Starting Member
19 Posts |
Posted - 2007-03-10 : 06:52:50
|
| Thanks Peter. |
 |
|
|
|
|
|
|
|