i have a trouble when i want to create a csv From Table ..
here it’s my table sample…
ID HEADER_row Value_row
1 A 100
2 B 200
3 C 300
4 D 400
5 E 500
6 A 600
7 B 700
8 C 800
9 D 900
10 E 1000
and its the csv data model that i want to create, check this out guys:
A B C D E A B C D E
100 200 300 400 500 600 700 800 900 1000
and
A B C D E
1OO 2OO 3OO 4OO 5OO
600 700 800 900 1000
I Already try this for my first model problem…
SELECT STUFF(
(SELECT ',' + s.ElementName
FROM tableName s
FOR XML PATH('')),1,1,'')
UNION ALL
SELECT STUFF(
(SELECT ',' + s.Value
FROM tableName s
FOR XML PATH('')),1,1,'')
but it give me the result like this:
A B C D E A B C D E
100 200 300 400 500 600 700 800 900 1000
now i want to get the other result like this
A B C D E
1OO 2OO 3OO 4OO 5OO
600 700 800 900 1000
what kind of query i should use on my problem beside using pivot?
To split this data into separate rows you are going to include the use of
row_number()in the second query that gets theValue_row:By applying the
row_number()you can partition the data by theHeader_rowvalue to assign two separate number:See SQL Fiddle with Demo. This query gives the result:
Then you incorporate this into your existing query so the code will be:
See SQL Fiddle with Demo
This gives the result: