Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7831117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:34:48+00:00 2026-06-02T11:34:48+00:00

I have following SQL query but this is not quite what I want: SELECT

  • 0

I have following SQL query but this is not quite what I want:

SELECT 
    TOP (20) Attribs.ImageID AS ItemID 
FROM 
    Attribs 
LEFT OUTER JOIN 
    Items ON Attribs.ImageID = Items.ImageID 
WHERE
    (attribID IN ('a','b','c','d','e')) 
    AND (deleted NOT IN (1,2)) 
    AND Attribs.attribID = 'a' AND Attribs.attribID = 'b'
GROUP BY 
    Attribs.ImageID 
ORDER BY 
    COUNT(DISTINCT attribID) DESC

What I need is to query

   AND Attribs.attribID = 'a' AND Attribs.attribID = 'b'

first, then rest of the WHERE clause based on the above query results.

Is this possible to achieve using sub query?

I’m using SQL Server 2008

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:34:51+00:00Added an answer on June 2, 2026 at 11:34 am

    I’m not totally getting the reason why you want to do this one query first before the other…. but you could use a Common Table Expression (CTE) – something like this:

    ;WITH FirstQuery AS
    (
        SELECT a.ImageId
        FROM dbo.Attribs a
        WHERE a.attribID = 'a' AND a.attribID = 'b'
    )
    SELECT 
        TOP (20) a.ImageID AS ItemID 
    FROM 
        dbo.Attribs a
    INNER JOIN
        FirstQuery fq ON a.ImageId = fq.ImageId  
    LEFT OUTER JOIN 
        dbo.Items i ON a.ImageID = i.ImageID 
    WHERE
        (attribID IN ('a','b','c','d','e')) 
        AND (deleted NOT IN (1,2)) 
    GROUP BY 
        a.ImageID 
    ORDER BY 
        COUNT(DISTINCT attribID) DESC
    

    With this, you first select the ImageID from your dbo.Attribs table in the CTE, and then join that result set with the result of the table and join to the Items table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following SQL query I was given, but I am not sure
I have the following SQL Query. SELECT MIN(PLD.available_date) as 'FROM', MAX(PLD.available_date) as 'UNTIL', (
I have the following which works in SQL Query Analyzer . select oh.* from
I have the following SQL query: SELECT Phrases.* FROM Phrases WHERE (((Phrases.phrase) Like *ing
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
I have the following SQL query - SELECT * FROM dbo.LocalContacts WHERE name LIKE
I have the following SQL query: SELECT DISTINCT business_key FROM Memory WHERE concept <>
I have the following SQL query: SELECT DISTINCT ProductNumber, PageNumber FROM table I am
I have the following sql query and I want to filter the results where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.