Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql sql将产品所有者表分组,其中包含一列作为主要所有者和连接的次要所有者_Mysql_Sql_Group By_Concatenation - Fatal编程技术网

Mysql sql将产品所有者表分组,其中包含一列作为主要所有者和连接的次要所有者

Mysql sql将产品所有者表分组,其中包含一列作为主要所有者和连接的次要所有者,mysql,sql,group-by,concatenation,Mysql,Sql,Group By,Concatenation,我和他们的主人有一张产品表。每个所有者都位于自己的行中,其所有者类型为“主要”或“次要”。并非每个产品都有第二所有者 我需要得到一个按产品分组的表,主所有者在一列中,所有次要所有者连接在第二列中。如果一个产品有多个主要所有者,则应选择第一个所有者,其余所有者归次要所有者。如果产品没有主要所有者,那么它应该只选择第一个/任何第二个所有者 这是一个输入表: +---------+------------+----------+ | Product | Owner Type | Owner |

我和他们的主人有一张产品表。每个所有者都位于自己的行中,其所有者类型为“主要”或“次要”。并非每个产品都有第二所有者

我需要得到一个按产品分组的表,主所有者在一列中,所有次要所有者连接在第二列中。如果一个产品有多个主要所有者,则应选择第一个所有者,其余所有者归次要所有者。如果产品没有主要所有者,那么它应该只选择第一个/任何第二个所有者

这是一个输入表:

+---------+------------+----------+
| Product | Owner Type |  Owner   |
+---------+------------+----------+
| a       | primary    | one      |
| a       | secondary  | two      |
| a       | secondary  | three    |
| b       | primary    | four     |
| b       | secondary  | five     |
| c       | primary    | six      |
| d       | secondary  | seven    |
| e       | secondary  | eight    |
| e       | secondary  | nine     |
| f       | primary    | ten      |
| f       | primary    | eleven   |
| f       | secondary  | twelve   |
| f       | secondary  | thirteen |
+---------+------------+----------+
预期结果是:

+---------+---------------+--------------------------+
| Product | Primary Owner |     Secondary Owners     |
+---------+---------------+--------------------------+
| a       | one           | two, three               |
| b       | four          | five                     |
| c       | six           |                          |
| d       | seven         |                          |
| e       | eight         | nine                     |
| f       | ten           | eleven, twelve, thirteen |
+---------+---------------+--------------------------+
如果您注意到,产品d和e没有主要所有者,因此它会选择第一个次要所有者,然后不再将其包含在次要所有者列中。类似于具有两个主要所有者的产品f

我知道如何按产品分组,并使用FOR XML PATH连接行/字段。在小组中,我知道如何选择所有者类型为主的第一个产品。我无法理解的是,选择第一个主要所有者并将其从次要所有者列中排除和/或选择第一个次要所有者(如果没有主要所有者并将其从次要所有者列中排除)所需的逻辑

我甚至不知道从哪里开始使用SQL


有什么想法吗?

一种方法是指定行号,对所有者的行进行优先级排序。然后将第一行作为主要所有者,并将其他组作为次要所有者

select product
,max(case when rnum=1 then owner end) as primary_owner
,group_concat(case when rnum<>1 then owner end order by rnum) as secondary_owners
from (select product,owner_type,owner,
      @rn:=case when @prev_product=product then @rn+1 else 1 end as rnum,
      @prev_product:=product 
      from tablename 
      cross join (select @rn:=0,@prev_product:='',@prev) r
      order by product,owner_type='Primary',owner
     ) t
group by product
order by 1

如何选择第一行?是否有列指定顺序?@vkp不重要。不需要排序。只需先选择。如果需要排序,则可以按所有者列进行排序。我要试试这个。谢谢然后我必须弄清楚它实际上是如何工作的,因为这引入了很多我不熟悉的东西。所以这是我现在需要的,但现在我们被迫使用SQL Server 2014,它没有group_concat。有什么想法吗?