0

I have a select like this:

SELECT persistent_id, artist, name, play_count FROM tracks ORDER BY play_count DESC

Where the result is this:

7FE074BC7C6429F3    artist1 track1  88
EC3CFAF957357763    artist2 track2  87
E40063C38F3010B2    artist3 track6  49
B09423DCE75A0908    artist2 track3  43
1F6E5B2E1FC1CC85    artist2 track4  15
2BB3103CABB9B4DC    artist3 track5  6

How could I rewrite this query so I could specify that the rows so I can specify (using persistent_id) that B09423DCE75A0908 + 1F6E5B2E1FC1CC85, and also E40063C38F3010B2 + 2BB3103CABB9B4DC should be summed? So instead the result would be:

7FE074BC7C6429F3    artist1 track1  88
EC3CFAF957357763    artist2 track2  87
B09423DCE75A0908    artist2 track3  58
E40063C38F3010B2    artist3 track6  55

I tried to do something like:

sum(DISTINCT CASE WHEN persistent_id = 'B09423DCE75A0908' THEN play_count END) AS plays1, 
sum(DISTINCT CASE WHEN persistent_id = '1F6E5B2E1FC1CC85' THEN play_count END) AS plays2

then try to sum plays1 and plays2 as play_count but I didn't get further than putting the play_count of those two persistent_ids on the same row.

2
  • Perhaps your question could be improved if you explain your overall goal a bit more. Are you trying to get the play count by artist?
    – trademark
    Jan 28, 2021 at 20:15
  • @trademark The goal is to combine the plays based on data that is not in the database (in this case the are the same tracks but have different names), the duplicates will always be those specific persistent_ids Jan 28, 2021 at 20:36

2 Answers 2

1

seems syou need a case on persistent_id

SELECT case when persistent_id  IN ('B09423DCE75A0908'  , '1F6E5B2E1FC1CC85')  
         THEN 'B09423DCE75A0908'
         when persistent_id  IN ( 'E40063C38F3010B2','2BB3103CABB9B4DC ')   
            THEN 'E40063C38F3010B2'  ELSE persistent_id END persistent_id
            min(artist), min(name), sum(play_count )
FROM tracks 
GROUP BY persistent_id
ORDER BY play_count DESC

could be that for the most older mysql version you must repeat the code for aggregation key in group by

SELECT case when persistent_id  IN ('B09423DCE75A0908'  , '1F6E5B2E1FC1CC85')  
         THEN 'B09423DCE75A0908'
         when persistent_id  IN ( 'E40063C38F3010B2','2BB3103CABB9B4DC ')   
            THEN 'E40063C38F3010B2'  ELSE persistent_id END persistent_id
            min(artist), min(name), sum(play_count )
FROM tracks 
GROUP BY ase when persistent_id  IN ('B09423DCE75A0908'  , '1F6E5B2E1FC1CC85')  THEN 'B09423DCE75A0908'
         when persistent_id  IN ( 'E40063C38F3010B2','2BB3103CABB9B4DC ')   THEN 
            THEN 'E40063C38F3010B2'  ELSE persistent_id END
ORDER BY play_count DESC
9
  • Yout also need the case when in the group by.
    – Luuk
    Jan 28, 2021 at 20:13
  • the group by normally should use the alias .. (at least in most recent version)
    – ScaisEdge
    Jan 28, 2021 at 20:15
  • I have "Microsoft SQL Server 2017", which is indeed not the most recent version...
    – Luuk
    Jan 28, 2021 at 20:16
  • mysql ... not SQL SERVER .. are not the same
    – ScaisEdge
    Jan 28, 2021 at 20:17
  • BTW: what is the current latest, most recent, version? I just tested with MySQL 8.0.22 and it failed .
    – Luuk
    Jan 28, 2021 at 20:42
0
SELECT 
   CASE WHEN persistent_id IN ('B09423DCE75A0908','1F6E5B2E1FC1CC85','E40063C38F3010B2') 
      THEN 'B09423DCE75A0908' else persistent_id END as persitent_id,
   SUM(play_count)
FROM tracks 
ORDER BY play_count DESC
GROUP BY CASE WHEN persistent_id IN ('B09423DCE75A0908','1F6E5B2E1FC1CC85','E40063C38F3010B2') 
              THEN 'B09423DCE75A0908' else persistent_id END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.