7

I have the following sql query and I want to filter the results where the alias imagefile is null, but I can't get it to work. it's kinda basic sql... sorry for that!

SELECT Categorie.CategorieID, Categorie.Highlight, CategorieTaal.CategorieNaam,
        (SELECT TOP (1) ImageFile
                FROM Artikel WHERE (CategorieID = Categorie.CategorieID) 
                     AND (Onzichtbaar = 0) 
                     AND (NietBestelbaar = 0) 
                     AND (Voorraad = - 1000 OR Voorraad > LevertijdDrempel)
                     ORDER BY Volgnummer, ArtikelID DESC) AS 'imagefile' 
        FROM Categorie INNER JOIN 
                     CategorieTaal ON 
                     Categorie.CategorieID = CategorieTaal.CategorieID  
        WHERE (Categorie.CategorieGroepID = @catgroepid) 
               AND (Categorie.Onzichtbaar = 0) 
               AND (CategorieTaal.TaalCode = @tc) 
       ORDER BY Categorie.Volgnummer, CategorieTaal.CategorieNaam

3 Answers 3

2

You might want to try this:

SELECT Categorie.CategorieID, Categorie.Highlight, CategorieTaal.CategorieNaam,
FROM Categorie 
INNER JOIN 
    CategorieTaal ON
    Categorie.CategorieID = CategorieTaal.CategorieID  
WHERE (Categorie.CategorieGroepID = @catgroepid) 
    AND (Categorie.Onzichtbaar = 0) 
    AND (CategorieTaal.TaalCode = @tc) 
    AND NOT EXISTS (SELECT 1 ImageFile
        FROM Artikel WHERE (CategorieID = Categorie.CategorieID) 
            AND (Onzichtbaar = 0) 
            AND (NietBestelbaar = 0) 
            AND (Voorraad = - 1000 OR Voorraad > LevertijdDrempel))
ORDER BY Categorie.Volgnummer, CategorieTaal.CategorieNaam
Sign up to request clarification or add additional context in comments.

1 Comment

one small thing: there is a ',' too much on the first line. and 'NOT' should be out the query to get the correct results. execution time : 129 in sql management studio
1

You can optimize this by using an inner join again, in lieu of trying to use a subquery twice:

SELECT 
    c.CategorieID, 
    c.Highlight, 
    ct.CategorieNaam,
    a.ImageFile
FROM 
    Categorie c 
    INNER JOIN CategorieTaal ct ON 
        c.CategorieID = ct.CategorieID
    INNER JOIN 
        (select 
             CategorieID,
             ImageFile, 
             row_number() over (partition by CategorieID) as rownum
         from
             Artikel
         where
             Onzichtbaar = 0
             and NietBestelbaar = 0
             and (Voorraad = -1000 OR Voorraad > LevertijdDrempel)) a ON
        c.CategorieID = a.CategorieID
        and a.rownum = 1
WHERE 
    c.CategorieGroepID = @catgroepid
    AND c.Onzichtbaar = 0
    AND ct.TaalCode = @tc
ORDER BY c.Volgnummer, ct.CategorieNaam

Since you're using SQL Server (or at least I think you are, with your top and whatnot), you can take advantage of row_number. This will bring back just the ImageFile you need, without having to do two correlated subqueries (usually performance killers).

Also, here you only have to maintain that subquery in one place, not in two different parts of your query.

1 Comment

wow really impressive. i am not that good with sql.it's indeed sql server2005 but when i ran your query i got: The ranking function "row_number" must have an ORDER BY clause. so i changed the line with row_number() to: row_number() over (partition by CategorieID order by artikelid desc) as rownum but when i included the 'client statistics' in the management studio it said that it's not faster then the one i posted with the subquery in the where clause. maybe i am looking at the wrong numbers in sql management studio 2008? how can i measure it for sure? thanks for your reply, i learned from it
1

found it!!

SELECT  Categorie.CategorieID, Categorie.Highlight, CategorieTaal.CategorieNaam,
        (SELECT TOP (1) ImageFile
            FROM Artikel
            WHERE (CategorieID = Categorie.CategorieID)
                AND (Onzichtbaar = 0)
                AND (NietBestelbaar = 0)
                AND (Voorraad = - 1000
                    OR Voorraad > LevertijdDrempel)
            ORDER BY Volgnummer, ArtikelID DESC) AS 'imagefile'
FROM Categorie
INNER JOIN CategorieTaal ON Categorie.CategorieID = CategorieTaal.CategorieID
WHERE (Categorie.CategorieGroepID = @catgroepid)
    AND (Categorie.Onzichtbaar = 0)
    AND (CategorieTaal.TaalCode = @tc)
    AND ((
            SELECT TOP (1) ImageFile
                FROM Artikel AS Artikel_1
                WHERE (CategorieID = Categorie.CategorieID)
                    AND (Onzichtbaar = 0)
                    AND (NietBestelbaar = 0)
                    AND (Voorraad = - 1000
                        OR Voorraad > LevertijdDrempel)
    ) IS NOT NULL)
ORDER BY Categorie.Volgnummer, CategorieTaal.CategorieNaam

1 Comment

execution time: 210 :S (worst possible option atm)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.