在SQL中使用JOIN语句会提供不正确的信息

时间:2015-08-10 16:23:16

标签: php sql sql-server join

这是我在这里的第一个问题所以我会尽力明白。

我正在尝试在Microsoft SQL中运行一个SQL语句(如果这很重要,我将把它翻译成php)并且我正在使用join语句。我将粘贴确切的代码,并尝试提供一个正在发生的事情的例子,以及我需要发生的事情。

SELECT
  WORK_ORDER.BASE_ID,
  OPERATION.WORKORDER_SUB_ID,
  WORK_ORDER.PART_ID,
  OPERATION.SETUP_HRS,
  OPERATION.RUN,
  OPERATION.RUN_TYPE,
  OPERATION.RUN_HRS,
  WORK_ORDER.DESIRED_QTY,
  WORK_ORDER.DESIRED_WANT_DATE,
  WORK_ORDER.COMMODITY_CODE
FROM
  WORK_ORDER
  JOIN OPERATION ON WORK_ORDER.BASE_ID = OPERATION.WORKORDER_BASE_ID
WHERE
  OPERATION.RESOURCE_ID = '280LASERS'
  AND OPERATION.STATUS='R'
ORDER BY
  DESIRED_WANT_DATE,
  OPERATION.WORKORDER_SUB_ID;

我得到的信息(简而言之)是:

+---------+-------------+-------------+-------------------------+----------------+------------------+-----------+--------------+----------+---------+
| BASE_ID |   PART_ID   | DESIRED_QTY |    DESIRED_WANT_DATE    | COMMODITY_CODE | WORKORDER_SUB_ID | SETUP_HRS |     RUN      | RUN_TYPE | RUN_HRS |
+---------+-------------+-------------+-------------------------+----------------+------------------+-----------+--------------+----------+---------+
| W146589 | 11000001722 | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               13 | 0.100     | 180.00000000 | PCS/HR   | 0.33    |
| W146589 | 11000001722 | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               16 | 0.150     | 180.00000000 | PCS/HR   | 0.66    |
| W146589 | 11000001722 | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               17 | 0.330     | 140.00000000 | PCS/HR   | 0.42    |
| W146589 | 11000001722 | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               19 | 0.150     | 240.00000000 | PCS/HR   | 0.49    |
| W146589 | 11000001722 | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               22 | 0.330     | 140.00000000 | PCS/HR   | 0.42    |
| W146589 | 11000001722 | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               23 | 0.330     | 140.00000000 | PCS/HR   | 0.42    |
| W146589 | 11000001722 | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               25 | 0.100     | 400.00000000 | PCS/HR   | 0.15    |
| W146589 | 11000001722 | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               30 | 0.330     | 30.00000000  | PCS/HR   | 3.93    |
| W146589 | 11000001722 | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               33 | 0.100     | 45.00000000  | PCS/HR   | 1.31    |
| W146589 | 11000001722 | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |                5 | 0.100     | 48.00000000  | PCS/HR   | 1.23    |
| W146589 | 11000001722 | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |                8 | 0.100     | 40.00000000  | PCS/HR   | 1.48    |
+---------+-------------+-------------+-------------------------+----------------+------------------+-----------+--------------+----------+---------+

以下是应该返回的内容:

 +---------+-------------+-------------+-------------------------+----------------+------------------+-----------+--------------+----------+---------+
 | BASE_ID |   PART_ID   | DESIRED_QTY |    DESIRED_WANT_DATE    | COMMODITY_CODE | WORKORDER_SUB_ID | SETUP_HRS |     RUN      | RUN_TYPE | RUN_HRS |
 +---------+-------------+-------------+-------------------------+----------------+------------------+-----------+--------------+----------+---------+
 | W146589 | F3702202    | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               13 | 0.100     | 180.00000000 | PCS/HR   | 0.33    |
*| W146589 | F3723301    | 118.0000    | 2015-05-13 00:00:00.000 | FLASH TECH     |               16 | 0.150     | 180.00000000 | PCS/HR   | 0.66    |
 | W146589 | F3723502    | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               17 | 0.330     | 140.00000000 | PCS/HR   | 0.42    |
