March 11, 2016 at 12:45 am
I'm having issue to move data to new row for my txt output.
sample data
resultA resultB resultC resultD
chicago ID123 20pax ID123
austria ID999 90pax ID999
Spain ID777 10pax ID777
i would like to make it group by row.
expexted date
result
chicago
20pax
austria
90pax
spain
10pax
is that possible?
March 11, 2016 at 5:08 am
SELECT resultA
FROM YourTable
UNION ALL
SELECT resultC
FROM YourTable;
If that's not what you need, then please post a CREATE TABLE statement for the table, INSERT statements with sample data, expected results, and an explanation.
March 13, 2016 at 8:07 pm
I tried that way but not in order.
March 14, 2016 at 12:22 am
This looks like a simple UNPIVOT requirement
CREATE TABLE #temp (
ID VARCHAR(100)
, ResultA VARCHAR(100)
, ResultB VARCHAR(100)
);
INSERT INTO #temp ( ID, ResultA, ResultB )
SELECT src.ID, src.ResultA, src.ResultB
FROM (VALUES ( 'ID123', 'chicago', '20pax' )
, ( 'ID999', 'austria', '90pax' )
, ( 'ID777', 'Spain' , '10pax' )
) AS src( ID, ResultA, ResultB );
SELECT Value
FROM
(
SELECT [ID]
, [ResultA]
, [ResultB]
FROM #temp
) AS srcData
UNPIVOT
(
Value FOR colName IN ( [ResultA], [ResultB] )
) AS rslt
ORDER BY ID;
March 14, 2016 at 1:10 am
I'm getting this error.
Any idea?
The type of column "" conflicts with the type of other columns specified in the UNPIVOT list.
March 14, 2016 at 3:08 am
girl_bj (3/14/2016)
The type of column "" conflicts with the type of other columns specified in the UNPIVOT list.
The datatypes of your source columns does not match. Find the biggest one, and cast the others to match that.
If you paste exactly what you typed above into Google, you will see a number of answers to your issue.
March 14, 2016 at 6:53 am
;WITH sample_data (resultA, resultB, resultC, resultD) AS (
SELECT 'chicago', 'ID123', '20pax', 'ID123' UNION ALL
SELECT 'austria', 'ID999', '90pax', 'ID999' UNION ALL
SELECT 'Spain', 'ID777', '10pax', 'ID777')
SELECT x.newcol1, x.newcol2
FROM sample_data
CROSS APPLY (
VALUES (resultA, resultB, 1),
(resultC, resultD, 2)
) x (newcol1, newcol2, OrderBy)
ORDER BY newcol2, OrderBy
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 14, 2016 at 9:29 pm
Thanks.
Is it possible to include in between new data?
Newrecord
chicago
20pax
Newrecord
Spain
10pax
Newrecord
austria
90pax
March 15, 2016 at 2:29 am
girl_bj (3/14/2016)
Thanks.Is it possible to include in between new data?
Newrecord
chicago
20pax
Newrecord
Spain
10pax
Newrecord
austria
90pax
Sure:
;WITH sample_data (resultA, resultB, resultC, resultD) AS (
SELECT 'chicago', 'ID123', '20pax', 'ID123' UNION ALL
SELECT 'austria', 'ID999', '90pax', 'ID999' UNION ALL
SELECT 'Spain', 'ID777', '10pax', 'ID777')
SELECT x.newcol1 --, x.newcol2
FROM sample_data
CROSS APPLY (
VALUES
('Newrecord', resultB, 0),
(resultA, resultB, 1),
(resultC, resultB, 2)
) x (newcol1, newcol2, OrderBy)
ORDER BY newcol2, OrderBy
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply