2

我是编写 SQL 查询的新手,一直在尝试从 3 个不同的表中提取信息并将其链接在一起。

数据驻留在 Microsoft SQL Server 中。

这是我当前的查询(这不是我想要的方式):

SELECT h.jhhold, h.jhjob, h.jhpart, h.jhrev, h.jhcust, h.jhextd, o.jadesc, h.jhpqty, o.jaoqty, o.jacqty, o.japo, o.javend 
FROM Jhead AS h
LEFT JOIN jjops AS o ON h.jhjob = o.jajob 
LEFT JOIN hpodt AS p ON h.jhjob = p.hdjob 
WHERE h.jhpcmp = 0 AND o.jatype = 2 AND o.jacqty < o.jaoqty AND japcmp = 0 
ORDER BY h.jhjob

解释我想要做什么。我有一个作业标题表Jhead、一个作业详细信息表jjops和一个采购订单详细信息表hpodt。我相信工作编号是 jhead 表中的主键。

这是我的问题:一旦我添加了第二个左连接,我就会得到很多额外的重复信息行。

我尝试使用 GROUP BY jhead.jhjob 但收到以下错误:

“列 'jhead.jhhold' 在选择列表中无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。”</p>

所以我研究了错误,其中一种解决方案是让我的 GROUP BY 看起来像这样: GROUP BY jhead.jhjob、jhead.jhhold、jhead.jhpart、jhead.jhrev、jhead.jhcust、jhead.jhextd

但是当我输入 jhead.jhextd 时,我得到了这个错误:“text、ntext 和 image 数据类型无法比较或排序,除非使用 IS NULL 或 LIKE 运算符。”</p>

现在已经撞到了一堵砖墙。我确实看到您可以将类型更改为 NVARCHAR。我真的不想按描述字段进行排序,因为它是独一无二的,并且可能对服务器造成很大负担。有一个更好的方法吗?

使用示例数据进行编辑: 我删除了额外的字段以简化查询并过滤了 1 个工作编号的结果。

只有 1 个左连接: 只有 1 个左连接

使用两个左连接: 第二

如您所见,ERP 软件在 1 个 PO# 下有多个工作,我只对显示具有匹配工作编号的行感兴趣。 第三

查询已更新

SELECT h.jhhold, h.jhjob, h.jhrev, o.jadesc, h.jhpqty, o.jaoqty, o.jacqty, o.japo, o.javend, p.hdprcd, p.hdrecd
FROM Jhead AS h
LEFT JOIN jjops AS o ON h.jhjob = o.jajob 
LEFT JOIN hpodt AS p ON h.jhjob = p.hdjob 
WHERE h.jhpcmp = 0 AND o.jatype = 2 AND o.jacqty < o.jaoqty AND japcmp = 0 AND p.hdjob = h.jhjob AND p.hdpo = o.japo
ORDER BY h.jhjob

这越来越接近我想要的了。现在的问题是,当每行实际上应该不同时,我的 jacksc 字段正在复制第一个结果。

4

我过滤后只显示一份工作。它们应该只有一列。

获得额外的行,但拥有我需要的所有数据

SELECT h.jhhold, h.jhjob, h.jhrev, o.jadesc, h.jhpqty, o.jaoqty, o.jacqty, o.japo, o.javend, p.hdprcd, p.hdrecd, p.hdseq 
FROM Jhead AS h 
LEFT JOIN jjops AS o ON h.jhjob = o.jajob 
LEFT JOIN hpodt AS p ON h.jhjob = p.hdjob 
WHERE (h.jhpcmp = 0 AND o.jatype = 2 AND o.jacqty < o.jaoqty AND japcmp = 0 AND p.hdjob = h.jhjob AND p.hdpo = o.japo AND p.hdseq = o.jaseq AND o.jaopr != 'PT') 
  OR (h.jhpcmp = 0 AND o.jatype = 2 AND o.jacqty < o.jaoqty AND japcmp = 0 AND o.japo = 0 AND o.jaopr != 'PT')
ORDER BY h.jhjob, o.jaseq
4

1 回答 1

0

I guess you want to show detailed information about the jhead and jjops tables, but just want to show some aggregate information found on the hpodt table.

If that's the case, you should do something like this:

SELECT
    h.jhhold, h.jhjob, h.jhpart, h.jhrev, h.jhcust, h.jhextd,
    o.jadesc, h.jhpqty, o.jaoqty, o.jacqty, o.japo, o.javend,
    (SELECT COUNT(*) FROM hpodt p WHERE p.jhjob = h.hdjob) AS po_line_count,
    CASE WHEN EXISTS (SELECT * FROM hpodt p WHERE p.jhjob = h.hdjob)
        THEN 'found it'
        ELSE 'not found'
    END AS info_on_hpodt
FROM
    Jhead AS h
    LEFT JOIN jjops AS o ON h.jhjob = o.jajob 
WHERE
    h.jhpcmp = 0 AND o.jatype = 2 AND o.jacqty < o.jaoqty AND japcmp = 0 
ORDER BY
    h.jhjob

It isn't clear to me which specific information you want from the hpodt table, but I guess this strategy of using a sub-query will work by just adding other criteria to its WHERE clause.

Another option would be using window functions, but the sub-query approach is simpler.

于 2012-10-31T21:55:27.920 回答