简体   繁体   English

按出现次数对两列进行分组,并在MYSQL中按时间排序

[英]Group two columns by occurrence and sort by time in MYSQL

I am working with a table that contains a list of uploaded files that will be processed by a Laser Cutter. 我正在使用一个表,其中包含将由Laser Cutter处理的上载文件列表。

To increase efficiency I need to list files that have the same material and colour in order of how many of the same combination occurs. 为了提高效率,我需要按照发生多少相同组合的顺序列出具有相同材料颜色的文件。 Then order that by the time it was sent. 然后在发送时订购。

Here is a very simplified table example: 这是一个非常简化的表示例:

文件表示例

From the example table above I want to achieve the following results: 从上面的示例表中我想实现以下结果:

6    File Number 6    Plastic    Red     9am
4    File Number 4    Plastic    Red     10am
5    File Number 5    Plastic    Red     10:30am
1    File Number 1    Card       Blue    9am
2    File Number 2    Card       Blue    9:30am
7    File Number 7    Plastic    Purple  8am
3    File Number 3    Card       Green   9am

So where both material and colour occur the most, the respective rows are at the top and within that, the earliest file is at the top. 因此,在材料和颜色最多的情况下,相应的行位于顶部,在其中,最早的文件位于顶部。

As you can see, red plastic occurs the most so is at the top. 正如您所看到的,红色塑料发生最多,因此位于顶部。 Single files are ordered by time only as seen with file 7 and 3. 单个文件按时间排序,如文件7和3所示。

Ideally I would like to perform this in one MYSQL query but given its complexity I'm not sure that will be possible. 理想情况下,我想在一个MYSQL查询中执行此操作,但鉴于其复杂性,我不确定这是否可行。 Perhaps storing the results and looping through arrays? 也许存储结果并循环遍历数组?

I have nearly managed to achieve this with this query: 我几乎设法通过此查询实现此目的:

SELECT * 
FROM propellor.pro_files 
GROUP BY material, colour 
ORDER BY count(*) DESC, 
   material DESC, 
   colour DESC, 
   sent DESC;

Although this seems to be ordered almost correctly it only returns single rows for each material/colour combination. 虽然这似乎几乎正确地排序,但它只返回每个材质/颜色组合的单行。

It's worth noting that the colour column can be null . 值得注意的是,颜色列可以为null

For an idea of how I'm grouping them, here's how it will look in practice: 为了了解我如何对它们进行分组,以下是它在实践中的表现:

分组示例

UPDATE: The material / colour groups are ordered correctly now but single files are ordered newest to oldest when I need it the other way around, I have tried playing with sent ASC / DESC but it only affects files in a group. 更新:材料/颜色组现在正确排序,但是当我需要时,单个文件从最新到最旧排序,我尝试使用发送的ASC / DESC但它只影响组中的文件。

排序示例

UPDATE 2: Here is my current full MYSQL query: 更新2:这是我当前的完整MYSQL查询:

SELECT * 
FROM propellor.pro_files

INNER JOIN propellor.pro_users ON propellor.pro_files.userid=propellor.pro_users.userid

INNER JOIN (
SELECT material, colour, count(*) AS occ 
FROM propellor.pro_files
GROUP BY material, colour
) 

AS mcCounts 
USING (material, colour)
WHERE status =1 AND laser=1
ORDER BY mcCounts.occ DESC,
material,
colour,
propellor.pro_files.timesent ASC
;

You need to calculate the counts separately, though it can all be done in a single query; 您需要单独计算计数,尽管它可以在一个查询中完成; this should do it: 这应该这样做:

SELECT pf.* 
FROM propellor.pro_files AS pf
INNER JOIN (
   SELECT material, colour, count(*) AS occ 
   FROM propellor.pro_files
   GROUP BY material, colour 
) AS mcCounts USING (material, colour)
ORDER BY mcCounts.occ DESC
   , material, colour
   , pf.sent DESC
;

Edit: Added material, colour to ORDER BY... for when two combinations have the same frequency. 编辑:添加材料,颜色为ORDER BY ...当两个组合具有相同的频率时。

You can try creating count columns in your sql select and then ordering by those counts 您可以尝试在sql select中创建计数列,然后按这些计数进行排序

select count(material) over partition by material) as materialCount
       , count(colour) over partition by colour) as colourCount
       , ..... other columns
from   propellor.pro_files
order by materialCount desc, colourcount desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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