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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:05:25+00:00 2026-05-18T04:05:25+00:00

Alright I have a table where I need to get the relevant date from

  • 0

Alright I have a table where I need to get the relevant date from a GROUP BY function…

Here is a sample of the data:
min_price, day
140000     2010-09-26
130000     2010-09-27
154991     2010-10-08
143000     2010-10-09
156470     2010-10-10

I would like this result:
130000     2010-09-27
143000     2010-10-09

Here is the SQL I use:

SELECT MIN(min_price) AS low_price, min_price, day
FROM compress
WHERE item_name = '$item'
GROUP BY EXTRACT(MONTH FROM day), EXTRACT(YEAR FROM day)

Here is what I get:
130000     2010-09-26
143000     2010-10-08

As you can see, the dates don’t match up with the min row value. I need them to match up. What SQL can I use to get that result?

  • 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-18T04:05:25+00:00Added an answer on May 18, 2026 at 4:05 am
    Select C.min_price, C.day
    From compress As C
    Where C.item_name = '$item'
        And Exists  (
                    Select 1
                    From compress As C2
                    Where C2.item_name = C.item_name
                        And Extract(Month From C2.day) = Extract(Month From C.day)
                        And Extract(Year From C2.day) = Extract(Year From C.day)
                    Group By Extract(Month From C2.day), Extract(Year From C2.day)
                    Having C.min_price = Min(C2.min_price)
                    )
    

    ADDITION

    MySQL is better with joins so here’s a variant of the same query

    Select C.min_price, C.day
    From compress As C
        Join    (
                Select C2.item_name
                    , Extract(Month From C2.day) As ItemMonth
                    , Extract(Year From C2.day) As ItemYear
                    , Min(C2.min_price) As MinPrice
                From compress As C2
                Group By C2.item_num, Extract(Month From C2.day), Extract(Year From C2.day)
                ) As Z
            On Z.item_name = C.item_name
                And Z.ItemMonth = Extract(Month From C.day)
                And Z.ItemYear = Extract(Year From C.day)
                And Z.MinPrice = C.min_price
    Where C.item_name = '$item'
    

    What is really slowing down any variation is the calling of Extract on every row. If you had a calendar table containing a row for each date and a column for the month and year of the date and your calendar table covered the date ranges in your data, you could then do something like:

    Select C.min_price, C.day
    From compress As C
        Join Calendar As Cal
            On Cal.Date = C.Day
        Join    (
                Select C3.item_name
                    , Cal3.Month, Cal3.Year
                    , Min(C3.min_price) As MinPrice
                From compress As C3
                    Join Calendar As Cal3
                        On Cal3.Date = C3.day
                Group By C3.item_num, Cal3.Month, Cal3.Year
                ) As Z
            On Z.item_name = C.Item_name
                An Z.Month = Cal.Month
                And Z.Year = Cal.Year
                And Z.MinPrice = C.min_price
    Where C.item_name = '$item'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright. So I have a very large amount of binary data (let's say, 10GB)
Alright so I have this C++ image capturing class. I was wondering if I
Alright, currently I have my SWF hitting a php file that will go and
Alright, I have been doing the following (variable names have been changed): FileInputStream fis
Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
Alright, so I have a query that looks like this: SELECT `orders`.*, GROUP_CONCAT( CONCAT(
Alright...I've given the site a fair search and have read over many posts about
Is it alright to expect that the user using the back end will have
Alright. So I figure it's about time I get into unit testing, since everyone's
Alright, I'm trying to read a comma delimited file and then put that into

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.