操作查询结果以显示按字段分组的MAX数据

时间:2009-10-06 05:20:47

标签: php mysql

这是我现有的查询:

$Pareto = mysql_query("SELECT
            zsca_open.tqs_batch_num AS 'Batch Number',
            ft_tests.test_name AS 'Test Name',
            (SUM(ft_stats.over_filter + ft_stats.under_filter)) AS 'Failures'
        FROM
            ((zsca_open INNER JOIN ft_header ON zsca_open.tqs_batch_num = ft_header.batch_code)
            INNER JOIN ft_tests ON ft_header.serial_hi = ft_tests.serial_hi) 
            INNER JOIN ft_stats ON ft_tests.serial_si = ft_stats.serial_si
        WHERE
            (((zsca_open.vendor)='ASE')
            AND ((zsca_open.lot_type)='T')
            AND ((zsca_open.status)='H')
            AND ((zsca_open.is_active)=1)
            AND ((zsca_open.rec_ver)=0)
            AND ((ft_header.test_step)='PROD')
            AND ((ft_header.status)='VIRGIN')
            AND ((ft_stats.stats_name)='spec_stats'))
        GROUP BY
            zsca_open.tqs_batch_num,
            ft_tests.test_name
        HAVING
             (((ft_tests.test_name) Not In ('SW Bin','HW Bin','Pass')))
        ORDER BY
            SUM(ft_stats.over_filter + ft_stats.under_filter) DESC") or die(mysql_error());

这是输出:

Batch Number TestName Failures <br>
0938AT5213   PCS_Pout 2865 <br>
0939AT5228 LB_PAE 921 <br>
0938AT5213 DCS_Pout 769 <br>
0939AT5228 LB_Pout_Nom 730 <br>
0939AT5228 LB_Pout 642 <br>
0938AT5213 DCS_PAE 517 <br>
0919AT3366 Iswitch 513 <br>
0938AT5213 PCS_PAE 481 <br>
0939AT5228 RX2_Insert_Loss 474 <br>
0939AT5228 HB_PAE 470 <br>
0939AT5228 HB_Pout 434 <br>
0939AT5228 HB_Pout_Nom 426 <br>
0919AT3366 ACLR1L_H1_846p6 399 

我需要的输出是每个批号仅显示MAX Failures。

Batch Number test_name Failures <br>
0938AT5213 PCS_Pout 2865 <br>
0939AT5228 LB_PAE 921<br>
0919AT3366 Iswitch 513

残友帮我查询?提前致谢

2 个答案:

答案 0 :(得分:0)

如果您不需要test_name字段,这非常简单。如果是这种情况,只需将整个查询包装在:

SELECT `Batch Number`, max(Failures) from (**YOURQUERYHERE**) as myquery GROUP BY Failures;

这将按批号给出最大失败次数,但不会为您提供与最大失败次数相对应的test_name。为了获取test_name,您需要执行子查询连接,如演示here。或here

在您的情况下,由于您的查询已经非常复杂,我建议将其转换为临时表:

CREATE TEMPORARY TABLE mytable (batch varchar(30), testname varchar(30), failures INT);
INSERT INTO mytable (**YOURQUERYHERE**);

然后您可以通过执行这些链接上建议的步骤之一来获得最大值。例如:

SELECT t1.batch, t1.testname, t1.failures
  FROM mytable AS t1,
       (SELECT batch, MAX(failures) AS maxfail
          FROM mytable
         GROUP BY batch) AS t2
  WHERE t2.batch = t1.batch
        AND t1.failures = t2.maxfail;

答案 1 :(得分:0)

在MySQL中,您可以嵌套查询,因此您可以将初始查询的结果视为临时表,如:

SELECT
        batches.'Batch Number',
        tests.'Test Name',
        batches.'Failures'
FROM
(
        SELECT
                zsca_open.tqs_batch_num AS 'Batch Number',
                MAX(SUM(ft_stats.over_filter + ft_stats.under_filter)) AS 'Failures'
        FROM
                ((zsca_open INNER JOIN ft_header ON zsca_open.tqs_batch_num = ft_header.batch_code)
                INNER JOIN ft_tests ON ft_header.serial_hi = ft_tests.serial_hi)
                INNER JOIN ft_stats ON ft_tests.serial_si = ft_stats.serial_si
        WHERE
                (((zsca_open.vendor)='ASE')
                AND ((zsca_open.lot_type)='T')
                AND ((zsca_open.status)='H')
                AND ((zsca_open.is_active)=1)
                AND ((zsca_open.rec_ver)=0)
                AND ((ft_header.test_step)='PROD')
                AND ((ft_header.status)='VIRGIN')
                AND ((ft_stats.stats_name)='spec_stats'))
        GROUP BY
                zsca_open.tqs_batch_num
        HAVING
                (((ft_tests.test_name) Not In ('SW Bin','HW Bin','Pass')))
) batches
INNER JOIN ft_header ON tests.'Batch Number' = ft_header.batch_code
INNER JOIN (
        SELECT
                ft_tests.serial_hi,
                ft_tests.test_name AS 'Test Name',
                SUM(ft_stats.over_filter + ft_stats.under_filter) AS 'Failures'
        FROM
                ft_tests
        GROUP BY
                ft_tests.serial_hi,ft_tests.test_name
) tests ON (ft_header.serial_hi = tests.serial_hi AND test.'Failures' = bartches.'Failures')

这是第一次猜测,但它本质上是写出你需要一次执行查询然后将SELECT语句视为表格的各种“视图”的结果。

这也可能效率很低,因此您可能需要考虑使用MySQL views等替代方法或在代码中进行最终聚合。

如果您想了解我的查询中使用的技术的更多信息,请查看MySQL网站上的this page