*| W146589 | F3771901    | 118.0000    | 2015-05-13 00:00:00.000 | FLASH TECH     |               19 | 0.150     | 240.00000000 | PCS/HR   | 0.49    |
 | W146589 | F3864701    | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               22 | 0.330     | 140.00000000 | PCS/HR   | 0.42    |
 | W146589 | F3864702    | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               23 | 0.330     | 140.00000000 | PCS/HR   | 0.42    |
 | W146589 | F3703100    | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               25 | 0.100     | 400.00000000 | PCS/HR   | 0.15    |
*| W146589 | F3864501    | 118.0000    | 2015-05-13 00:00:00.000 | FLASH TECH     |               30 | 0.330     | 30.00000000  | PCS/HR   | 3.93    |
 | W146589 | 3704901-4   | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |               33 | 0.100     | 45.00000000  | PCS/HR   | 1.31    |
 | W146589 | 3704901-3   | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |                5 | 0.100     | 48.00000000  | PCS/HR   | 1.23    |
 | W146589 | 3704901-5   | 59.0000     | 2015-05-13 00:00:00.000 | FLASH TECH     |                8 | 0.100     | 40.00000000  | PCS/HR   | 1.48    |
 +---------+-------------+-------------+-------------------------+----------------+------------------+-----------+--------------+----------+---------+

正如您所看到的,第一个查询重复PART_IDDESIRED_QTYSUB_ID正在发生变化,这非常奇怪,让我脱掉头发。

我可以告诉你,重复的PART_IDDESIRED_QTY来自0 SUB_ID,这是唯一一个DESIRED_WANT_DATE,但我需要确保它只是拉来自具有预定日期的Work_Orders。

我试图删除DESIRED_WANT_DATE IS NOT NULL语句并且它没有更改输出,只是提供了更多Work_Orders。

我认为使用JOIN语句可能会出现问题,但我不确定如何提取所有必需的信息。我想如果我能够(JOIN ON WORK_ORDER.BASE_ID = OPERATION.WORKORDER_BASE_ID AND WORK_ORDER.SUB_ID = OPERATION.WORKORDER_SUB_ID)可以帮助它提取正确的信息,但我不知道如何以这种方式操纵SQL中的数据输出。

编辑:

要求的表格示例:

WORK_ORDER:

| ROWID | TYPE |  BASE_ID   | LOT_ID | SPLIT_ID | SUB_ID |  PART_ID   | GLOBAL_RANK | DESIRED_QTY | RECEIVED_QTY |       CREATE_DATE       |    DESIRED_RLS_DATE     |    DESIRED_WANT_DATE    | CLOSE_DATE | COSTED_DATE | STATUS | COPY_FROM_SPLIT_ID | ENGINEERED_BY |     ENGINEERED_DATE     | DRAWING_ID | DRAWING_REV_NO | PRODUCT_CODE | COMMODITY_CODE | FORWARD_SCHEDULE | POSTING_CANDIDATE | MAT_GL_ACCT_ID | LAB_GL_ACCT_ID | BUR_GL_ACCT_ID | SER_GL_ACCT_ID | VARIABLE_TABLE | SCHEDULE_GROUP_ID | SCHED_START_DATE | SCHED_FINISH_DATE | COULD_FINISH_DATE | EST_MATERIAL_COST | EST_LABOR_COST | EST_BURDEN_COST | EST_SERVICE_COST | ACT_MATERIAL_COST | ACT_LABOR_COST | ACT_BURDEN_COST | ACT_SERVICE_COST | REM_MATERIAL_COST | REM_LABOR_COST | REM_BURDEN_COST | REM_SERVICE_COST | MARKED_FOR_PURGE | PRINTED_DATE | DRAWING_FILE | WAREHOUSE_ID | WIP_VAS_REQUIRED | ALLOCATED_QTY | FULFILLED_QTY | DEF_LBL_FORMAT_ID | HARD_RELEASE_DATE | USER_1 | USER_2 |      USER_3      | USER_4 | USER_5 | USER_6 | USER_7 | USER_8 | USER_9 | USER_10 | UDF_LAYOUT_ID | DBR_TYPE | DBR_PRIORITY | DBR_CODE | WBS_CODE | WBS_PROJECT | WBS_CUST_ORDER_ID | MILESTONE_SEQ_NO | MILESTONE_SUB_ID | ECN_REVISION | EDI_BLANKET_FLAG | DISPATCHED | ORIG_STAGE_REVISION_ID |     STATUS_EFF_DATE     | ALLOW_ALT_PARTS | ALLOW_UPD_FROM_MST | ALLOW_UPD_FROM_LEG | UPDATE_FROM_REF | UPDATE_USER_ID | UPDATE_ENG_MASTER | UPDATE_DATE | SITE_ID | PROD_ORDER_TYPE | PLANNER_ID | INACTIVE |

