简体   繁体   中英

How to create a sequence on selected values in MySQL?

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.

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

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`;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM