简体   繁体   中英

SQL SELECT ID WHERE rows with the same ID have different Values

I need some help creating a SQL statement across rows.

    SELECT SZ.Stammindex AS ID, S.sEbene1, S.sEbene2, S.sEbene3
    FROM SuchbaumZuordnung SZ 
    LEFT JOIN Suchbaum S 
    ON SZ.gSuchbaumID = S.gID
    WHERE (S.sEbene1 IN ('Test1') 
    AND (S.sEbene2 IN ('Test2') OR S.sEbene2 IS NULL) 
    AND S.sEbene3 IS NULL)

这是我到目前为止所得到的

As you can see in the screenshot, I selected ID=10004 and ID=10005. But actually I only want ID=10005 to show up. I am trying to filter across Rows as already mentioned.

My goal is to get all the IDs, where all the conditions are connected with "AND", something like this:

WHERE (sEbene1 IN ('Test1') 
       AND (sEbene2 IN ('Test2') *AND* sEbene2 IS NULL) 
       AND sEbene3 IS NULL)

But this will return nothing.

Edit

表Suchbaum结构

表SuchbaumZuorndung结构

I hope you guys can help me.

I suspect that you want:

SELECT SZ.Stammindex AS ID
FROM SuchbaumZuordnung SZ 
WHERE EXISTS (SELECT 1
              FROM Suchbaum S
              WHERE SZ.gSuchbaumID = S.gID AND
                    S.sEbene1 IN ('Test1') AND
                    sEbene2 IN ('Test2')
             ) AND
       EXISTS (SELECT 1
              FROM Suchbaum S
              WHERE SZ.gSuchbaumID = S.gID AND
                    S.sEbene2 IS NULL AND
                    S.sEbene3 IS NULL
             );

This is looking for two different rows in Suchbaum , each one matching one of the conditions.

Considering you only have 3 columns you want to check different rows, it seems like this would be easily serviced with a CTE and a Windowed COUNT :

WITH CTE AS(
    SELECT SZ.Stammindex AS ID,
           S.sEbene1, --Guessed the table alias
           S.sEbene2, --Guessed the table alias
           S.sEbene3, --Guessed the table alias
           COUNT(DISTINCT CONCAT(ISNULL(S.S.sEbene1,'-'),ISNULL(S.sEbene2,'-'),ISNULL(S.sEbene3,'-'))) OVER (PARTITION BY SZ.Stammindex) AS DistinctRows
    FROM SuchbaumZuordnung SZ 
         LEFT JOIN Suchbaum S ON SZ.gSuchbaumID = S.gID) --This was missing the ON in your sample
SELECT C.Stammindex,
       C.sEbene1,
       C.sEbene2,
       C.sEbene3
FROM CTE C
WHERE C.DistinctRows > 1;

If it's purely where an ID has more than 1 rows (which could be identical) then you can just use COUNT :

WITH CTE AS(
    SELECT SZ.Stammindex AS ID,
           S.sEbene1, --Guessed the table alias
           S.sEbene2, --Guessed the table alias
           S.sEbene3, --Guessed the table alias
           COUNT(*) OVER (PARTITION BY SZ.Stammindex) AS [Rows]
    FROM SuchbaumZuordnung SZ 
         LEFT JOIN Suchbaum S ON SZ.gSuchbaumID = S.gID)
SELECT C.Stammindex,
       C.sEbene1,
       C.sEbene2,
       C.sEbene3
FROM CTE C
WHERE C.[Rows] > 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM