How to get values alternate for ROW_NUMBER()?
Asked Answered
R

2

6

I have a table with values like these:

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

I want the result be like this:

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

I got the result using ROW_NUMBER() in SQL Server.

I want the same result in SQL Compact, But I can't use ROW_NUMBER() in SQL Compact.

I'm using SQL Compact version - 4.0.8482.1

How can I get the result?

Remex answered 1/6, 2016 at 8:8 Comment(1)
Show us your current (SQL Server) query.Xenophanes
F
7

Why do you need ROW_NUMBER()? you can use conditional aggregation using 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
Foulard answered 1/6, 2016 at 8:15 Comment(0)
C
2

simple Pivot will give the similar result

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
Clypeus answered 1/6, 2016 at 10:17 Comment(2)
Thank you for your answer @mohan111. SQL Compact not accept the PIVOT.Remex
Al though this solution won't work on compact, it will work on SQL Server, so it can still help people, +1Foulard

© 2022 - 2024 — McMap. All rights reserved.