這裏是我的兩個查詢我怎樣才能加入這個2次的查詢?(有加盟選擇查詢和一個UNPIVOT查詢)
SELECT EWND.Position,
NKey = CASE WHEN ISNULL(Translation.Name, '') = '' THEN EWND.Name
ELSE Translation.Name END, Unit = EW_N_DEF.Units
FROM EWND
INNER JOIN EW_N_DEF ON EW_N_DEF.Nutr_No = EWND.Nutr_No
LEFT JOIN Translation ON Translation.CodeMain = EWND.Nutr_no
WHERE Translation.CodeTrans = 1
ORDER BY EWND.Position
這是逆透視一個
SELECT *
FROM
(SELECT N1,N2,N3,N4,N5,N6,N7,N8,N9,N10,N11,N12,N13,N14,N15,N16,N17,N18,N19,N20,N21,N22,N23,N24,N25,N26,N27,N28,N29,N30,N31,N32,N33,N34
FROM EWNVal WHERE Code=6035) Test
UNPIVOT
(Value FOR NUTCODE IN
(N1,N2,N3,N4,N5,N6,N7,N8,N9,N10,N11,N12,N13,N14,N15,N16,N17,N18,N19,N20,N21,N22,N23,N24,N25,N26,N27,N28,N29,N30,N31,N32,N33,N34)
)AS test
的輸出查詢是這樣的:
Position Name Unit
1 Calories
2 Protein g
3 Total Fat g
4 Total Carbs g
5 Calories from Fat
6 Saturated Fat g
7 Trans Fat g
8 Monounsaturated Fat g
9 Polyunsaturated Fat g
而且
Value Code
0 N1
0 N2
0 N3
0 N4
0 N5
0 N6
0 N7
0 N8
0 N9
輸出必須是這樣
1 Calories 0 N1
2 Protein g 0 N2
3 Total Fat g 0 N3
4 Total Carbs g 0 N4
5 Calories from Fat 0 N5
6 Saturated Fat g 0 N6
7 Trans Fat g 0 N7
8 Monounsaturated Fat g 0 N8
9 Polyunsaturated Fat g 0 N9
兩個詢問拿出相同數目的行而不是列,是否有可能加入這個兩?我嘗試了工會,但它有我無法解決的問題
在此先感謝!
可以提供具有預期結果的樣本數據嗎? –
@MANGKANOR這兩個記錄集以任何方式相互關聯嗎? – Taryn
我想是這樣,它是基於它的代碼連接的,位置1的N1等等 –