0

複数の行を列に選択できる選択を作成したいと思います。年が存在しない場合、その年の NULL 値を表示します

例えば下の表

CREATE TABLE [dbo].[MPG_TABLE](
[Driver] [char](40) NULL,
[Plate] [char](20) NOT NULL,
[Type] [char](20) NOT NULL,
[Build] [char](6) NULL,
[Miles] [float] NULL,
[Gallon] [float] NULL,
[MPG] [numeric](8, 2) NULL
) ON [PRIMARY]

表を埋める:

insert into MPG_TABLE (Driver, Plate, Type, Build, Miles, Gallon)  
values ('JOHN', 'AAAA', '4X4', '2010', 20000, 2100), 
('JOHN', 'AAAA', '4X4', '2011', 30000, 2900),
('JOHN', 'AAAA', '4X4', '2012', 25000, 2300),
('JOHN', 'AAAA', '4X4', '2013', 21420, 2130),
('PETER', 'BBBB', 'LUXE', '2011', 22040, 1650),
('PETER', 'BBBB', 'LUXE', '2012', 24000, 1800), 
('JACK', 'CCCC', 'LORRY', '2009', 180040, 96005),       
('JACK', 'CCCC', 'LORRY', '2010', 220040, 100005),
('JACK', 'CCCC', 'LORRY', '2011', 240000, 100235), 
('ADRIAN', 'EEEE', 'LUXE', '2010', 19040, 1450),
('ADRIAN', 'EEEE', 'LUXE', '2011', 30000, 2200),         
('ADRIAN', 'DDDD', 'LUXE', '2012', 22040, 1650),
('ADRIAN', 'DDDD', 'LUXE', '2013', 24000, 1800),  
('JERRY', 'FFFF', 'LUXE', '2013', 2000, 100)   

次に、最後の列を更新します。

update MPG_TABLE
set MPG = (Miles / Gallon)

私はこのようなものを持っていますが、それは私が望むものを示していません:

select distinct c.Driver, c.Plate, c.Type, a.build, a.Miles, a.gallon, 
a.mpg, b.build, b.miles,  b.gallon, b.mpg, c.build, c.miles, c.gallon, c.mpg  
from MPG_TABLE as a left outer join MPG_TABLE as b 
on ISNULL(a.Driver, null) = ISNULL(b.driver, null),
MPG_TABLE as c    
where a.Build = '2012'
and b.Build = '2013' 
and c.Build = '2011'

結果は次のようになります。

                          2009                    2010                      2011
Driver    Plate   Type    Miles   Gallon  MPG     Miles    Gallon   MPG     Miles
JOHN      AAAA    4X4     NULL    NULL    NULL    20000    2100     9.52    30000
PETER     BBBB    LUXE    NULL    NULL    NULL    NULL     NULL     NULL    22040
JACK      CCCC    LORRY   180040  96005   1.88    220040   100005   2.2     240000
ADRIAN    EEEE    LUXE    NULL    NULL    NULL    19040    1450     13.13   30000
ADRIAN    DDDD    LUXE    NULL    NULL    NULL    NULL     NULL     NULL    NULL
JERRY     FFFF    LUXE    NULL    NULL    NULL    NULL     NULL     NULL    NULL

このための選択を作成する方法は?

4

2 に答える 2

0

このクエリを試してください:

select Driver, Plate, Type
    , min(case Build when '2009' then Miles end) as Miles2009
    , min(case Build when '2009' then Gallon end) as Gallon2009
    , min(case Build when '2009' then MPG end) as MPG2009
    , min(case Build when '2010' then Miles end) as Miles2010
    , min(case Build when '2010' then Gallon end) as Gallon2010
    , min(case Build when '2010' then MPG end) as MPG2010
    , min(case Build when '2011' then Miles end) as Miles2011
    , min(case Build when '2011' then Gallon end) as Gallon2011
    , min(case Build when '2011' then MPG end) as MPG2011
    , min(case Build when '2012' then Miles end) as Miles2012
    , min(case Build when '2012' then Gallon end) as Gallon2012
    , min(case Build when '2012' then MPG end) as MPG2012
    , min(case Build when '2013' then Miles end) as Miles2013
    , min(case Build when '2013' then Gallon end) as Gallon2013
    , min(case Build when '2013' then MPG end) as MPG2013
from MPG_TABLE t
cross join (values(1),(2),(3),(4),(5))b(b) -- number of years you want to display
group by Driver, Plate, Type

ただし、重要なことを 1 つ覚えておいてください。このようなピボットは、サーバーではなく常にクライアントで行う必要があります!!!

于 2013-08-09T11:57:45.243 に答える