5

我有一个要使用另一个表的记录来更新的表,我这样做是为了将信息从一个系统(数据库)带到另一个系统(数据库)。场景有点复杂,但我迫切需要帮助:-s

有 3 个表-componentscanstage_link

零件

component_id  stage_id
------------  --------
1              NULL
2              NULL
3              NULL
4              NULL
5              NULL

扫描

scan_id  component_id  scanner_id           date_scanned
-------  ------------  ----------  -----------------------
 1         1           scanner_a    2012-01-01 07:25:15.125
 2         1           scanner_b    2012-01-02 08:14:05.456
 3         2           scanner_a    2012-01-01 12:05:45.465
 4         3           scanner_a    2012-01-01 19:45:12.536
 5         1           scanner_c    2012-01-03 23:33:54.243
 6         2           scanner_b    2012-01-02 11:59:12.545

stage_link

stage_link_id  scanner_id  stage_id
    -------     ----------  ----------  
       1         scanner_a    1   
       2         scanner_b    1    
       3         scanner_c    2    
       4         scanner_d    2    
       5         scanner_e    2   
       6         scanner_f    3  

我需要根据最新扫描update的表格componentset字段。stage_id根据所涉及的扫描仪,每次扫描都会将组件带到一个阶段。我已经为updatetable编写了以下查询component,但它会抛出一个错误说;

Subquery returned more than 1 value. This is not permitted when the subquery follows '='

查询是;

UPDATE component
SET stage_id = (select stage_id
                from(
                    select scn.scanner_id, sl.stage_id
                    from scan scn
                    INNER JOIN stage_link sl ON scn.scanner_id = sl.scanner_id
                    where scn.date_scanned = (  select temp_a.max_date 
                                                from (  SELECT x.component_id, MAX(x.date_scanned) as max_date
                                                        FROM scan x
                                                        where component_id = x.component_id 
                                                        GROUP BY x.component_id
                                                      ) as temp_a
                                                where component_id = temp_a.component_id)
                    ) as temp_b
                )

我正在努力MS SQL Server并希望使用不使用PHP任何语言或任何其他语言来解决这个问题。

我已经尝试了一天来完成这项工作,但仍然没有办法完成这项工作。任何帮助将不胜感激!

非常感谢你:-)

4

3 回答 3

3

在不使用相关子查询的情况下检查这一点:

UPDATE  Com
SET     stage_id = Temp4.stage_id
FROM    dbo.component Com
        INNER JOIN 
        ( 
            SELECT Temp2.component_id ,SL.stage_id
            FROM   dbo.stage_link SL
            INNER JOIN (
                            SELECT component_id ,scanner_id
                            FROM   scan
                            WHERE  date_scanned IN (
                                SELECT  MaxScanDate
                                FROM    
                                ( 
                                    SELECT component_id , MAX(date_scanned) MaxScanDate
                                    FROM scan
                                    GROUP BY component_id
                                ) Temp 
                            )
                        ) Temp2 ON Temp2.scanner_id = SL.scanner_id
        ) Temp4 ON Com.component_id = Temp4.component_id

输出:

component_id stage_id
------------ -----------
1            2
2            1
3            1
4            NULL
5            NULL
于 2012-08-23T16:23:11.850 回答
2

好吧,您的子查询返回多个值。一种简单的方法是进行聚合:

SET stage_id = (select max(stage_id)
. . .

一个可能的原因是因为在最近的日期进行了多次扫描。由于您只能选择一个,给定上下文,那么 MIN 或 MAX 就足够了。

但是,我认为真正的原因是您没有相关子查询的正确别名。我认为这些行:

where component_id = x.component_id
where component_id = temp_a.component_id

应该包括别名,可能是:

where component.component_id = x.component_id
where component.component_id = temp_a.component_id

如果这还不够,你需要解释你想要什么。您是否希望查询从最近的日期返回随机扫描?您想在最近日期更新所有扫描的组件吗?

您需要对此进行进一步调查。尝试这样的事情:

select scn.scanner_id, sl.stage_id, count(*)
from scan scn INNER JOIN
     stage_link sl
     ON scn.scanner_id = sl.scanner_id join
     (SELECT x.component_id,
             MAX(x.date_scanned) as max_date
      FROM scan x
      GROUP BY x.component_id
     ) cmax
     on scn.component_id = cmax.component_id
where scn.date_scanned = cmax.maxdate 
group by scn.scanner_id, sl.stage_id
order by count(*) desc
于 2012-08-23T15:12:30.230 回答
2

这需要一个 OLAP 函数才能工作:

UPDATE Component SET Component.stage_id = Stage_Link.stage_id
FROM Component
JOIN (SELECT component_id, scanner_id, 
             ROW_NUMBER() OVER(PARTITION BY component_id 
                               ORDER BY date_scanned DESC) rownum
      FROM Scan) Scan
  ON Scan.component_id = Component.component_id
     AND Scan.rownum = 1
JOIN Stage_Link
  ON Stage_Link.scanner_id = Scan.scanner_id
WHERE Component.stage_id IS NULL

这会生成以下结果集:

Component
component_id   stage_id
========================
1              2
2              1
3              1
4              null
5              null

(我也有一个有效的 SQL Fiddle示例。)

于 2012-08-23T15:35:40.977 回答