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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:48:38+00:00 2026-06-01T13:48:38+00:00

I’m building a report for a database where I need to determine the number

  • 0

I’m building a report for a database where I need to determine the number of “first scans” grouping by company, job, and date.

The scan table can contain multiple scans for the same item, however I only want to include the original scan in my COUNT, which can only be identified as being the scan with the earliest date that matches a particular item.

My first attempt at this was:

SELECT 
     _item_detail.job_id, 
     _item_group.group_id, 
     _scan.company_id, 
     DATE(scan_date_time) as scan_date, 
     COUNT(1)
FROM _scan
    INNER JOIN _item_detail ON _item_detail.company_id = _scan.company_id 
    AND 
    _item_detail.serial_number = _scan.serial_number
    INNER JOIN _item_group ON _item_group.group_id = _item_detail.group_id
WHERE _item_detail.job_id = '0326FCM' AND _scan.company_id = '152345' AND _item_group.group_id = 13
GROUP BY 
_item_detail.job_id, 
_item_group.group_id, 
_scan.company_id, scan_date -- first_scan_count
HAVING min(scan_date_time);

This is giving me incorrect results, though (about 3x too many). I am assuming it’s because the MIN record is being recalculated for each date, so if the min was found on day 1, it may also be found on day 3 and counted again.

How can I modify my query to achieve the desired results?

  • 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-01T13:48:39+00:00Added an answer on June 1, 2026 at 1:48 pm

    Something similar to this should work… I’m not completely sure of how your tables are laid out or how the data relates them together, but this is the general idea:

    SELECT 
        _item_detail.job_id, 
        _item_group.group_id, 
        _scan.company_id, 
        DATE(scan_date_time) as scan_date, 
        COUNT(1)
    FROM
        _scan s1
        INNER JOIN _item_detail 
            ON _item_detail.company_id = s1.company_id 
            AND _item_detail.serial_number = s1.serial_number
            AND _item_detail.job_id = '0326FCM'
        INNER JOIN _item_group 
            ON _item_group.group_id = _item_detail.group_id
            AND _item_group.group_id = 13
    WHERE 
        s1.company_id = '152345'
        AND s1.scan_date_time = (
            SELECT MIN(s2.scan_date_time)
            FROM _scan s2
            WHERE 
                s2.company_id = s1.company_id
                AND s2.serial_number = s1.serial_number
        )
    GROUP BY 
        _item_detail.job_id, 
        _item_group.group_id, 
        s1.company_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.