Here are my two queries
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
And this is the unpivot one
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
The output of the queries are like this:
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
And
Value Code
0 N1
0 N2
0 N3
0 N4
0 N5
0 N6
0 N7
0 N8
0 N9
The output must be like this
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
Both Queries put out same number of rows but not columns, Is it possible to join this two? I tried the union but it has problems that I cant solve
Thanks in advance!
If your data from the two queries is joinable based on the numeric portion of the
CodeandPositionthen you might be able to use something like this: