0

为了生成“生产报告”,我需要将许多表连接在一起,并找出每批生产的产品线数量的总和,以及每条生产线生产的最新活动是什么。

我可以在单独的 SQL 查询中汇总数据并找出 MAX 活动,但是当我加入它们时,查询显示错误。所以你能帮帮我吗?非常感谢。

我的表格如下:

tbl_pdtn_startup
pdtn_st_id | pd_id | pdtn_st_date
   3       |  001  |  3/9/12
   4       |  002  |  4/9/12

tbl_pdtn_sizecolor
pdtn_szcl_id | pdtn_st_id | pdtn_st_color | pdtn_st_size | pdtn_st_qty_est
    23       |    3       |   Blue        |    S         |    100
    26       |    3       |   Pink        |    S         |    100
    27       |    3       |   Green       |    S         |    100
    28       |    4       |   Blue        |    M         |    200
    30       |    4       |   Green       |    M         |    200


tbl_production
pdtn_id | pdtn_szcl_id | pdtn_st_id | pdtn_date | pd_making_id | lb_id
  24    |     23       |    3       |   4/9/12  |    1         |  12
  26    |     23       |    3       |   5/9/12  |    2         |  12
  27    |     26       |    3       |   4/9/12  |    1         |  15
  28    |     27       |    3       |   5/9/12  |    1         |  15 <--MAX(pdtn_date)
  29    |     28       |    4       |   4/9/12  |    4         |  15
  30    |     30       |    4       |   6/9/12  |    5         |  12 <--MAX(pdtn_date)

tbl_product
  pd_id | Name
   001  | product A
   002  | product B

tbl_pdWk_process
 pd_making_id | wk_stage | pd_id | cost
     1        | step 1   |  001  |  12
     2        | step 2   |  001  |  15
     3        | step 1   |  003  |  12
     4        | step 1   |  002  |  18
     5        | step 2   |  002  |  10

tbl_labor
lb_id | lb_name
 12   | john
 15   | Mary

我需要如下所示的输出:

pd_id | pd_name  | pdtn_st_date | sum(pdtn_st_qty_est)*|MAX(pdtn_date)**| wk_stage | lb_name
 001  | product A|   3/9/12     |   300                |   5/9/12       |  step 1  |  Mary
 002  | product B|   4/9/12     |   400                |   6/9/12       |  step 2  | John

注意
* Sum(pdtn_st_qty_est) 来自同一 pdtn_st_id (production_startup_id) 中所有 pdtn_st_qty_est 的总和(例如:- 蓝色=100,粉红色=100,绿色=100,pdtn_st_id = 3)

** MAX(pdtn_date) 来自最新工人为每个产品批次所做的活动(例如:- 5/9/12 来自 pdtn_st_id = 3 )

以下是我尝试将 3 个电源表连接在一起但失败的尝试。

SELECT U.pd_id,  A.SumOfpdtn_st_qty_est, S.MaxOfpdtn_date
FROM (tbl_pdtn_startup AS U INNER JOIN 
    [SELECT  Sum(tbl_pdtn_sizecolor.pdtn_st_qty_est) AS SumOfpdtn_st_qty_est FROM tbl_pdtn_sizecolor GROUP BY tbl_pdtn_sizecolor.pdtn_st_id]. AS A ON U.pdtn_st_id = A.pdtn_st_id) 
         INNER JOIN 
        [SELECT Max(tbl_production.pdtn_date) AS MaxOfpdtn_date FROM tbl_production GROUP BY tbl_production.pdtn_st_id]. AS S ON (A.pdtn_szcl_id = S.pdtn_szcl_id) AND (U.pdtn_st_id = S.pdtn_st_id)



我的下一次尝试:
我想出了进行单独查询并将它们连接在一起的想法,它可以产生我想要的部分数据。现在只有 2 个数据我无法正确检索。分别是 lb_name 和 wk_stage。那么任何人都可以帮助我。非常感谢。

q_maxdate

SELECT tbl_pdtn_startup.pdtn_st_id, tbl_pdtn_startup.pd_id, Max(tbl_production.pdtn_date) AS MaxOfpdtn_date, tbl_product.pd_name, tbl_pdtn_startup.pdtn_st_pdNote, tbl_pdtn_startup.pdtn_st_date
FROM (((tbl_pdtn_startup INNER JOIN tbl_pdtn_sizecolor ON tbl_pdtn_startup.pdtn_st_id=tbl_pdtn_sizecolor.pdtn_st_id) INNER JOIN tbl_production ON tbl_pdtn_sizecolor.pdtn_szcl_id=tbl_production.pdtn_szcl_id) INNER JOIN tbl_product ON tbl_pdtn_startup.pd_id=tbl_product.pd_id) INNER JOIN tbl_pdWk_process ON (tbl_production.pd_making_id=tbl_pdWk_process.pd_making_id) AND (tbl_product.pd_id=tbl_pdWk_process.pd_id)
GROUP BY tbl_pdtn_startup.pdtn_st_id, tbl_pdtn_startup.pd_id, tbl_product.pd_name, tbl_pdtn_startup.pdtn_st_pdNote, tbl_pdtn_startup.pdtn_st_date;

然后我在这个查询中加入了它......

SELECT q_maxdate.pdtn_st_id, Sum(tbl_pdtn_sizecolor.pdtn_st_qty_est) AS SumOfpdtn_st_qty_est, q_maxdate.MaxOfpdtn_date, q_maxdate.pd_name, q_maxdate.pdtn_st_pdNote, q_maxdate.pd_id, q_maxdate.pdtn_st_date
FROM q_maxdate INNER JOIN tbl_pdtn_sizecolor ON q_maxdate.pdtn_st_id = tbl_pdtn_sizecolor.pdtn_st_id
GROUP BY q_maxdate.pdtn_st_id, q_maxdate.MaxOfpdtn_date, q_maxdate.pd_name, q_maxdate.pdtn_st_pdNote, q_maxdate.pd_id, q_maxdate.pdtn_st_date;
4

1 回答 1

0

这是示例 SQL。您的劳工姓名似乎与您的样本数据不匹配。

SELECT tbl_pdtn_startup.pd_id,
       tbl_product.name,
       tbl_pdtn_startup.pdtn_st_date,
       tbl_pdwk_process.wk_stage,
       tbl_labor.lb_name,
       SUM(tbl_pdtn_sizecolor.pdtn_st_qty_est) AS SumOfpdtn_st_qty_est,
       Max(tbl_production.pdtn_date)           AS MaxOfpdtn_date
FROM   (((tbl_production
          INNER JOIN tbl_labor
                  ON tbl_production.lb_id = tbl_labor.lb_id)
         INNER JOIN (tbl_pdtn_sizecolor
                     INNER JOIN tbl_pdtn_startup
                             ON tbl_pdtn_sizecolor.pdtn_st_id =
                                tbl_pdtn_startup.pdtn_st_id)
                 ON tbl_production.pdtn_szcl_id =
                    tbl_pdtn_sizecolor.pdtn_szcl_id)
        INNER JOIN tbl_pdwk_process
                ON tbl_production.pd_making_id = tbl_pdwk_process.pd_making_id)
       INNER JOIN tbl_product
               ON tbl_pdtn_startup.pd_id = tbl_product.pd_id
GROUP  BY tbl_pdtn_startup.pd_id,
          tbl_product.name,
          tbl_pdtn_startup.pdtn_st_date,
          tbl_pdwk_process.wk_stage,
          tbl_labor.lb_name; 
于 2012-09-04T13:19:25.627 回答