2016-06-01 1 views
7

J'ai une table avec des valeurs comme celles-ci:Comment obtenir des valeurs alternatives pour ROW_NUMBER()?

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

Je veux que le résultat soit comme ceci:

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

Je suis le résultat en utilisant ROW_NUMBER() dans SQL Server.

Je veux le même résultat dans SQL Compact, mais je ne peux pas utiliser ROW_NUMBER() dans SQL Compact.

J'utilise SQL Version compacte - 4.0.8482.1

Comment puis-je obtenir le résultat?

+2

Montrez-nous votre requête en cours (SQL Server). – jarlh

Répondre

8

Pourquoi avez-vous besoin de ROW_NUMBER()? vous pouvez utiliser l'agrégation conditionnelle à l'aide 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

simple, Pivot donnera le même résultat

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

Merci pour votre réponse @ mohan111. SQL Compact n'accepte pas le PIVOT. – DineshDB

+0

welcome @DineshDB – mohan111

+0

Mais cela fonctionne très bien dans SQL Server. +1 – DineshDB