如何在列中使用值获取列输出作为列名称 [英] how to get the column output as column name in sql server with values

查看:472
本文介绍了如何在列中使用值获取列输出作为列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有2张桌子.

表格:EMP_DETail

Hi,

I have 2 tables.

TABLE: EMP_DETail

EMP_ID     EMP_NAme     EMP_LAST_NAME
 1         inayat       basha
 2         santosh      muddala
 3         pavan        sree


表格:EMP_ADDRESS


TABLE: EMP_ADDRESS

EMP_ID     TYPE     PHONE_NO
  1        RES      080-2414
  1        CELL     987451
  1        OFF      080241
  2        RES      028741
  2        CELL     87414
  2        OFF      21474
  3        RES      04087845
  3        CELL      874555


这是我拥有的两个表,我希望将输出作为


These are the two tables i have, and i want the out put as

EMP_ID    EMP_NAME   EMP_LAST NAME      CELL       RES
 1        inayat     basha              981451     080-2414
 2        santosh    maddula            87414      028741
 3        pavan      sree               874555     04087845



问候,
S.Inayat Basha.

[edit]已添加代码块以整洁显示,忽略HTML ..."选项已禁用-OriginalGriff [/edit]



Regards,
S.Inayat Basha.

[edit]Code blocks added to tidy up display, "Ignore HTML..." option disabled - OriginalGriff[/edit]

推荐答案

您还可以使用普通联接和两次加入EMP_ADDRESS表.像
You can also use normal join and join the EMP_ADDRESS table twice. Something like
SELECT ed.*,
       ea1.Phone_No AS Cell,
       ea2.Phone_No AS Res
FROM   Emp_Detail ed 
       INNER JOIN Emp_Address ea1 ON ed.Emp_Id = ea1.Emp_Id
       INNER JOIN Emp_Address ea2 ON ed.Emp_Id = ea2.Emp_Id 
WHERE  ea1.Type = ''RES''
AND    ea2.Type = ''CELL''


如果单元格和res不是必需的行,请使用LEFT JOIN代替


If cell and res are not mandatory rows use LEFT JOIN instead


,您可以尝试执行以下操作:

you can try something like this:

Select (det.emp_id,det.emp_name,det.emp_last_name,
(select phone_no from table emp_address where epmid =det.emp_id and type = 'cell') as 'cell',(select phone_no from table emp_address where epmid =det.emp_id and type = 'Res') as 'Res')
from table emp_detail det



我目前没有SQL来检查查询,但是逻辑可能是这样的.

使用"pre"标记作为代码



I am currently not having SQL to check the query but the logic could be like this.

Used "pre" tag for the code


这篇关于如何在列中使用值获取列输出作为列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