可以在sql中组合id上的列吗?

时间:2014-09-11 18:28:25

标签: sql sql-server

我正在使用SQL Server,我需要一个可以更改我正在使用的表的查询:

从此

| Band Name | Guitar1       | Guitar2 | Drums     | Bass          | Vocals      |
---------------------------------------------------------------------------------
| LedZep    | JimmyPage     | NULL    | NULL      | NULL          | NULL        |
| LedZep    | NULL          | NULL    | JonBonham | NULL          | NULL        |
| LedZep    | NULL          | NULL    | NULL      | JohnPaulJones | NULL        |
| LedZep    | NULL          | NULL    | NULL      | NULL          | RobertPlant |

“MAGIC SQL QUERY”

到此:

Band Name | Guitar1    | Guitar2 | Drums     | Bass         | Vocals      |
---------------------------------------------------------------------------
LedZep    | Jimmy Page | NULL    | JonBonham | JonPaulJones | RobertPlant |

4 个答案:

答案 0 :(得分:2)

这可能取决于您使用的服务器软件是什么,但基本的想法是:

SELECT 
    BandName,
    MAX(Guitar1) Guitar1,
    MAX(Guitar2) Guitar2,
    MAX(Drums) Drums,
    MAX(Vocals) Vocals
FROM Bands
GROUP BY BandName

但是,如果乐队有两个记录,其值为Vocals(或任何列),您对结果的期望是什么?

答案 1 :(得分:2)

这里的细节非常稀少,但这应该有用。

select BandName
    , MAX(Guitar1) as Guitar1
    , MAX(Guitar2) as Guitar2
    , MAX(Drums) as Drums
    , MAX(Bass) as Bass
    , MAX(Vocal) as Vocals
from SomeTable
group by BandName

答案 2 :(得分:0)

我不知道使用这种方法是否有任何好处,我编写了查询,所以我会分享,这是一个相当复杂的解决方案,而不仅仅是使用其他两个SO用户建议的MAX()和GROUP BY: )。无论如何,最好有一个计划B到位:)

SQL Fiddle

MS SQL Server 2008架构设置

查询1

DECLARE @Table TABLE 
(BandName VARCHAR(20),Guitar1 VARCHAR(20),Guitar2 VARCHAR(20)
, Drums VARCHAR(20),Bass VARCHAR(20),Vocals VARCHAR(20))

INSERT INTO @Table VALUES 
('LedZep1','JimmyPage1',   NULL  ,  NULL     ,  NULL     ,      NULL ),
('LedZep1',   NULL    ,   NULL  ,'JonBonham1',  NULL     ,      NULL ),
('LedZep1',   NULL    ,   NULL  ,  NULL     , 'JohnPaulJones1', NULL ),
('LedZep1',   NULL    ,   NULL  ,  NULL     ,  NULL          ,'RobertPlant1'),
('LedZep2','JimmyPage2',   NULL  ,  NULL     ,  NULL     ,      NULL ),
('LedZep2',   NULL    ,   NULL  ,'JonBonham2',  NULL     ,      NULL ),
('LedZep2',   NULL    ,   NULL  ,  NULL     , 'JohnPaulJones2', NULL ),
('LedZep2',   NULL    ,   NULL  ,  NULL     ,  NULL          ,'RobertPlant2')

SELECT * 
FROM @Table t
 UNPIVOT ( Vals FOR N IN (Guitar1, Guitar2, Drums, Bass, Vocals ))up
 PIVOT (MAX(Vals)
        FOR N 
        IN (Guitar1, Guitar2, Drums, Bass, Vocals )
        )p

<强> Results

| BANDNAME |    GUITAR1 | GUITAR2 |      DRUMS |           BASS |       VOCALS |
|----------|------------|---------|------------|----------------|--------------|
|  LedZep1 | JimmyPage1 |  (null) | JonBonham1 | JohnPaulJones1 | RobertPlant1 |
|  LedZep2 | JimmyPage2 |  (null) | JonBonham2 | JohnPaulJones2 | RobertPlant2 |

答案 3 :(得分:0)

管理每个角色有多个人的情况:

DECLARE @Table TABLE 
(BandName VARCHAR(20),Guitar1 VARCHAR(20),Guitar2 VARCHAR(20)
, Drums VARCHAR(20),Bass VARCHAR(20),Vocals VARCHAR(20))

INSERT INTO @Table VALUES 
('LedZep','JimmyPage',   NULL  ,  NULL     ,  NULL     ,      NULL ),
('LedZep',   NULL    ,   NULL  ,'JonBonham',  NULL     ,      NULL ),
('LedZep',   NULL    ,   NULL  ,  NULL     , 'JohnPaulJones', NULL ),
('LedZep',   NULL    ,   NULL  ,  NULL     ,  NULL          ,'RobertPlant'),
('Other',   'Guitar',   NULL  ,  NULL     ,  NULL     ,      NULL ),
('Other',   NULL    ,   NULL  , 'Drums'     ,  NULL     ,      NULL ),
('Other',   NULL    ,   NULL  ,  NULL     , 'Bass', NULL ),
('Other',   NULL    ,   NULL  ,  NULL     ,  NULL          ,'Vocal1'),
('Other',   NULL    ,   NULL  ,  NULL     ,  NULL          ,'Vocal2')


SELECT DISTINCT BandName
    , STUFF((SELECT ','+Guitar1 
             FROM @table S2 
             WHERE S1.BandName = S2.BandName
             FOR XML PATH('')), 1, 1, '') AS Guitar1
    , STUFF((SELECT ','+Guitar2 
             FROM @table S2 
             WHERE S1.BandName = S2.BandName
             FOR XML PATH('')), 1, 1, '') AS Guitar2             
    , STUFF((SELECT ','+Guitar2 
             FROM @table S2 
             WHERE S1.BandName = S2.BandName
             FOR XML PATH('')), 1, 1, '') AS Drums
    , STUFF((SELECT ','+Drums 
             FROM @table S2 
             WHERE S1.BandName = S2.BandName
             FOR XML PATH('')), 1, 1, '') AS Bass            
    , STUFF((SELECT ','+Bass 
             FROM @table S2 
             WHERE S1.BandName = S2.BandName
             FOR XML PATH('')), 1, 1, '') AS Vocals
    , STUFF((SELECT ','+Vocals 
             FROM @table S2 
             WHERE S1.BandName = S2.BandName
             FOR XML PATH('')), 1, 1, '') AS Vocals          

FROM @table S1