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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:57:23+00:00 2026-06-15T07:57:23+00:00

I have this query : SELECT p.name, COUNT(DISTINCT t.keyTask) AS totalTasksCount, SUM(DISTINCT CASE WHEN

  • 0

I have this query :

SELECT
    p.name,
    COUNT(DISTINCT t.keyTask) AS totalTasksCount,
    SUM(DISTINCT CASE WHEN t.keyPriority = 24 AND (t.keyState = 16 OR t.keyState = 17) THEN 1 ELSE 0 END) AS highPriorityTasksCount,
    SUM(DISTINCT CASE WHEN t.keyState = 16 OR t.keyState = 17 THEN 1 ELSE 0 END) AS activesTasksCount,
    SUM(DISTINCT t.estimatedDuration) AS estimatedDuration,
    SUM(TIMESTAMPDIFF(SECOND,wp.start,wp.end)) * 1000 AS workedDuration
FROM projects_projects p
LEFT JOIN projects_tasks t
    ON p.keyProject = t.keyProject
LEFT JOIN projects_workPeriods wp
    ON wp.keyTask = t.keyTask
LEFT JOIN common_organizations o
    ON o.keyOrganization = p.keyOrganization
LEFT JOIN common_users uc
    ON uc.keyUser = p.keyUserCreator
LEFT JOIN common_users uu
    ON uu.keyUser = p.keyUserUpdater
GROUP BY
    p.keyProject
ORDER BY
    highPriorityTasksCount DESC,
    activesTasksCount DESC,
    p.updated DESC,
    p.name;

But the result fields highPriorityTasksCount and activesTasksCount returns 0 or 1 which is normal with this query. I was wondering if there is any way to make DISTINCTROW work on row and not on case result value for these fields without subquery ?

Current result :

p.name,
totalTasksCount,
highPriorityTasksCount,
activesTasksCount,
estimatedDuration,
workedDuration

'Project 1',  '4', '1', '1',  '14400000',  '15300000'
'Project 2', '48', '1', '1',  '84600000', '503100000'
'Project 3',  '6', '1', '1', '108000000',        NULL
'Project 4',  '4', '1', '1',  '25200000',  '30600000'
'Project 5',  '5', '1', '1', '226800000',  '39600000'
'Project 6',  '2', '0', '1',        NULL,  '10800000'
'Project 7',  '9', '0', '1',        NULL,  '36900000'

Expected result :

'Project 1',  '4', '1', '1',  '14400000',  '15300000'
'Project 2', '48','20', '2',  '84600000', '503100000'
'Project 3',  '6', '1', '1', '108000000',        NULL
'Project 4',  '4', '4', '2',  '25200000',  '30600000'
'Project 5',  '5', '5', '1', '226800000',  '39600000'
'Project 6',  '2', '0', '1',        NULL,  '10800000'
'Project 7',  '9', '0', '1',        NULL,  '36900000'

EDIT :

Modified query with subqueries since there is no way, help about optimization would be appreciated, this one is working :

SELECT
    p.name,
    COUNT(DISTINCT t.keyTask) AS totalTasksCount,
    /* OLD SUM(DISTINCT CASE WHEN t.keyPriority = 24 AND (t.keyState = 16 OR t.keyState = 17) THEN 1 ELSE 0 END) AS highPriorityTasksCount, */
    (
        SELECT
    SUM(CASE WHEN st.keyPriority = 24 AND (st.keyState = 16 OR st.keyState = 17) THEN 1 ELSE 0 END)
    FROM projects_tasks st
    WHERE
    st.keyProject = p.keyProject
) AS highPriorityTasksCount,
    /* OLD SUM(DISTINCT CASE WHEN t.keyState = 16 OR t.keyState = 17 THEN 1 ELSE 0 END) AS activesTasksCount, */
    (
        SELECT
    SUM(CASE WHEN st.keyState = 16 OR st.keyState = 17 THEN 1 ELSE 0 END)
    FROM projects_tasks st
    WHERE
    st.keyProject = p.keyProject
) AS activesTasksCount,
    SUM(t.estimatedDuration) AS estimatedDuration,
    SUM(TIMESTAMPDIFF(SECOND,wp.start,wp.end)) * 1000 AS workedDuration
FROM projects_projects p
LEFT JOIN projects_tasks t
    ON p.keyProject = t.keyProject
LEFT JOIN projects_workPeriods wp
    ON wp.keyTask = t.keyTask
LEFT JOIN common_organizations o
    ON o.keyOrganization = p.keyOrganization
LEFT JOIN common_users uc
    ON uc.keyUser = p.keyUserCreator
LEFT JOIN common_users uu
    ON uu.keyUser = p.keyUserUpdater
GROUP BY
    p.keyProject
ORDER BY
    highPriorityTasksCount DESC,
    activesTasksCount DESC,
    p.updated DESC,
    p.name;
  • 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-15T07:57:26+00:00Added an answer on June 15, 2026 at 7:57 am

    without subquery.

    No, you will have to use a subquery, or there’s something I don’t know about MySQL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working query as SELECT p.id, p.name AS ProductName, count(DISTINcT s.salesid) as
I have this query: Select (Country.Name + ', ' + City.Name) as Location ...
I have this working query: $q = $this->db->query('SELECT u.name FROM users u JOIN user_group
i have this sql query select * from table where name like ? but
I have a query like this: select empno,name from emp where job = 'CLERK'
I have a query like this SELECT TOWN, NAME FROM CINEMA WHERE CITY_ID =
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
i have this query: SELECT `completed`.`ID` AS `ID`,`completed`.`level` AS `level`,`completed`.`completed_in` AS `completed_in`, COUNT(1) AS
i have a query that goes like this: $get_feed_amount = mysql_query( SELECT COUNT(*) as
I have a query like : SELECT DISTINCT g.thumb, h.hotel_name, h.id, COUNT(c.id) as total_comments,

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.