|  7888 | M    | 0006-12096 |      0 |        0 |      0 | 0006-12096 |          50 | 1000.0000   | 0.0000       | 2010-05-19 13:54:51.137 | 2010-05-19 00:00:00.000 | 2010-05-19 00:00:00.000 | NULL       | NULL        | U      | NULL               | NULL          | 2010-05-19 00:00:00.000 | B43521-02  |             03 | CONTRACT     | CHROMOLOX      | N                | Y                 | NULL           | NULL           | NULL           | NULL           | NULL           | NULL              | NULL             | NULL              | NULL              | 34.66             | 26.74          | 188.64          | 290.00           | 0.00              | 0.00           | 0.00            | 0.00             | 34.66             | 26.74          | 188.64          | 290.00           | N                | NULL         | NULL         | NULL         | NULL             | 0.0000        | 0.0000        | NULL              | N                 | NULL   | NULL   | COSMETIC LEVEL 3 | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL    | NULL          | O        | NULL         | NULL     | NULL     | N           | NULL              | NULL             | NULL             | NULL         | N                | NULL       | NULL                   | 2013-07-29 00:00:00.000 | NULL            | Y                  | Y                  | NULL            | NULL           | NULL              | NULL        | GAR     | NULL            | NULL       | N        |

OPERATION:

| ROWID | WORKORDER_TYPE | WORKORDER_BASE_ID | WORKORDER_LOT_ID | WORKORDER_SPLIT_ID | WORKORDER_SUB_ID | SEQUENCE_NO |   RESOURCE_ID   | SETUP_HRS |     RUN      | RUN_TYPE | LOAD_SIZE_QTY | RUN_HRS | MOVE_HRS | TRANSIT_DAYS | SERVICE_ID | SCRAP_YIELD_PCT | SCRAP_YIELD_TYPE | FIXED_SCRAP_UNITS | MINIMUM_MOVE_QTY | CALC_START_QTY | CALC_END_QTY | COMPLETED_QTY | DEVIATED_QTY | ACT_SETUP_HRS | ACT_RUN_HRS | STATUS | SETUP_COMPLETED | SERVICE_BEGIN_DATE | CLOSE_DATE | OPERATION_TYPE | DRAWING_ID | DRAWING_REV_NO | OVERRIDE_QTYS | BEGIN_TRACEABILITY | CAPACITY_USAGE_MAX | CAPACITY_USAGE_MIN | TEST_ID | SPC_QTY | SCHED_START_DATE | SCHED_FINISH_DATE | COULD_FINISH_DATE | ISDETERMINANT | SETUP_COST_PER_HR | RUN_COST_PER_HR | RUN_COST_PER_UNIT | BUR_PER_HR_SETUP | BUR_PER_HR_RUN | BUR_PER_UNIT_RUN | SERVICE_BASE_CHG | BUR_PERCENT_SETUP | BUR_PERCENT_RUN | BUR_PER_OPERATION | EST_ATL_LAB_COST | EST_ATL_BUR_COST | EST_ATL_SER_COST | REM_ATL_LAB_COST | REM_ATL_BUR_COST | REM_ATL_SER_COST | ACT_ATL_LAB_COST | ACT_ATL_BUR_COST | ACT_ATL_SER_COST | EST_TTL_MAT_COST | EST_TTL_LAB_COST | EST_TTL_BUR_COST | EST_TTL_SER_COST | REM_TTL_MAT_COST | REM_TTL_LAB_COST | REM_TTL_BUR_COST | REM_TTL_SER_COST | ACT_TTL_MAT_COST | ACT_TTL_LAB_COST | ACT_TTL_BUR_COST | ACT_TTL_SER_COST | SPLIT_ADJUSTMENT | MILESTONE_ID | SCHEDULE_TYPE | MIN_SEGMENT_SIZE | PROTECT_COST | DRAWING_FILE | DISPATCHED_QTY | SERVICE_MIN_CHG | VENDOR_ID | VENDOR_SERVICE_ID | SERVICE_PART_ID | LAST_DISP_DATE | LAST_RECV_DATE | WAREHOUSE_ID | ALLOCATED_QTY | FULFILLED_QTY | LEAST_MIN_MOVE_QTY | MAX_GAP_PREV_OP | APPLY_CALENDAR | MAX_DOWNTIME | ACCUM_DOWNTIME | RUN_QTY_PER_CYCLE | USER_1 | USER_2 | USER_3 | USER_4 | USER_5 | USER_6 | USER_7 | USER_8 | USER_9 | USER_10 | UDF_LAYOUT_ID | NUM_MEM_TO_SCHED | SERVICE_BUFFER | MILESTONE_SUB_ID | POST_MILESTONE | PROJ_MILESTONE_OP | WBS_CODE | WBS_START_DATE | WBS_END_DATE | WBS_DURATION | MILESTONE_SEQ_NO | PRD_INSP_PLAN_ID | SETUP_INSPECT_REQ | RUN_INSPECT_REQ |     STATUS_EFF_DATE     | PRED_SUB_ID | PRED_SEQ_NO | SITE_ID |

