我编写了一个需要用于从asp.net SQL命令导出到Excel文件的查询,但首先我只想要转置列到行我尝试使用数据透视但是它不起作用
我写了一个这样的查询
SELECT CATAGORY_NAME,GROUP_NAME,FUNCTION_NAME,'G'+TEST_CASE.GID + '-TC'+TEST_CASE.CASE_ID CASE_ID,CASE_NAME,CASE_NAME_TH,EXPECT_RESULT,EXPECT_DETAIL,INTERFACE,RESULT,TEST_DATE,TEST_BY,REMARK
FROM TEST_CASE
LEFT OUTER JOIN TEST_CATAGORY
ON TEST_CASE.CID = TEST_CATAGORY.CID
AND TEST_CASE.SID = TEST_CATAGORY.SID
LEFT OUTER JOIN TEST_GROUP ON TEST_CASE.GID = TEST_GROUP.GID AND TEST_CASE.CID = TEST_GROUP.CID WHERE TEST_CASE.SID = 1 ORDER BY TEST_CASE.CID
它工作正常我需要通过CATAGORY NAME转换它们
from(所有因为有很多列的例子)
CATAGORY_NAME GROUP_NAME FUNCTION_NAME CASE_ID
TS12345 TDAS NOS 1234
CATAGORY_NAME GROUP_NAME FUNCTION_NAME CASE_ID
TS12346 TDAF NOS 1235
要
CATAGORY NAME TS12345
GROUP NAME TDAS
FUNCTION NAME NOS
CASE ID 1234
CATAGORY NAME TS12346
GROUP NAME TDAF
FUNCTION NAME NOS
CASE ID 1235
来自查询的表数据
CATAGORY_NAME GROUP_NAME FUNCTION_NAME CASE_ID CASE_NAME EXPECT_RESULT INTERFACE RESULT TEST_DATE TEST_BY REMARK
Desbes Optivy TESTING125 FAST CASH G01-TC00101 Fast Rejected VISA N/R 12-ธ.ค.-14 Tester1 4012999971111142 4012999971111142
IBM Omron ADM3 (S1G366932) VISA Chip (Single App) FAST CASH G03-TC00501 Fast Approved VISA PASS 11-ธ.ค.-14 Tester1 4761340000000035 4761340000000035
Wincor PC280 (S1A365305) Abnormal SCB Card Abnormal Fast cash G28-TC11804 Inactive status Rejected CBS PASS 25-ธ.ค.-14 tester3 5.57755E+15
Wincor PC280 (S1A365305) Abnormal VISA Magnetic Abnormal Withdrawal G30-TC15402 Do not input amount Rejected #NAME? BASE24 PASS tester3
TB TEST_CASE
SID CID GID CASE_ID FUNCTION_NAME CASE_NAME CASE_NAME_TH TEST_CARD CONDITION EXPECT_RESULT ACTUAL_RESULT SEQ TEST_BY TEST_DATE REMARK RESULT INTERFACE TEMPLATE_NAME RETEST_DATE
1 1 1 101 FAST CASH Fast cash - 200 FASTCAS 12345 NULL Rejected Approved 20-21 TESTER1 12-ธ.ค.-14 TEST N/R VISA Recycle ;PN13 Jan 15
1 1 1 102 FAST CASH Fast cash - 500 FASTCAS 12345 NULL Approved Approved 22-23 TESTER2 12-ธ.ค.-14 TEST PASS VISA Recycle NULL
1 1 1 103 FAST CASH Fast cash - 1,000 FASTCAS 12345 NULL Approved Approved 26-27 TESTER3 12-ธ.ค.-14 TEST PASS VISA Recycle NULL
TB TEST_CATAGORY
SID CID CATAGORY_NAME INFO_1 INFO_2 INFO_3 INFO_4 INFO_5 TEMPLATE_NAME INFO_L1 INFO_L2 INFO_L3 INFO_L4 INFO_L5
1 1 Diebold Optiva378 (S1G366937) S1G366937 Diebold Win7 $DATA.AT401000.CDMD3IN1 Optiva378 Recycle Term Vendor OS CI File NULL
1 2 NCR S6622E (S1G365311) S1G365311 NCR Win7 $DATA.AT40N50. CI587IPF S6622E ATM Full Term Vendor OS CI File NULL
1 3 Wincor PC1500XE (S1A365303) S1A365303 Wincor XP $DATA.AT401000. CIWINCOR PC 1500XE ATM Partial Term Vendor OS CI File NULL
1 4 IBM Nautilus CD4 (S1G365306) S1G365306 IBM XP $DATA.AT401000. OMIP02 Nautilus ATM Partial Term Vendor OS CI File NULL
TB TEST_GROUP
SID CID GID GROUP_NAME GROUP_CODE TEMPLATE_NAME
1 01 VISA Magnetic NULL ATM Full
1 01 VISA Magnetic NULL ATM Multi
1 01 VISA Magnetic NULL ATM Partial
1 01 VISA Magnetic NULL Existing ATM
1 01 VISA Magnetic NULL Existing CDM
1 01 VISA Magnetic NULL Existing Multi
1 01 VISA Magnetic NULL Existing Recycle
1 01 VISA Magnetic NULL Recycle
1 02 VISA Chip-Fallback NULL ATM Full
1 02 VISA Chip-Fallback NULL ATM Multi
1 02 VISA Chip-Fallback NULL ATM Partial
1 02 VISA Chip NULL Existing ATM
1 02 VISA Chip NULL Existing CDM
1 02 VISA Chip NULL Existing Multi
1 02 VISA Chip NULL Existing Recycle
1 02 VISA Chip-Fallback NULL Recycle
1 03 VISA Chip (Single App) NULL ATM Full
1 03 VISA Chip (Single App) NULL ATM Multi
1 03 VISA Chip (Single App) NULL ATM Partial
1 03 MDS Magnetic NULL Existing ATM
1 03 MDS Magnetic NULL Existing CDM
1 03 MDS Magnetic NULL Existing Multi
1 03 MDS Magnetic NULL Existing Recycle
1 03 VISA Chip (Single App) NULL Recycle
1 04 VISA Chip (Multi App) NULL ATM Full
1 04 VISA Chip (Multi App) NULL ATM Multi
1 04 VISA Chip (Multi App) NULL ATM Partial
1 04 MDS Chip NULL Existing ATM
1 04 MDS Chip NULL Existing CDM
1 04 MDS Chip NULL Existing Multi
1 04 MDS Chip NULL Existing Recycle
1 04 VISA Chip (Multi App) NULL Recycle
1 05 MDS Magnetic NULL ATM Full
答案 0 :(得分:1)
试试这个
SELECT 'CATAGORY_NAME' COL,CATAGORY_NAME
FROM Yourtable
UNION ALL
SELECT 'GROUP_NAME',GROUP_NAME
FROM Yourtable
UNION ALL
SELECT 'FUNCTION_NAME',FUNCTION_NAME
FROM Yourtable
UNION ALL
SELECT 'CASE_ID',CASE_ID
FROM Yourtable
编辑:
对于您的更新问题,可以遵循以下方法。
样本表
CREATE TABLE #TEMP(CATAGORY_NAME VARCHAR(200),GROUP_NAME VARCHAR(200), FUNCTION_NAME VARCHAR(200),CASE_ID VARCHAR(200))
INSERT INTO #TEMP
SELECT 'Desbes Optivy', 'TESTING125', 'FAST CASH' , 'G01-TC00101'
UNION ALL
SELECT 'IBM Omron ADM3 (S1G366932)', 'VISA Chip (Single App)', 'FAST CASH', 'G03-TC00501'
UNION ALL
SELECT 'Wincor PC280 (S1A365305)', 'Abnormal SCB Card', 'Abnormal Fast cash', 'G28-TC11804'
UNION ALL
SELECT 'Wincor PC280 (S1A365305)', 'Abnormal VISA Magnetic', 'Abnormal Withdrawal', 'G30-TC15402'
<强> QUERY 强>
;WITH CTE AS
(
-- Here we get a unique id for each row
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT(0))) RNO,*
FROM #TEMP
)
,CTE2 AS
(
-- Hardcode the column names
SELECT 'CATAGORY NAME' COL,CATAGORY_NAME,RNO
FROM CTE
UNION ALL
SELECT 'GROUP NAME',GROUP_NAME,RNO
FROM CTE
UNION ALL
SELECT 'FUNCTION NAME',FUNCTION_NAME,RNO
FROM CTE
UNION ALL
SELECT 'CASE ID',CASE_ID,RNO
FROM CTE
)
-- Select all the data and order by a column according to our logic
-- Row number gets you unique id for each rows
SELECT * FROM CTE2
ORDER BY RNO,
CASE WHEN COL = 'CATAGORY NAME' THEN 1
WHEN COL = 'GROUP NAME' THEN 2
WHEN COL = 'FUNCTION NAME' THEN 3
WHEN COL = 'CASE ID' THEN 4
END
编辑2:
试试吧。
;WITH CTE AS
(
SELECT ROW_NUMBER() OVER(ORDER BY TEST_CASE.CID) RNO,
CATAGORY_NAME,GROUP_NAME,FUNCTION_NAME,
'G'+TEST_CASE.GID + '-TC'+TEST_CASE.CASE_ID CASE_ID,
CASE_NAME,CASE_NAME_TH,EXPECT_RESULT,EXPECT_DETAIL,
INTERFACE,RESULT,TEST_DATE,TEST_BY,REMARK
FROM TEST_CASE
LEFT OUTER JOIN TEST_CATAGORY
ON TEST_CASE.CID = TEST_CATAGORY.CID
AND TEST_CASE.SID = TEST_CATAGORY.SID
LEFT OUTER JOIN TEST_GROUP ON TEST_CASE.GID = TEST_GROUP.GID AND
TEST_CASE.CID = TEST_GROUP.CID WHERE TEST_CASE.SID = 1
)
,CTE2 AS
(
-- Hardcode the column names
SELECT 'CATAGORY NAME' COL,CATAGORY_NAME,RNO
FROM CTE
UNION ALL
SELECT 'GROUP NAME',GROUP_NAME,RNO
FROM CTE
UNION ALL
SELECT 'FUNCTION NAME',FUNCTION_NAME,RNO
FROM CTE
UNION ALL
SELECT 'CASE ID',CASE_ID,RNO
FROM CTE
)
-- Select all the data and order by a column according to our logic
-- Row number gets you unique id for each rows
SELECT * FROM CTE2
ORDER BY RNO,
CASE WHEN COL = 'CATAGORY NAME' THEN 1
WHEN COL = 'GROUP NAME' THEN 2
WHEN COL = 'FUNCTION NAME' THEN 3
WHEN COL = 'CASE ID' THEN 4
END
答案 1 :(得分:1)
我将使用Cross apply
取消结果
SELECT column_name,
data
FROM result
CROSS apply (VALUES ('CATAGORY_NAME',CATAGORY_NAME),
('GROUP_NAME',GROUP_NAME),
('FUNCTION_NAME',FUNCTION_NAME),
('CASE_ID',CONVERT(VARCHAR(50), CASE_ID))) cs(column_name, data)
<强> SQLFIDDLE DEMO 强>