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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:47:23+00:00 2026-05-12T16:47:23+00:00

Looking at the related questions, I don’t think this specific question has been asked,

  • 0

Looking at the related questions, I don’t think this specific question has been asked, so here goes.

I had a situation where I joined on a table three times to get different data based on dates.

This took too long, so in an effort to optimize, I rewrote it using a group by as defined here: http://weblogs.sqlteam.com/jeffs/jeffs/archive/2007/06/12/60230.aspx

I’m having a hard time with the logic, and I’m beginning to think it’s not possible to get exactly what I want through this. I’ll show you my current code then describe what I need from it (tables/variables changed to protect the innocent).

SELECT
 upc,
 MAX(CASE WHEN ip_start_date <= GETDATE() THEN ip_unit_price END) AS retail_amount,
 MAX(CASE WHEN ip_start_date <= GETDATE() THEN ip_price_multiple END) AS retail_multiplier_num,    
 MAX(CASE WHEN ip_start_date BETWEEN GETDATE() AND DATEADD(ww,1,GETDATE()) THEN ip_unit_price END) AS retail_amt_nxt_wk,
 MAX(CASE WHEN ip_start_date BETWEEN GETDATE() AND DATEADD(ww,1,GETDATE()) THEN ip_price_multiple END) AS retail_multipler_num_nxt_wk,
 MAX(CASE WHEN ip_start_date BETWEEN DATEADD(ww,1,GETDATE()) AND DATEADD(ww,2,GETDATE()) THEN ip_unit_price END) AS retail_amt_wk_after_nxt,
 MAX(CASE WHEN ip_start_date BETWEEN DATEADD(ww,1,GETDATE()) AND DATEADD(ww,2,GETDATE()) THEN ip_price_multiple END) AS retail_multiplier_num_wk_after_nxt
FROM 
 items AS im WITH (NOLOCK)
  retails AS ip WITH (NOLOCK)
   ON im.ID = ip.ID
GROUP BY
 upc 

So looking at first line, this gets me the max retail with a date less than today. I actually need the most recent one, not the largest. This used to be handled with a sub-query which got me the MAX(start_date) less than today. I can’t do a MAX within a MAX, for what are most likely good reasons. I was considering LAST, but I’m not quite sure the last record will always be the most recent in our system (new system).

Does anyone see a solution to this? The BETWEENS work fine, MAX retail within that week is good enough as those are for estimation. The other one must be accurate, though.

(Feel free to edit title..I couldn’t come up with a succinct way to ask this)

  • 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-12T16:47:23+00:00Added an answer on May 12, 2026 at 4:47 pm

    In SQL Server 2005+:

    SELECT  upc, retail_today.*, retail_next_week.*, retail_two_weeks.*
    FROM    items im
    OUTER APPLY
            (
            SELECT  TOP 1
                    ip_unit_price, ip_price_multiple
            FROM    retail ip
            WHERE   ip.ip_start_date <= GETDATE()
                    AND ip.id = im.id
            ORDER BY
                    ip_start_date DESC
            ) retail_today
    OUTER APPLY
            (
            SELECT  TOP 1
                    ip_unit_price, ip_price_multiple
            FROM    retail ip
            WHERE   ip.ip_start_date BETWEEN GETDATE() AND DATEADD(ww, 1, GETDATE())
                    AND ip.id = im.id
            ORDER BY
                    ip_start_date DESC
            ) retail_next_week
    OUTER APPLY
            (
            SELECT  TOP 1
                    ip_unit_price, ip_price_multiple
            FROM    retail ip
            WHERE   ip.ip_start_date BETWEEN DATEADD(ww, 1, GETDATE()) AND DATEADD(ww, 2, GETDATE())
                    AND ip.id = im.id
            ORDER BY
                    ip_start_date DESC
            ) retail_two_weeks
    

    In SQL Server 2000:

    SELECT  upc,
            (
            SELECT  TOP 1
                    ip_price
            FROM    retail ip
            WHERE   ip.ip_start_date <= GETDATE()
                    AND ip.id = im.id
            ORDER BY
                    ip_start_date DESC
            ) AS ip_price_today,
            (
            SELECT  TOP 1
                    ip_price_multiple
            FROM    retail ip
            WHERE   ip.ip_start_date <= GETDATE()
                    AND ip.id = im.id
            ORDER BY
                    ip_start_date DESC
            ) AS ip_price_multiple_today,
            …
    FROM    items im
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is related to a question I asked here: Thread Locking in Ruby (use
I don't see any exact duplicates in the Related Questions above, so here goes.
This question is related to my earlier question, asked here: How do I get
This is related to another Delphi-version question but still different; I'm looking for a
I've asked a related question here: Show unmatched html tags in Notepad++ Having only
There are multiple related questions, but I'm looking for a solution specific to my
not sure if this question should be here or in serverfault, but it's java-related
I asked a related question a week ago, but here's another problem I'm facing.
This question relates to a RoR quiz app. I am looking to display the
I have been looking around and even found a couple of related answers and

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.