| 13451 | M              | 0006-12096        |                0 |                  0 |                0 |          10 | 280LASERS       | 0.250     | 600.00000000 | PCS/HR   | NULL          | 1.67    | NULL     | 0.000        | NULL       | 0.00            | S                | 0.0000            | NULL             | 1000.0000      | 1000.0000    | 0.0000        | 0.0000       | 0.00          | 0.00        | U      | N               | NULL               | NULL       | NULL           | NULL       | NULL           | N             | N                  | NULL               | NULL               | NULL    | NULL    | NULL             | NULL              | NULL              | N             | 13.95             | 13.95           | 0.000000          | 98.42            | 98.42          | 0.000000         | 0.00             | 0.000             | 0.000           | 0.00              | 26.74            | 188.64           | 0.00             | 26.74            | 188.64           | 0.00             | 0.00             | 0.00             | 0.00             | 34.66            | 0.00             | 0.00             | 0.00             | 34.66            | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.0000           | NULL         |             0 | NULL             | N            | NULL         | 0.0000         | 0.00            | NULL      | NULL              | NULL            | NULL           | NULL           | NULL         | 0.0000        | 0.0000        | NULL               | NULL            | NULL           | NULL         | NULL           | NULL              | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL    | NULL          | NULL             | NULL           | NULL             | N              | N                 | NULL     | NULL           | NULL         | NULL         | NULL             | NULL             | N                 | N               | 2013-07-29 00:00:00.000 | NULL        | NULL        | GAR     |
| 13452 | M              | 0006-12096        |                0 |                  0 |                0 |          20 | 800-QC          | 0.000     | 0.00000000   | HRS/PC   | NULL          | 0.00    | NULL     | 0.000        | NULL       | 0.00            | S                | 0.0000            | NULL             | 1000.0000      | 1000.0000    | 0.0000        | 0.0000       | 0.00          | 0.00        | U      | N               | NULL               | NULL       | NULL           | NULL       | NULL           | N             | N                  | NULL               | NULL               | NULL    | NULL    | NULL             | NULL              | NULL              | N             | 0.00              | 0.00            | 0.000000          | 0.00             | 0.00           | 0.000000         | 0.00             | 0.000             | 0.000           | 0.00              | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.0000           | NULL         |             0 | NULL             | N            | NULL         | 0.0000         | 0.00            | NULL      | NULL              | NULL            | NULL           | NULL           | NULL         | 0.0000        | 0.0000        | NULL               | NULL            | NULL           | NULL         | NULL           | NULL              | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL    | NULL          | NULL             | NULL           | NULL             | N              | N                 | NULL     | NULL           | NULL         | NULL         | NULL             | NULL             | N                 | N               | 2013-07-29 00:00:00.000 | NULL        | NULL        | GAR     |
| 13453 | M              | 0006-12096        |                0 |                  0 |                0 |          30 | TRANSMATL       | 0.000     | 0.00000000   | HRS/PC   | NULL          | 0.00    | NULL     | 0.000        | NULL       | 0.00            | S                | 0.0000            | NULL             | 1000.0000      | 1000.0000    | 0.0000        | 0.0000       | 0.00          | 0.00        | U      | N               | NULL               | NULL       | NULL           | NULL       | NULL           | N             | N                  | NULL               | NULL               | NULL    | NULL    | NULL             | NULL              | NULL              | N             | 0.00              | 0.00            | 0.000000          | 0.00             | 0.00           | 0.000000         | 0.00             | 0.000             | 0.000           | 0.00              | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.0000           | NULL         |             0 | NULL             | N            | NULL         | 0.0000         | 0.00            | V00198    | 80011             | 7014A64H01      | NULL           | NULL           | NULL         | 0.0000        | 0.0000        | NULL               | NULL            | NULL           | NULL         | NULL           | NULL              | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL    | NULL          | NULL             | NULL           | NULL             | N              | N                 | NULL     | NULL           | NULL         | NULL         | NULL             | NULL             | N                 | N               | 2013-07-29 00:00:00.000 | NULL        | NULL        | GAR     |
| 13454 | M              | 0006-12096        |                0 |                  0 |                0 |          40 | Outside Service | 0.000     | 0.00000000   | DAYS/PC  | NULL          | 0.00    | NULL     | 7.000        | 80618      | 0.00            | S                | 0.0000            | NULL             | 1000.0000      | 1000.0000    | 0.0000        | 0.0000       | 0.00          | 0.00        | U      | N               | NULL               | NULL       | NULL           | NULL       | NULL           | N             | N                  | NULL               | NULL               | NULL    | NULL    | NULL             | NULL              | NULL              | N             | 0.00              | 0.00            | 0.290000          | 0.00             | 0.00           | 0.000000         | 0.00             | 0.000             | 0.000           | 0.00              | 0.00             | 0.00             | 290.00           | 0.00             | 0.00             | 290.00           | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.0000           | NULL         |             0 | NULL             | N            | NULL         | 0.0000         | 75.00           | V00207    | 80618             | NULL            | NULL           | NULL           | NULL         | 0.0000        | 0.0000        | NULL               | NULL            | NULL           | NULL         | NULL           | NULL              | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL    | NULL          | NULL             | NULL           | NULL             | N              | N                 | NULL     | NULL           | NULL         | NULL         | NULL             | NULL             | N                 | N               | 2013-07-29 00:00:00.000 | NULL        | NULL        | GAR     |
| 13455 | M              | 0006-12096        |                0 |                  0 |                0 |          50 | RECVMATL        | 0.000     | 0.00000000   | HRS/PC   | NULL          | 0.00    | NULL     | 0.000        | NULL       | 0.00            | S                | 0.0000            | NULL             | 1000.0000      | 1000.0000    | 0.0000        | 0.0000       | 0.00          | 0.00        | U      | N               | NULL               | NULL       | NULL           | NULL       | NULL           | N             | N                  | NULL               | NULL               | NULL    | NULL    | NULL             | NULL              | NULL              | N             | 0.00              | 0.00            | 0.000000          | 0.00             | 0.00           | 0.000000         | 0.00             | 0.000             | 0.000           | 0.00              | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.0000           | NULL         |             0 | NULL             | N            | NULL         | 0.0000         | 75.00           | NULL      | NULL              | NULL            | NULL           | NULL           | NULL         | 0.0000        | 0.0000        | NULL               | NULL            | NULL           | NULL         | NULL           | NULL              | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL    | NULL          | NULL             | NULL           | NULL             | N              | N                 | NULL     | NULL           | NULL         | NULL         | NULL             | NULL             | N                 | N               | 2013-07-29 00:00:00.000 | NULL        | NULL        | GAR     |
| 13456 | M              | 0006-12096        |                0 |                  0 |                0 |          60 | 800-QC          | 0.000     | 0.00000000   | HRS/PC   | NULL          | 0.00    | NULL     | 0.000        | NULL       | 0.00            | S                | 0.0000            | NULL             | 1000.0000      | 1000.0000    | 0.0000        | 0.0000       | 0.00          | 0.00        | U      | N               | NULL               | NULL       | NULL           | NULL       | NULL           | N             | N                  | NULL               | NULL               | NULL    | NULL    | NULL             | NULL              | NULL              | N             | 0.00              | 0.00            | 0.000000          | 0.00             | 0.00           | 0.000000         | 0.00             | 0.000             | 0.000           | 0.00              | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.0000           | NULL         |             0 | NULL             | N            | NULL         | 0.0000         | 75.00           | NULL      | NULL              | NULL            | NULL           | NULL           | NULL         | 0.0000        | 0.0000        | NULL               | NULL            | NULL           | NULL         | NULL           | NULL              | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL    | NULL          | NULL             | NULL           | NULL             | N              | N                 | NULL     | NULL           | NULL         | NULL         | NULL             | NULL             | N                 | N               | 2013-07-29 00:00:00.000 | NULL        | NULL        | GAR     |
| 13457 | M              | 0006-12096        |                0 |                  0 |                0 |          70 | TRANSMATL       | 0.000     | 0.00000000   | HRS/PC   | NULL          | 0.00    | NULL     | 0.000        | NULL       | 0.00            | S                | 0.0000            | NULL             | 1000.0000      | 1000.0000    | 0.0000        | 0.0000       | 0.00          | 0.00        | U      | N               | NULL               | NULL       | NULL           | NULL       | NULL           | N             | N                  | NULL               | NULL               | NULL    | NULL    | NULL             | NULL              | NULL              | N             | 0.00              | 0.00            | 0.000000          | 0.00             | 0.00           | 0.000000         | 0.00             | 0.000             | 0.000           | 0.00              | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.0000           | NULL         |             0 | NULL             | N            | NULL         | 0.0000         | 75.00           | NULL      | NULL              | NULL            | NULL           | NULL           | NULL         | 0.0000        | 0.0000        | NULL               | NULL            | NULL           | NULL         | NULL           | NULL              | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL    | NULL          | NULL             | NULL           | NULL             | N              | N                 | NULL     | NULL           | NULL         | NULL         | NULL             | NULL             | N                 | N               | 2013-07-29 00:00:00.000 | NULL        | NULL        | GAR     |
| 13458 | M              | 0006-12096        |                0 |                  0 |                0 |          80 | RECVMATL        | 0.000     | 0.00000000   | HRS/PC   | NULL          | 0.00    | NULL     | 0.000        | NULL       | 0.00            | S                | 0.0000            | NULL             | 1000.0000      | 1000.0000    | 0.0000        | 0.0000       | 0.00          | 0.00        | U      | N               | NULL               | NULL       | NULL           | NULL       | NULL           | N             | N                  | NULL               | NULL               | NULL    | NULL    | NULL             | NULL              | NULL              | N             | 0.00              | 0.00            | 0.000000          | 0.00             | 0.00           | 0.000000         | 0.00             | 0.000             | 0.000           | 0.00              | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.00             | 0.0000           | NULL         |             0 | NULL             | N            | NULL         | 0.0000         | 75.00           | NULL      | NULL              | NULL            | NULL           | NULL           | NULL         | 0.0000        | 0.0000        | NULL               | NULL            | NULL           | NULL         | NULL           | NULL              | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL    | NULL          | NULL             | NULL           | NULL             | N              | N                 | NULL     | NULL           | NULL         | NULL         | NULL             | NULL             | N                 | N               | 2013-07-29 00:00:00.000 | NULL        | NULL        | GAR     |

