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 7536879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:41:47+00:00 2026-05-30T06:41:47+00:00

I have a table INFO whose design is like this id – bigint Name

  • 0

I have a table INFO whose design is like this

id - bigint
Name - varchar2
refid - bigint
status - int
ExpDate - datetime
  • status values can be 0,1,2,3,4
  • refid is the foreign key of other table which we are going to use too

I want to write a query where all records should come from this table with status 0, 1, 2, 3, but only 7 latest records by expdate should come whose status is 4.

I can’t figure out how can we achieve this in T-SQL.

Please help me out or give me suggestion so that I can start writing it.

Upto now I have written

SELECT * 
FROM INFO 
WHERE STATUS IN (0,1,2,3) AND 
      REFID IN (SELECT REFID FROM REFTABLE WHERE REFCHAIN='BMW')

SELECT TOP 7
FROM INFO 
WHERE STATUS=4 AND 
      REFID IN(SELECT REFID FROM REFTABLE WHERE REFCHAIN='BMW') 
ORDER BY EXPDATE DESC

i need to join them??? how.. suggest and also the query

(SELECT REFID FROM REFTABLE WHERE REFCHAIN='BMW') 

is coming twice how to optimize it thanks..

  • 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-05-30T06:41:48+00:00Added an answer on May 30, 2026 at 6:41 am

    Just UNION your 2 current queries together (you need a derived table to get the TOP though)

    SELECT ...
    FROM INFO 
    WHERE STATUS IN (0,1,2,3) AND 
          REFID IN (SELECT REFID FROM REFTABLE WHERE REFCHAIN='BMW')
    UNION ALL
    SELECT ...
    FROM 
        (
        SELECT TOP 7 ...
        FROM INFO 
        WHERE STATUS=4 AND 
              REFID IN(SELECT REFID FROM REFTABLE WHERE REFCHAIN='BMW') 
        ORDER BY EXPDATE DESC
        ) T
    

    You can get fancier, but it may not be as efficient:

    SELECT *
    FROM
        (
        SELECT ...,
           ROW_NUMBER() OVER (ORDER BY EXPDATE DESC) AS rn
        FROM INFO 
        WHERE REFID IN (SELECT REFID FROM REFTABLE WHERE REFCHAIN='BMW')
        ) T
    WHERE
        rn <= 7 OR STATUS <= 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table that looks like this: id (int primary) name (text)
I have a table named Info of this schema: int objectId; int time; int
I have table with 50 entries (users with such details like Name Surname Location
I have a datatable storing info about a student classroom. My table looks like
I have a table schema which is essentially a bunch of transaction info with
I have a form that sends info into a database table. I have it
I have table with some fields that the value will be 1 0. This
I have a table, of whose number of columns can change depending on the
To start, I put my info into Core Data and I have my table
I have some php querying and printing the info of a mysql table on

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.