我有數據在SQL Server 2008中的表看起來像這樣:SQL Server 2008的透視查詢
我要轉動它看起來像這樣:
基本上我想對ReadTime和Coater進行分組,然後在DataType列上旋轉。有人可以幫我用T-SQL來做到這一點嗎?
我有數據在SQL Server 2008中的表看起來像這樣:SQL Server 2008的透視查詢
我要轉動它看起來像這樣:
基本上我想對ReadTime和Coater進行分組,然後在DataType列上旋轉。有人可以幫我用T-SQL來做到這一點嗎?
也許這將幫助:
測試數據:
DECLARE @tbl TABLE
(
ReadTime DATETIME,
Coater VARCHAR(100),
Value FLOAT,
DateType VARCHAR(100)
)
INSERT INTO @tbl VALUES('2012-10-01 13:28:01','PBG11A-COATER',1,'FCDAOpside473')
INSERT INTO @tbl VALUES('2012-10-01 13:28:01','PBG11A-COATER',1,'FCDABackside473')
INSERT INTO @tbl VALUES('2012-10-01 13:28:01','PBG11A-COATER',0,'FCDAOpside472')
INSERT INTO @tbl VALUES('2012-10-01 13:28:01','PBG11A-COATER',0.9,'FCDABackside472')
INSERT INTO @tbl VALUES('2012-10-01 13:28:01','PBG11B-COATER',1,'FCDAOpside473')
INSERT INTO @tbl VALUES('2012-10-01 13:28:01','PBG11B-COATER',2,'FCDABackside472')
INSERT INTO @tbl VALUES('2012-10-01 13:28:01','PBG11B-COATER',2.9,'FCDAOpside472')
INSERT INTO @tbl VALUES('2012-10-01 13:28:01','PBG11B-COATER',0,'FCDABackside473')
INSERT INTO @tbl VALUES('2012-10-01 13:28:41','PBG11A-COATER',1,'FCDAOpside473')
INSERT INTO @tbl VALUES('2012-10-01 13:28:41','PBG11A-COATER',1,'FCDABackside473')
INSERT INTO @tbl VALUES('2012-10-01 13:28:41','PBG11A-COATER',0,'FCDAOpside472')
INSERT INTO @tbl VALUES('2012-10-01 13:28:41','PBG11A-COATER',0.9,'FCDABackside472')
INSERT INTO @tbl VALUES('2012-10-01 13:28:41','PBG11B-COATER',1,'FCDAOpside473')
INSERT INTO @tbl VALUES('2012-10-01 13:28:41','PBG11B-COATER',2,'FCDABackside472')
INSERT INTO @tbl VALUES('2012-10-01 13:28:41','PBG11B-COATER',2.9,'FCDAOpside472')
INSERT INTO @tbl VALUES('2012-10-01 13:28:41','PBG11B-COATER',0,'FCDABackside473')
查詢:
SELECT
*
FROM
(
SELECT
ReadTime,
Coater,
DateType,
Value
FROM @tbl AS tbl
) AS SourceTable
PIVOT
(
MIN(Value)
FOR DateType IN
(
[FCDAOpside472],
[FCDAOpside473],
[FCDABackside472],
[FCDABackside473]
)
)AS PivotTable
ORDER BY
ReadTime
你真的想讓我混亂這個問題與查詢的所有迭代我試過了?那隻會讓你知道我已經嘗試了一些東西。 – Hosea146 2012-01-18 15:41:08
最成功的呢? – 2012-01-18 15:43:35
@ Hosea146實際上通過顯示您嘗試過的查詢來幫助社區瞭解是否改變您所做的工作會給您想要的結果。所以這不是浪費雙方的時間。 – Taryn 2012-01-18 15:44:11