How to create a sequence on selected values in MySQL?

↘锁芯ラ 提交于 2020-01-17 02:52:31

问题


I'm trying to create a query which will select a team from a declared variable and then make the remaining teams "anonymous" by giving them generic brands and sequential IDs.

For example, if my dataset has 3 different team names (ABC, DEF, and GHI), but I would only like the true value of the 1 chosen team (ABC) displayed. Here is the skeleton of the query I'm working with:

SET @teamid = 123;

SELECT     CASE WHEN ID = @teamid
                THEN NAME
                ELSE 'Team' + ' - ' + SEQ
                END AS 'Team Name',
           SUM(TOTAL) AS 'Team Total'
FROM       TEAM
GROUP BY   1;

I would like the results to look something like this:

 Team Name:    Team Total:
 ABC           100
 Team - 1      50
 Team - 2      150

How can I go about creating a query which will create a unique number that I can replace the original team name with? I know I have to replace the "SEQ" portion of the case statement, but I'm not sure what exactly to replace it with. Also, it is important that each team has the same ID whether or not it is anonymous (so if team DEF has 50 rows, it should be shown as Team - 1 only as opposed to Team - 1-50) so that my groupings will work properly.

Thanks for the help.


回答1:


Use a user-defined variable that you increment. Also, MySQL uses CONCAT to concatenate strings, not +.

SET @teamid = 123;

SELECT     CASE WHEN ID = @teamid
                THEN NAME
                ELSE CONCAT('Team  - ', @SEQ)
                END AS 'Team Name',
           SUM(TOTAL) AS 'Team Total',
           @SEQ := CASE WHEN ID = @teamid 
                        THEN @SEQ
                        ELSE @SEQ + 1
                   END
FROM       TEAM
CROSS JOIN (SELECT @SEQ := 1) AS vars
GROUP BY   ID;

DEMO




回答2:


This should work. The inner query aliases the team names by only incrementing the counter when the name changes. The outer one uses those names to group on.

SET @teamid := [the id you want];
SET @prevName := "";
SET @team_i := 0;

SELECT `Team Name`, SUM(TOTAL) AS `Team Total`
FROM (
   SELECT @team_i := @team_i + IF(@prevName <>NAME, 1, 0) AS `teamIndex`
      , @prevName := NAME AS `OriginalName`
      , IF(ID = @teamid, NAME, CONCAT('Team  - ', @team_i)) AS `Team Name`
      , TOTAL
   FROM TEAM
   ORDER BY NAME) AS subQ
GROUP BY `Team Name`;

and actually, it can probably be reversed (and this one might be slightly faster, though you do end up with extra fields in the final results):

SET @teamid := [the id you want];
SET @prevName := "";
SET @team_i := 0;

SELECT @team_i := @team_i + IF(@prevName <>NAME, 1, 0) AS `teamIndex`
      , @prevName := NAME AS `OriginalName`
      , IF(ID = @teamid, NAME, CONCAT('Team  - ', @team_i)) AS `Team Name`
      , `Team Total`
FROM (
   SELECT `NAME`, SUM(TOTAL) AS `Team Total`
   FROM TEAM
   GROUP BY NAME) AS subQ
ORDER BY `NAME`;


来源:https://stackoverflow.com/questions/30336361/how-to-create-a-sequence-on-selected-values-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!