如何编写SQL以获得所需的结果? [英] How to write sql to get desired result?

查看:117
本文介绍了如何编写SQL以获得所需的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi,

 i have table in db as below

deptId   deptName       9      10      11      13      groupName       A       G1       B       C      G2
------  --------       ---     ---     ---     ---     ----------     ----     ---      ---    ----    ---
8	Doctors 	2      	2       0       0       Permanent       0       1       0       0       1
8	Doctors 	2       2       0       0       Apprentice	0	0	0	0	1
     
have any ways to get the table as below

                                                                            Permanent                                 Apprentice
                                                                           -------------                              -----------
deptId   deptName       9      10      11      13      groupName       A       G1       B       C      G2       A      G1       B       C      G2
------  --------       ---     ---     ---     ---     ----------     ----     ---      ---    ----    ---     --      --       --      --     --
8	Doctors 	2      	2       0       0       Permanent       0       1       0       0       1      	0	0	0	0	1


where A,G1,B,C,G2 are the shift in permanent group in a department.

Thanks..

推荐答案

我将带您上路:

您需要将表与自身连接.
然后,您可以在单个select子句中按两个(或多个)版本中的任何一个从表中选择数据,它们将在同一行中显示.

现在-是否使用内部联接还是使用左/右外部联接尚不清楚,因为我不知道您要显示什么.

这是将表中的行转录为行中的列的常用方法.
I''ll get you on your way:

You need to do a join of the table with itself.
You may then select the data from the table by either of the two (or more) versions in a single select clause and they''ll come out in a single row.

Now - whether to use inner joins or left/right outer joins is not clear as I don''t know what you''re trying to show.

It is a common method of transcribing rows in a table to columns in a row.


这篇关于如何编写SQL以获得所需的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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