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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:46:01+00:00 2026-06-13T21:46:01+00:00

I have the following query: select fp.id, fr.id, sum(case when to_date(fp.offered_date) BETWEEN TO_DATE( :ad_startdate,

  • 0

I have the following query:

select 
    fp.id, 
    fr.id,
    sum(case 
        when to_date(fp.offered_date) BETWEEN TO_DATE( :ad_startdate, 'YYYY-MM-DD') 
             AND TO_DATE(:ad_enddate, 'YYYY-MM-DD') and fp.result <> 'E'
             then 1 
        else 0 
        end) total,
    sum(case when fp.result = 'G' 
        and  to_date(fp.offered_date) >=  :ad_startdate
        and  to_date(fp.offered_date) <= :ad_enddate then 1 else 0 end) colorgreen,
    sum(case when fp.resultat = 'R' 
        and  to_date(fp.offered_date) >=  :ad_startdate
        and  to_date(fp.offered_date) <= :ad_enddate then 1 else 0 end) colorred
FROM 
    fruit_properties fp, fruit fr
WHERE 
    fp.id = fr.id
GROUP BY 
    fp.id, fr.id

I’m checking dates 1 time for each sum column and have a feeling this can be made once somehow? Right now if I check only once at the total column, then colorgreen + colorred might be larger than the total since it counts no matter what date they have.

Can my query be enhanced somehow?

  • 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-13T21:46:02+00:00Added an answer on June 13, 2026 at 9:46 pm

    you can simplify like this. but PLEASE check your SQL. you’re mixing TO_DATE and CHAR datatypes. this will only end in disaster.

    eg you have:

    when to_date(fp.offered_date) BETWEEN TO_DATE( :ad_startdate, 'YYYY-MM-DD') 
                 AND TO_DATE(:ad_enddate, 'YYYY-MM-DD')
    

    vs

    sum(case when fp.result = 'G' 
        and  to_date(fp.offered_date) >=  :ad_startdate
    

    in one case you are TO_DATE’ing ad_startdate but not another (so is it a date already or not?). you are also TO_DATEing the column but crucially WITHOUT a format mask. is the column really a VARCHAR datatype? if so you really should not store dates as anything but DATEs.

    anyway assuming the column is a DATE datatype and the binds are of type DATE..

    select fruit_prop_Id,fruit_id, 
           sum(case when result != 'E' then within_offer else 0 end) total,
           sum(case when result = 'R' then within_offer else 0 end) colorred,
           sum(case when result = 'G' then within_offer else 0 end) colorgreen
     from (select fp.id fruit_id, 
                    fr.id fruit_prop_Id,
                    fp.result,
                    case 
                       when fp.offered_date >= :ad_startdate
                        and fp.offered_date <= :ad_enddate then 1 else 0 end within_offer
             from fruit_properties fp, fruit fr
            where fp.id = fr.id)
            group by fruit_id, fruit_prop_Id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query something like the following: select sum(t1.qty) t1Total, sum(t2.qty) t2Total, sum(t1.qty
I have a query similar to the following: SELECT CASE WHEN (GROUPING(Name) = 1)
have the following sql query select catalogid , sum(numitems) numitems , sum(allitems) - sum(numitems)
This is my base query select distinct a.projects , case when(billing_fy!=0) then(select round(((sum(cost_project)/(sum(billing_fy)/((10/12)*365)))),2) from
I have a script of the following structure: SELECT SUM(CASE WHEN pf.info IS NOT
I have the following query: SELECT sum((select count(*) as itemCount) * SalesOrderItems.price) as amount,
I have the following query: SELECT SUM(COUNTED) AS TCOUNTED FROM (SELECT COUNTED FROM `clicks`.`t1`
I have the following query? SELECT id_service, creation_date, SUM(ammount) FROM transactions WHERE DATE_FORMAT(creation_date, '%m/%d/%Y')
I have the following MySQL query: SELECT SUM(IF(num_content>3,3,num_content)) FROM ( SELECT DATE(FROM_UNIXTIME(content_timestamp)) as dt,
I have the following query: SELECT tableOneId SUM(a+b+c) AS tableOneData, MIN(d) AS tableTwoData, FROM

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.