我有一个类似的查询:
SELECT NUM,
PARAM_1
FROM STOCK_CARD_PARAMETER WITH(NOLOCK)
WHERE FK_STOCK_CARD IN (SELECT FK_sTOCK_CARD
FROM STOCK_BARCODE WITH(NOLOCK)
WHERE BARCODE = '2002002232364' )
它返回:
NUM PARAM_1
1 İNDİRİMSİZ
2 SEZON
3 UNISEXYETISKIN
我必须得到这样的结果。 (并排)
İNDİRİMSİZ SEZON UNISEXYETISKIN
为此,我执行两次相同的查询,如下所示:
INDIRIM = (SELECT PARAM_1
FROM STOCK_CARD_PARAMETER WITH(NOLOCK)
WHERE FK_STOCK_CARD IN (SELECT FK_sTOCK_CARD
FROM STOCK_BARCODE WITH(NOLOCK)
WHERE BARCODE = @BARKOD AND NUM = 1)),
STATU = ( SELECT PARAM_1
FROM STOCK_CARD_PARAMETER WITH(NOLOCK)
WHERE FK_STOCK_CARD IN (SELECT FK_sTOCK_CARD
FROM STOCK_BARCODE WITH(NOLOCK)
WHERE BARCODE = @BARKOD AND NUM = 2))
NUM
列是此处的关键。如何将这两个查询组合在一起并获得结果?
答案 0 :(得分:3)
使用SELECT [1], [2], [3]
FROM
(SELECT NUM, PARAM_1
FROM table) AS SourceTable
PIVOT
(
MAX(PARAM_1)
FOR NUM IN ([1], [2], [3])
) AS PivotTable;
WITH C(NUM, PARAM_1) AS(
SELECT 1 , N'İNDİRİMSİZ' UNION ALL
SELECT 2 , N'SEZON' UNION ALL
SELECT 3 , N'UNISEXYETISKIN'
)
SELECT [1], [2], [3]
FROM
(SELECT NUM, PARAM_1
FROM C) AS SourceTable
PIVOT
(
MAX(PARAM_1)
FOR NUM IN ([1], [2], [3])
) AS PivotTable;
带输出的示例数据
İNDİRİMSİZ SEZON UNISEXYETISKIN
输出
BitmapFont