2016-06-01 15 views
7

Ho una tabella con i valori come questi:Come ottenere valori alternati per ROW_NUMBER()?

Name Order Innings 
Suresh 1   1 
Ramesh 2   1 
Sekar  3   1 
Raju  1   2 
Vinoth 2   2 
Ramu  3   2 

Voglio che il risultato sia simile a questo:

1stInn 2ndInn Order 
Suresh Raju  1 
Ramesh Vinoth 2 
Sekar Ramu  3 

ho ottenuto il risultato usando ROW_NUMBER() in SQL Server.

Voglio lo stesso risultato in SQL Compact, ma non posso usare ROW_NUMBER() in SQL Compact.

sto usando SQL Versione compatta - 4.0.8482.1

Come posso ottenere il risultato?

+2

Mostraci il tuo interrogazione corrente (SQL Server). – jarlh

risposta

8

Perché è necessario ROW_NUMBER()? è possibile utilizzare l'aggregazione condizionale mediante CASE EXPRESSION:

SELECT MAX(CASE WHEN t.innings = 1 THEN t.name END) as 1stInn, 
     MAX(CASE WHEN t.innings = 2 THEN t.name END) as 2sndInn, 
     t.Order 
FROM YourTable t 
GROUP BY t.order 
3

semplice pivot darà il risultato simile

DECLARE @Table1 TABLE 
    (Name varchar(6), [Order] int, Innings int) 
; 

INSERT INTO @Table1 
    (Name , [Order] , Innings) 
VALUES 
    ('Suresh', 1, 1), 
    ('Ramesh', 2, 1), 
    ('Sekar', 3, 1), 
    ('Raju', 1, 2), 
    ('Vinoth', 2, 2), 
    ('Ramu', 3, 2) 
; 
select [1] AS '1stinn',[2] AS '2ndinn',[order] from(
select Name , [Order] , Innings from @Table1)T 
PIVOT (MAX(NAME) FOR Innings IN ([1],[2]))PVT 
+0

Grazie per la tua risposta @ mohan111. SQL Compact non accetta PIVOT. – DineshDB

+0

welcome @DineshDB – mohan111

+0

Ma funziona perfettamente in SQL Server. +1 – DineshDB

Problemi correlati