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 |
js.reddy
Yak Posting Veteran
80 Posts |
Posted - 2012-02-14 : 06:13:49
|
I have two tables like:table1: depdepid depname1 DEVELOP2 HR3 ADMINtable2: empempid empname depid1 raj 12 jan 13 sab 14 scot 25 rag 26 sar 37 lal 3I want the output like :output---------DEVELOPraj jansabHRscotrag ADMINsarlalIs it possible by single query to achieve this. please help me in this. RegardsJs.Reddy |
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-02-14 : 06:49:18
|
Formatting should be done in the front end (such as Crystal Reports).http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-14 : 10:03:38
|
you want all field values to come inside same filed? dept,emp etc?what meaning will it have?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
js.reddy
Yak Posting Veteran
80 Posts |
Posted - 2012-02-20 : 04:59:18
|
Hi visakhm,Unfortunately, my requirement is like this: DepartmentName and EmployeeName should come in same column.Anyway I got this finally.Here is the query. But I am not sure this will be good practice or not. SELECT DISTINCT a.Dept_Name AS Employees,a.dept_id, NULL AS Emp_id FROM dept_info a INNER JOIN emp_info b ON a.dept_id = b.Emp_Dept_id UNION ALL SELECT b.Emp_Name,a.dept_id,b.Emp_id FROM dept_info a INNER JOIN emp_info b ON a.dept_id = b.Emp_Dept_id ORDER BY dept_id,Emp_id;ThanksJs.Reddyquote: Originally posted by visakh16 you want all field values to come inside same filed? dept,emp etc?what meaning will it have?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-20 : 11:42:34
|
quote: Originally posted by js.reddy Hi visakhm,Unfortunately, my requirement is like this: DepartmentName and EmployeeName should come in same column.Anyway I got this finally.Here is the query. But I am not sure this will be good practice or not. SELECT DISTINCT a.Dept_Name AS Employees,a.dept_id, NULL AS Emp_id FROM dept_info a INNER JOIN emp_info b ON a.dept_id = b.Emp_Dept_id UNION ALL SELECT b.Emp_Name,a.dept_id,b.Emp_id FROM dept_info a INNER JOIN emp_info b ON a.dept_id = b.Emp_Dept_id ORDER BY dept_id,Emp_id;ThanksJs.Reddyquote: Originally posted by visakh16 you want all field values to come inside same filed? dept,emp etc?what meaning will it have?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
i really cant understand why you need to consider both employees and departments as oneif this is for presentation, then surely above is not the best way of doing this. you should be trying to get it done through front end application not in SQL------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|