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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:32:58+00:00 2026-06-10T11:32:58+00:00

Hope everyone is doing well. I have the following output… +———+————–+————–+———–+———+———-+ | ord_num |

  • 0

Hope everyone is doing well.

I have the following output…

+---------+--------------+--------------+-----------+---------+----------+
| ord_num | signoff_date | program_name | prod_desc | tx_comp | priority |
+---------+--------------+--------------+-----------+---------+----------+
| 1234567 | 2012-08-12   | ilearn       | run       |       1 |        1 |
| 1234567 | 2012-08-12   | ilearn       | plan      |       1 |        1 |
| 1234568 | 2012-08-12   | other        | run       |       1 |        1 |
| 1234569 | 2012-08-12   | other        | run       |       0 |        1 |
+---------+--------------+--------------+-----------+---------+----------+

What I would like to do is SUM the tx_comp column once per unique “ord_num”.

Now I cant use GROUP BY ord_num as I also do a sum on the type of tasks.

Its like I need to know what the previous ord_num was then sum if different?

Any ideas would be greatly appreciated.

Thanks.

* EDIT *

SELECT 
    signoff_date,
    SUM(IF(prod_desc = 'run', 1, 0)) AS run,
    SUM(IF(prod_desc = 'plan', 1, 0)) AS plan,
    SUM(tx_comp) AS tx_comp 
FROM
(
    SELECT 
        ord_num,
        signoff_date,
        program_name,
        prod_desc,
        tx_comp,
        priority 
    FROM
        test.orders 
        LEFT JOIN test.tx_comp USING (ord_num)
) AS grp

Obviously not the desired output

+--------------+------+------+---------+
| signoff_date | run  | plan | tx_comp |
+--------------+------+------+---------+
| 2012-08-12   |    3 |    1 |       3 |
+--------------+------+------+---------+

I am after…

+--------------+------+------+---------+
| signoff_date | run  | plan | tx_comp |
+--------------+------+------+---------+
| 2012-08-12   |    3 |    1 |       2 |
+--------------+------+------+---------+
  • 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-10T11:32:59+00:00Added an answer on June 10, 2026 at 11:32 am

    If the value of tx_comp is always 1 or zero, then we can leverage COUNT(), which may give us more options. For instance, we can count the distinct ord_num where tx_comp is 1:

    COUNT(distinct IF(tx_comp, ord_num, NULL))
    

    Which gives me a final query of:

    SELECT signoff_date,
      SUM(IF(prod_desc = 'run', 1, 0)) AS run,
      SUM(IF(prod_desc = 'plan', 1, 0)) AS plan,
      COUNT(distinct IF(tx_comp, ord_num, NULL)) as tx_comp
    FROM 
      test.orders
      JOIN test.tx_comp USING (ord_num)
      GROUP BY signoff_date
    

    And there is no need for the subquery in this case. (edit: updated for your JOIN)

    I have tested this with your sample data; the only dependency is on the semantic nature of tx_comp. You have been saying “SUM”, and this assumes that the value will be at most 1 (I understand it to be a boolean flag, and in a comment on another answer you mentioned MAX(tx_comp) returning 1, so I think we’re good).

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

Sidebar

Related Questions

hope every one is doing well :) I have struck with retrieving the month
Hope everyone is doing well I am having a problem with make files in
HI everyone I hope you can help me, I have a problem with a
Morning all, hope everyone is ok. I have an ODS that uses a combination
I hope everyone will enjoy reading this. I have two IF statements below public
Hope everyone here are doing great! Here is a situation where I need to
Hope everyone is enjoying this wonderful Aloha Friday, I have a question about HTML
i hope that everyone here know the php 'variable variable' syntax: $color = 'red';
Hope the AWK gurus can provide a solution to my problem . I have
Hope someone could help me with this: I would like to have a column

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.