1

My query is :

SELECT TOP 2 trans_date_np FROM day_in_status_mcg ORDER BY day_in_id DESC

Result :

2070÷10÷02  ->row 1
2070÷10÷01  ->row 2

Can I get result like this :

column 1       column 2
2070÷10÷02     2070÷10÷02

I tried to use XML PATH. 'trans_date_np' is varchar.

2 Answers 2

2

If you know how many rows you have you can use the pivot statement.

SELECT [1], [2], [3], [4], [5]
FROM
(SELECT TOP 5 trans_data_np, ROW_NUMBER() over (order by day_in_id) as rowNumber  
    FROM day_in_status_mcg
    ORDER BY day_in_id
    ) AS SourceTable
PIVOT
(
min(trans_data_np)
FOR rowNumber IN ([1], [2], [3], [4], [5])
) AS PivotTable;
Sign up to request clarification or add additional context in comments.

2 Comments

If number of rows are unknown, then how can I get desired result ?
i'm afraid not without creating and executing the Statement dynamically. With this statement you can execute dynamic SQL.
0

You can use following query:-

SELECT MAX(trans_date_np) AS column1, (SELECT MAX(trans_date_np) 
                                       FROM day_in_status_mcg 
                                       WHERE trans_date_np< SELECT MAX(trans_date_np) 
                                                            FROM day_in_status_mcg) AS column2
FROM day_in_status_mcg;

I hope this will solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.