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

  • Home
  • SEARCH
  • 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 6211201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:17:45+00:00 2026-05-24T06:17:45+00:00

I have a complicated query I wrote and it did initially retrieve all the

  • 0

I have a complicated query I wrote and it did initially retrieve all the data I would like from it. Now I realize I require the one field’s value slightly different than what I have it now.

I have : IF(MIN(tasks.due_date) < NOW(),1,0) AS taskoverdue and I would like to have it:

IF(MIN(tasks.due_date WHERE tasks.completed != 1) < NOW(),1,0) AS taskoverdue

I know this syntax will not work, is there any manipulation anyone can think of that would achieve this result?

Due to my query being complicated I cannot alter the where clause or the group by clause, and I cannot run a sub-query within the select clause without considerably killing my application’s speed. Oh and I am using php but I am looking for a mysql solution.

Here is my full query just for kicks:

SELECT 
    tickets.id,
    tickets.user_id,
    tickets.name,
    COUNT(DISTINCT tasks.id) AS numTasks,
    IF(MIN(tasks.due_date) < NOW(),1,0) AS taskoverdue,
    IF(tickets.due_date < NOW(),1,0) AS ticketoverdue,
    tasks.queue_id AS queue_id ,
    IF(MIN(tasksteps.time_due) < NOW(),1,0) AS taskstepoverdue,
    COUNT(DISTINCT ticketnotes.ticket_id) as notes
FROM 
    tickets
    LEFT OUTER JOIN ticketsinqueues ON tickets.id = ticketsinqueues.ticket_id
    LEFT OUTER JOIN tasks ON tickets.id = tasks.ticket_id
    LEFT OUTER JOIN tasksteps ON tasks.id = tasksteps.task_id 
    LEFT OUTER JOIN ticketnotes ON tickets.id = ticketnotes.ticket_id
WHERE
    ticketsinqueues.queue_id != tasks.queue_id AND
    tickets.status = (var decided on run time)
GROUP BY
    tickets.id,tasks.queue_id
UNION
SELECT 
    tickets.id,
    tickets.user_id,
    tickets.name,
    COUNT(DISTINCT tasks.id) AS numTasks,
    IF(MIN(tasks.due_date) < NOW(),1,0) AS taskoverdue,
    IF(tickets.due_date < NOW(),1,0) AS ticketoverdue,
    tasks.queue_id AS queue_id ,
    IF(MIN(tasksteps.time_due) < NOW(),1,0) AS taskstepoverdue,
    COUNT(DISTINCT ticketnotes.ticket_id) as notes
FROM 
    tickets
    LEFT OUTER JOIN ticketsinqueues ON tickets.id = ticketsinqueues.ticket_id
    LEFT OUTER JOIN tasks ON tickets.id = tasks.ticket_id
    LEFT OUTER JOIN tasksteps ON tasks.id = tasksteps.task_id 
    LEFT OUTER JOIN ticketnotes ON tickets.id = ticketnotes.ticket_id
WHERE
    ticketsinqueues.queue_id = tasks.queue_id AND
    tickets.status = (var decided on run time)
GROUP BY
    tickets.id,tasks.queue_id
UNION
SELECT 
    tickets.id,
    tickets.user_id,
    tickets.name,
    0 AS numTasks,
    0 AS taskoverdue,
    IF(tickets.due_date < NOW(),1,0) AS ticketoverdue,
    ticketsinqueues.queue_id AS queue_id ,
    0 AS taskstepoverdue,
    0 AS notes
FROM 
    tickets 
    LEFT OUTER JOIN ticketsinqueues ON tickets.id = ticketsinqueues.ticket_id
WHERE 
tickets.status = (var decided on run time) AND
NOT EXISTS (
    SELECT 
        * 
    FROM 
        tasks 
    WHERE 
        tickets.id = tasks.ticket_id AND 
        tickets.queue_id = tasks.queue_id 
    GROUP BY 
        tasks.ticket_id,
        tasks.queue_id)
ORDER BY id desc

Any help will be appreciated.

  • 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-24T06:17:46+00:00Added an answer on May 24, 2026 at 6:17 am
    MAX(tasks.completed != 1 AND tasks.due_date < NOW()) AS taskoverdue
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have quite complicated query from which I would like to create a view.
In MySQL, I have a simple "SELECT * FROM foo" query. I would like
I have a complicated HQL query. I would like to access the Oracle specific
I have a complicated MySQL query which takes a lot of time, selecting from
I have a slightly complicated query that I'd like to have as a natural
I have a particularly complicated query for a report. It selects several columns from
I have a quite complicated query which will by built up dynamically and is
I have a pretty complicated Linq query that I can't seem to get into
Hi I have a really complicated dynamic query that i want to use to
I have a problem with (for me to complicated) MySql query. Okay, here is

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.