Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friend,

i am facing problem when impliments query. i have two table name as dept and emp.dept attributes are deptid,deptname and emp attributes are empid,empname,deptid.you got in deptid is foreign key of dept table or entity.

i had inserted data in dept table just like

deptid  deptname

1       hr
2       .net

i had inserted data in emp table just like

empid  empname  deptid
1      ashwini  1
2      harshal  2
3      shital   1
4      ganpat   2
5      smita    1

i have retrieve data from both table just like

hr
.net
hr
ashiwini
shital
smita
.net
harshal
ganpat.

Did you get in. i want to fetch data firstly by deptname from dept table then later i fetch data from
both table dept and emp but as per group of dept.


its so much complicated to solve this query.please give me some suggestion or information how to get me
this type of output.

thanks in advanced !!
Posted
Updated 24-Jan-15 0:40am
v2
Comments
[no name] 24-Jan-15 6:47am    
No I did not get in....but maybe you can play with something like this
SELECT X,Y,Z
FROM DEPT
LEFT JOIN EMP ON EMP.DEPTID = DEPT.DEPTID
WHERE whatever you need
Member 8089110 24-Jan-15 7:11am    
i want to get in data just like first retrieve deptname then later group wise means first deptname then empname who is member of particular dept then another deptname ,empname who is member of that dept ...

 
Share this answer
 
Try this:
select empname, deptname from employee e join department d on e.deptid=d.deptid order by deptname
 
Share this answer
 
Not sure if I understood your question correctly but if the amount of departments is fixed then you can try something like
SQL
select 1, dept_name from dept
union all
select 2, dept_name from dept where dept_name = 'hr'
union all
select 3, emp_name  from emp  where dept_name = 'hr'
union all
select 4, dept_name from dept where dept_name = '.net'
union all
select 5, emp_name  from emp  where dept_name = '.net'
order by 1, 2

If the amount of departments isn't fixed I suggest using a table valued function. For example, see Using Table-Valued Functions in SQL Server[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900