Skip to content

How to Group with Matching records and give name to the grp

Lokesh K

Asked Aug 29, 2018, 9:25 AM EDT1 answer
Share
LinkedInX
Hi,
i'm strucked with this place, where i need to group the matched records to a group and give name to the group like shown below.
 
Case 1:  
Id         Number         Occurrence
1            1234               New
2            2345               1234
3            3456               2345
4            4567               New
5            5678               4567 
 
Here...Numbers are mapped with Occurrence place 
 
Case 2
Id            Number           Occurrence
1               1234                    New
2               2345                   1234
3              3456                    1234
4              4567                    1234
 
Here.... also Numbers are mapped with Occurrence place
 
My Output should be like shown below for Case 1:
 
Solution for Case 1:
Id           Number        Occurrence      OccurrenceTimes
1           1234                 New                     P1
2           2345                1234                     P1
3           3456                2345                     P1
4           4567                New                      P2
5           5678                4567                     P2
 
 
Solution for Case 2
Id           Number        Occurrence      OccurrenceTimes
1           1234                   New                     P1
2           2345                   1234                    P1
3           3456                   1234                    P1
4           4567                   1234                    P1
 
Pls anyone suggest for above Solution Cases. Using linq or???
 
Regards,
Lokesh 
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Gopal Sharma
    0
    if your table 
     
    Id Number Occurrence
    1 1234 New
    2 2345 1234
    3 3456 1234
    4 4567 1234
     
    and you want the output like 
     
    Solution for Case 1:
    Id Number Occurrence OccurrenceTimes
    1 1234 New P1
    2 2345 1234 P1
    3 3456 2345 P1
    4 4567 New P2
    5 5678 4567 P2
     
    In MS SQL Server (suppose your table name is xtDummy)
     
     
    Select Id, Number, Occurrence,
    ROW_NUMBER()over(partition by Occurrence order by Occurrence) as 'OccurrenceTimes'
    from xtDummy
    order by Occurrence
     
    0