Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Need to select top 5 rows for each id based on desc values of a particular column , value and find the subtotal of that column . for e.g (Tried creating the situation , actual table structure is large )

ID  VALUE   
1   2       
1   3       
1   4       
1   5       
1   6       
1   7       
1   8       
2   9       
2   10      
2   11      
2   12      
2   13      
2   14  

Output Expected

ID     VALUE
1        8
1        7
1        6
1        5
1        4
TOTAL   30
2       14
2       13
2       12
2       11
2       10
TOTAL   60

I could select top 5 rows using a code like this ;

@cust_rank := IF(@current_cust = id, @cust_rank + 1, 1) AS cust_rank,
                 @current_cust := id

and then selecting top 5

Also ,I could subtotal using code like this ;

SELECT id, value FROM source
UNION
SELECT NULL,SUM(value) FROM source
GROUP BY id ORDER BY id; 

I need to merge both requirements .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
235 views
Welcome To Ask or Share your Answers For Others

1 Answer

SELECT CAST(id as CHAR(50)) Id, value
FROM (SELECT id , value ,
             IF(@lastid=(@lastid:=id), @auto:=@auto+1, @auto:=1) indx 
      FROM source, (SELECT @lastid:=0, @auto:=1) A 
      ORDER BY id,value desc)as A  
WHERE indx <= 5

Output

Id  value
1   8
1   7
1   6
1   5
1   4
2   14
2   13
2   12
2   11
2   10

2nd Query

SELECT 'Total', SUM(value)
FROM (SELECT id , value ,
             IF(@lastid=(@lastid:=id), @auto:=@auto+1, @auto:=1) indx 
      FROM source, (SELECT @lastid:=0, @auto:=1) A 
      ORDER BY value desc)as A  
WHERE indx <= 5 
GROUP BY id ;

Output

Total   SUM(value)
Total   30
Total   60

Merged Query:

Select 
CASE
    WHEN indx =6 THEN "Total"
    ELSE id
END as ID,value
from
(
  select id,value,
IF(@lastid=(@lastid:=id), @auto:=@auto+1, @auto:=1) indx 
FROM
(
SELECT CAST(id as CHAR(50)) Id, value
FROM (SELECT id , value ,
             IF(@lastid=(@lastid:=id), @auto:=@auto+1, @auto:=1) indx 
      FROM source, (SELECT @lastid:=0, @auto:=1) A 
      ORDER BY id,value desc)as A  
WHERE indx <= 5
UNION
SELECT CAST(id as CHAR(50))as id, SUM(value)as value
FROM (SELECT id , value ,
             IF(@lastid1=(@lastid1:=id), @auto1:=@auto1+1, @auto1:=1) indx 
      FROM source, (SELECT @lastid1:=0, @auto1:=1) A 
      ORDER BY value desc)as A  
WHERE indx <= 5 
GROUP BY id)as output ,(SELECT @lastid:=0, @auto:=1) A ORDER BY id) as output1

Output

ID    value
1       8
1       7
1       6
1       5
1       4
Total   30
2       10
2       11
2       12
2       13
2       14
Total   60

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...