我已经获得了这些数据,我需要使用像PIVOT表这样的东西。
我一直在尝试,但一直在犯错。
CREATE TABLE Table1
([Client_Id] varchar(8), [Project_Id] varchar(4), [WeekStart] datetime, [Total] int, [Country_Name] varchar(9));
INSERT INTO Table1
([Client_Id], [Project_Id], [WeekStart], [Total], [Country_Name])
VALUES
('CLIENTID', 'TEST', '2014-10-27 00:00:00', 1, 'UK'),
('CLIENTID', 'TEST', '2014-11-10 00:00:00', 1, 'UK'),
('CLIENTID', 'TEST', '2014-12-29 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-01-26 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-01-26 00:00:00', 2, 'Uruguay'),
('CLIENTID', 'TEST', '2015-01-26 00:00:00', 2, 'USA'),
('CLIENTID', 'TEST', '2015-01-26 00:00:00', 2, 'Italy'),
('CLIENTID', 'TEST', '2015-02-02 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-02-09 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-02-09 00:00:00', 2, 'Ireland'),
('CLIENTID', 'TEST', '2015-02-16 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-02-16 00:00:00', 2, 'Argentina'),
('CLIENTID', 'TEST', '2015-02-16 00:00:00', 2, 'Italy'),
('CLIENTID', 'TEST', '2015-02-16 00:00:00', 2, 'Brazil'),
('CLIENTID', 'TEST', '2015-02-16 00:00:00', 2, 'Mexico'),
('CLIENTID', 'TEST', '2015-02-16 00:00:00', 2, 'Spain'),
('CLIENTID', 'TEST', '2015-02-16 00:00:00', 1, 'Albania'),
('CLIENTID', 'TEST', '2015-02-16 00:00:00', 1, 'Portugal'),
('CLIENTID', 'TEST', '2015-03-02 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-03-30 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-04-27 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-06-01 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-06-29 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-08-03 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-08-31 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-09-28 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-11-02 00:00:00', 2, 'UK'),
('CLIENTID', 'TEST', '2015-11-30 00:00:00', 2, 'UK');
我需要以这种方式呈现数据
Client_Id Project_IdCountry_Na10/27/201411/10/201412/29/2014 1/26/2015 2/2/2015 2/9/2015 2/16/2015 3/2/2015 3/30/2015 4/27/2015 6/1/2015 6/29/2015 8/3/2015 8/31/2015 9/28/2015 11/2/201511/30/2015
CLIENTID TEST UK 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
CLIENTID TEST Uruguay 2
CLIENTID TEST USA 2
CLIENTID TEST Italy 2 2
CLIENTID TEST Ireland 2
CLIENTID TEST Argentina 2
CLIENTID TEST Brazil 2
CLIENTID TEST Mexico 2
CLIENTID TEST Spain 2
CLIENTID TEST Albania 1
CLIENTID TEST Portugal 1
非常感谢任何帮助!
谢谢!
答案 0 :(得分:1)
尝试使用此代码
DECLARE @Table1 TABLE
(
[Client_Id] VARCHAR(8),
[Project_Id] VARCHAR(4),
[WeekStart] DATETIME,
[Total] INT,
[Country_Name] VARCHAR(9)
);
INSERT INTO @Table1
([Client_Id],
[Project_Id],
[WeekStart],
[Total],
[Country_Name])
VALUES ('CLIENTID',
'TEST',
'2014-10-27 00:00:00',
1,
'UK'),
('CLIENTID',
'TEST',
'2014-11-10 00:00:00',
1,
'UK'),
('CLIENTID',
'TEST',
'2014-12-29 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-01-26 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-01-26 00:00:00',
2,
'Uruguay'),
('CLIENTID',
'TEST',
'2015-01-26 00:00:00',
2,
'USA'),
('CLIENTID',
'TEST',
'2015-01-26 00:00:00',
2,
'Italy'),
('CLIENTID',
'TEST',
'2015-02-02 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-02-09 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-02-09 00:00:00',
2,
'Ireland'),
('CLIENTID',
'TEST',
'2015-02-16 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-02-16 00:00:00',
2,
'Argentina'),
('CLIENTID',
'TEST',
'2015-02-16 00:00:00',
2,
'Italy'),
('CLIENTID',
'TEST',
'2015-02-16 00:00:00',
2,
'Brazil'),
('CLIENTID',
'TEST',
'2015-02-16 00:00:00',
2,
'Mexico'),
('CLIENTID',
'TEST',
'2015-02-16 00:00:00',
2,
'Spain'),
('CLIENTID',
'TEST',
'2015-02-16 00:00:00',
1,
'Albania'),
('CLIENTID',
'TEST',
'2015-02-16 00:00:00',
1,
'Portugal'),
('CLIENTID',
'TEST',
'2015-03-02 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-03-30 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-04-27 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-06-01 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-06-29 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-08-03 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-08-31 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-09-28 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-11-02 00:00:00',
2,
'UK'),
('CLIENTID',
'TEST',
'2015-11-30 00:00:00',
2,
'UK');
SELECT *
FROM @Table1
PIVOT (sum(total)
FOR weekstart IN ([2014-10-27],
[2014-11-10],
[2014-12-29],
[2015-01-26],
[2015-02-02],
[2015-02-09],
[2015-02-16],
[2015-03-02],
[2015-03-30],
[2015-04-27],
[2015-06-01],
[2015-06-29],
[2015-08-03],
[2015-08-31],
[2015-09-28],
[2015-11-02],
[2015-11-30]) )t