2 个答案:

答案 0 :(得分:0)

贾斯汀,

您似乎正在编写WorkStation_Next_Available_Task rpt。

您需要一份工作订单和操作序列列表,其中下一个未完成的操作是Status = R(Ready),而WorkCenter是' 280-Lasers'。换句话说,下一个工作步骤的所有工单都发生在工作中心' 280-Lasers'上,按需求日期升序排序,然后按操作顺序排序#。

如果这是正确的,那么你的SQL需要一个子选择,这样你才能获得那些NEXT AVAILABLE操作步骤准备好并且跨越工作中心' 280-Lasers'的工作。但是看起来你正在通过任何已经准备就绪的工作获得所有操作,这些工作可以跨越工作中心' 280-Lasers'

要解决这个问题,请在评估操作状态=' R'

的where测试后添加一个子选择

它应该是这样的:

AND   OPERATIONS.Seq_Num = ( 
                    Select  TOP 1 Oper.Seq_Num
                    FROM    OPERATION Oper 
                    Where   Oper.WorkOerder_Base_Id = WORK_ORDER.BASE_ID 
                        AND     Oper.Resource_ID = '280_LASERS'
                        AND     Oper.Status = 'R'
                )

另外,删除

的测试
Oper.Resource_ID = '280_LASERS'

