具有多个IN的复杂SQL语句

时间:2016-06-27 09:17:37

标签: sql sql-server

首先让我告诉你示例记录。

表:objekte

+--------+----------+---------+------------+
| objid  | objidate | objmask | deletedate |
+--------+----------+---------+------------+
| 271136 | 54084960 | 4       | 0          |
| ...    | ...      | ...     | ...        |
+--------+----------+---------+------------+

表:objkeys

+----------+--------+------------+-------------------------------------+
| parentid | okeyno |  okeyname  |              okeydata               |
+----------+--------+------------+-------------------------------------+
|  1511160 |      1 | BelegNr.   | 634685                              |
|  1511160 |      2 | BELEGARTBK | oU = Lieferschein ohne Unterschrift |
+----------+--------+------------+-------------------------------------+

现在,我到目前为止已经尝试了这些:

SELECT * FROM objekte
WHERE objid IN (
            SELECT MIN(parentid) as parentid FROM objkeys
            WHERE okeyname = 'BelegNr.' 
            GROUP BY okeydata
            HAVING COUNT(okeydata) = 1
    ) AND objmask = '69' AND deletedate = '0' AND DATEADD(mi, objidate, '30.12.1899') >= DATEADD(day, -30, getdate())

预期输出:
我想显示表 objekte objkeys.parentid = objekte.objid)中的所有 objid ,其中okeyname "Belegn"只有一条记录BELEGARTBK is "oU = Lieferschein ohne Unterschrift".

在我的SQL代码中,我可以显示所有当前对象" unique"。但我无法通过" BELEGARTBK"在这一刻。有一个简单的方法吗?

1 个答案:

答案 0 :(得分:0)

很抱歉,您的要求不是很清楚,请尝试使用此嵌套查询:

  SELECT parentid FROM objkeys
    WHERE okeyname = 'Belegn' and okeydata = 'Lieferschein ohne Unterschrift' 
    GROUP BY parentid 
    HAVING COUNT(*) = 1