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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:05:09+00:00 2026-05-12T21:05:09+00:00

I am having difficulties with a complicated (for me any way) query. The table

  • 0

I am having difficulties with a complicated (for me any way) query.

The table I’m querying has 3 colums, ClientID (int Not Null), ProductID (int Not Null) and ExpiryDate (smalldatetime nullable)

Given two client ID’s Master and Consolidated I need to perform the following business logic to return a single data set:

Select the ClientID with the greater
expiry date for a product where expiry
dates for both clientIDs are not null

Select the ClientID with a null expiry
date for a product where one expiry is
null and the other not null

Select the MasterID for a product
where both expiry dates are null or
both expiry dates are the same.

I have tried the following, but get stuck…

Create Table #ProductSub (ClientID int NOT NULL, 
                          ProductID int NOT NULL, 
                          ExpiryDate smalldatetime)  

/* In real life there is a Clustered Primary Key On ClientID and ProductID
   Load Up Some Test Data */  

  Insert into #ProductSub  Values (1, 100, null)
  Insert into #ProductSub  Values (2, 100, null)
  Insert into #ProductSub  Values (1, 101, null)
  Insert into #ProductSub  Values (2, 102, null)
  Insert into #ProductSub  Values (1, 200, null)
  Insert into #ProductSub  Values (2, 200, '2009-01-01')
  Insert into #ProductSub  Values (1, 300, '2009-01-01')
  Insert into #ProductSub  Values (2, 300, null)
  Insert into #ProductSub  Values (1, 400, '2009-01-01')
  Insert into #ProductSub  Values (2, 400, '2008-01-01')
  Insert into #ProductSub  Values (1, 500, '2008-01-01')
  Insert into #ProductSub  Values (2, 500, '2009-01-01')
  Insert into #ProductSub  Values (1, 600, '2009-01-01')
  Insert into #ProductSub  Values (2, 600, '2009-01-01')  

 --Select * from #ProductSub  

  Declare @MasterClient int,
          @ConsolClient int

  Select @MasterClient = 1, @ConsolClient = 2  


Select * from #ProductSub t1
  /* Use Master Client ID When Expiry Date is Null) */
  Where (ClientID = @MasterClient and ExpiryDate is null)
  /* Use Consol ClientID if Expiry Date is null nut Expiry Date for Master Client ID is not */
  OR    (ClientID = @ConsolClient and ExpiryDate is null and ProductID not in (
            Select ProductID from #ProductSub t2
            Where (ClientID = @MasterClient and ExpiryDate is null))
        ) 
  OR   -- OH NO my head exploded
/*  OR EXISTS (Select 1
             from #ProductSub t3
            )*/

Drop Table #ProductSub   

/**********  Expected  Output  ************************
ClientID     ProductID     ExpiryDate
1            100           NULL
1            101           NULL
2            102           NULL
1            200           NULL
2            300           NULL
1            400           2009-01-01 00:00:00
2            500           2009-01-01 00:00:00
1            600           2009-01-01 00:00:00

Any and all help greatly appreciated

EDIT: Although it sounds like it, this is not homework but a real life problem I am hoping to find a real life solution to, I could do this myself, but all my solutions are leading down the path to temp tables. I should point out the production environment is SQLServer 7!

  • 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-12T21:05:10+00:00Added an answer on May 12, 2026 at 9:05 pm

    Here I’ve moved the conditions to a subquery. The subquery joins the rows for Consol and Master, so you can access columns from both rows. The condition is still a little complex because either row can be missing.

    select ps.*
    from @ProductSub ps
    inner join (
        select     
          CASE 
            WHEN c.ClientID is null THEN m.ClientID
            WHEN m.ClientID is null THEN c.ClientID
            WHEN m.ExpiryDate is not null and c.ExpiryDate is not null THEN
              CASE 
                WHEN c.ExpiryDate > m.ExpiryDate THEN c.ClientID
                ELSE m.ClientID
              END
            WHEN m.ExpiryDate is null THEN m.ClientID
            WHEN c.ExpiryDate is null THEN c.ClientID
            ELSE m.ClientID
          END as ClientId,
          COALESCE(m.ProductId, c.ProductId) as ProductId
        from       @ProductSub m
        full outer join  @ProductSub c
        on         m.ProductID = c.ProductID
        and        m.ClientID <> c.ClientID
        where      IsNull(m.clientid,@MasterClient) = @MasterClient
        and        IsNull(c.clientid,@ConsolClient) = @ConsolClient
    ) filter
    on filter.clientid = ps.clientid
    and filter.productid = ps.productid
    order by ps.ProductId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having difficulties with a program that I have been working on all
I am having difficulties with pages that have more than one <audio> when I
I've got a service that I need to shut down and update. I'm having
I'm working on a console based file browser for Windows in C++ and am
I am trying to add a simple delay to a mouseover event of a
For some reason I cannot do this on files inside bundles and files residing
level: beginner the following code will print 'False' def function(x): if len(x) == 5:
Could someone show me how to get a list of aboslute paths for all
i'm using a multimap stl, i iterate my map and i did'nt find the
This is an offshoot question that's related to another I asked here . I'm

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.