AND     Oper.Status = 'R'

来自您现有的Where条件。让subselect做到这一点。

完整的SQL:

SELECT
  WORK_ORDER.BASE_ID,
  OPERATION.WORKORDER_SUB_ID,
  WORK_ORDER.PART_ID,
  OPERATION.SETUP_HRS,
  OPERATION.RUN,
  OPERATION.RUN_TYPE,
  OPERATION.RUN_HRS,
  WORK_ORDER.DESIRED_QTY,
  WORK_ORDER.DESIRED_WANT_DATE,
  WORK_ORDER.COMMODITY_CODE
FROM
  dbo.WORK_ORDER
  JOIN dbo.OPERATION ON WORK_ORDER.BASE_ID = OPERATION.WORKORDER_BASE_ID
WHERE
  OPERATION.Sequence_No = ( 


                    Select  TOP 1 Oper.Sequence_No
                    FROM    OPERATION Oper      
                    Where   Oper.WorkOrder_Base_Id =     WORK_ORDER.BASE_ID         
                        AND Oper.Resource_ID = '280LASERS'        
                        AND OPERATION.STATUS='R'
                    )
ORDER BY
  DESIRED_WANT_DATE,
  OPERATION.WORKORDER_SUB_ID;

答案 1 :(得分:0)

完整SQL:

Declare @BaseId as Varchar(10) = 'W146589'

SELECT
        WORK_ORDER.BASE_ID          
    ,   WORK_ORDER.PART_ID  
    ,   WORK_ORDER.DESIRED_QTY  
    ,   WO.DESIRED_WANT_DATE 
    ,   WO.COMMODITY_CODE   
    ,   OPERATION.WORKORDER_BASE_ID         
    ,   OPERATION.WORKORDER_SUB_ID      
    ,   OPERATION.SETUP_HRS
    ,   OPERATION.RUN           
    ,   OPERATION.RUN_TYPE
    ,   OPERATION.RUN_HRS

FROM
        WORK_ORDER
JOIN            
        WORK_ORDER  WO  
    ON  WORK_ORDER.BASE_ID      =   WO.BASE_ID
    AND WO.SUB_ID = 0 
JOIN    OPERATION 
    ON  WORK_ORDER.BASE_ID      =   @BaseId
    AND WORK_ORDER.BASE_ID      =   OPERATION.WORKORDER_BASE_ID
    AND WORK_ORDER.SUB_ID       =   OPERATION.WORKORDER_SUB_ID  

WHERE   OPERATION.Sequence_No   =   ( 
                                    SELECT  TOP 1 Oper.Sequence_No
                                    FROM    OPERATION Oper      
                                    WHERE   Oper.WorkOrder_Base_Id  =   WORK_ORDER.BASE_ID      
                                        AND Oper.Resource_ID        =   OPERATION.Resource_ID
                                        AND Oper.Resource_ID        =   '280LASERS'       
                                        AND Oper.Status             =   'R'                                     
                                        AND Oper.Status             =   OPERATION.Status
                                    )
ORDER 
    BY
        OPERATION.WORKORDER_SUB_ID  
    ,   WORK_ORDER.BASE_ID          
    ,   WORK_ORDER.PART_ID      
    ,   DESIRED_WANT_DATE

;