SQL: count rows where column = a value AND another column is the same as values in the group where the first condition is true?

Jacob Myer

I need to count the number of rows for each employee where the status = 9 and the date is the same as other rows within that employee_id where the status = 9. I also want the employee_id and count_all, a count of all the rows for each employee.

Figuring out how to get the count_same_date_status_9 column is where I am stuck. These dates are made up just for the example so I dont want to put them explicitly into the query.

My table:

employee_id     status     date
1               9          10/19/2020
1               7          07/16/2001
1               9          10/19/2020
2               5          08/11/2011
2               9          12/25/2012
2               9          11/19/2013
3               5          06/05/2016
3               4          01/01/2021
4               9          02/15/2018
4               9          02/15/2018
4               9          02/15/2018

I want to return the following:

employee_id     count_same_date_status_9     count_all
1               2                            3
2               0                            3
3               0                            2
4               3                            3
MT0

You can use:

SELECT employee_id,
       MAX( CASE WHEN status = 9 AND cnt > 1 THEN cnt ELSE 0 END ) AS count_9,
       SUM( cnt ) AS count_all
FROM   (
  SELECT employee_id,
         status,
         dt,
         COUNT(*) AS cnt
  FROM   table_name
  GROUP BY employee_id, status, dt
)
GROUP BY employee_id
ORDER BY employee_id;

Which, for your sample data:

CREATE TABLE table_name ( employee_id, status, dt ) AS
SELECT 1, 9, DATE '2020-10-19' FROM DUAL UNION ALL
SELECT 1, 7, DATE '2001-07-16' FROM DUAL UNION ALL
SELECT 1, 9, DATE '2020-10-19' FROM DUAL UNION ALL
SELECT 2, 5, DATE '2011-08-11' FROM DUAL UNION ALL
SELECT 2, 9, DATE '2012-12-25' FROM DUAL UNION ALL
SELECT 2, 9, DATE '2013-11-19' FROM DUAL UNION ALL
SELECT 3, 5, DATE '2016-06-05' FROM DUAL UNION ALL
SELECT 3, 4, DATE '2021-01-01' FROM DUAL UNION ALL
SELECT 4, 9, DATE '2018-02-15' FROM DUAL UNION ALL
SELECT 4, 9, DATE '2018-02-15' FROM DUAL UNION ALL
SELECT 4, 9, DATE '2018-02-15' FROM DUAL;

Outputs:

EMPLOYEE_ID | COUNT_9 | COUNT_ALL
----------: | ------: | --------:
          1 |       2 |         3
          2 |       0 |         3
          3 |       0 |         2
          4 |       3 |         3

db<>fiddle here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count frequency of value in pandas column where values in another column are similar

From Dev

SQL - How to count number of distinct values (payments), after sum of rows where they have another column value (Due Date) in common

From Dev

Oracle SQL Query to retrieve records where a value in a column equals the count of values in another table

From Dev

Query where table has multiple values for a column and the same value in another?

From Dev

How to update the value in a column based on another value in the same column where both rows have the same value in another column?

From Dev

Column value be max value of another column where another value is the same

From Dev

Add a new column to the data frame where the new column is the smallest date value of a group where another column matches a condition

From Dev

MySQL - Select rows where column value is only 0, group by another column?

From Mysql

Display value in column although it is excluded by a WHERE condition over another column

From Dev

Filter rows where column value is in list in another column?

From Dev

Split pandas column into multiple rows, where splitting is on the value of another column

From Dev

Getting value of a column where another column is minimum from group

From Dev

count in between values in a column on condition say I need to count for only specific value and group them by another column using python pandas

From Dev

Count rows of a column (while prioritizing a certain name) where the sum value for the corresponding rows in another column does not exceed an amount

From Dev

Ordering by where one column is exactly the same value as another column

From Dev

MySQL get count of all rows in another table where the value equals column in current table

From Dev

Select rows with the same value in one column and specific values in another column

From Dev

How to get values in one column where another column equals a value with SQL, and store into an array in PHP?

From Dev

SQL - Replace column values with another table column ONLY where they match, else keep original value?

From Dev

Excel VBA Concatenate values in another column if the value in first column is the same

From Dev

Remove rows where value in one column equals value in another

From Dev

Check if each column values exist in another dataframe column where another column value is the column header

From Dev

count number of rows before a value group by another column in pandas dataframe

From Dev

Check if condition is true and if so add value to another column in sql

From Dev

Replace excel rows from another sheet where column value matches

From Dev

Find rows where value in column not found in another row

From Dev

SQL group by: select value where another column has its min/max

From Dev

find all rows whose one column belongs to the same value and also another column satisfy a condition on SQL server 2008

From Dev

SQL Select where a column contains one value but not another in another row

Related Related

  1. 1

    Count frequency of value in pandas column where values in another column are similar

  2. 2

    SQL - How to count number of distinct values (payments), after sum of rows where they have another column value (Due Date) in common

  3. 3

    Oracle SQL Query to retrieve records where a value in a column equals the count of values in another table

  4. 4

    Query where table has multiple values for a column and the same value in another?

  5. 5

    How to update the value in a column based on another value in the same column where both rows have the same value in another column?

  6. 6

    Column value be max value of another column where another value is the same

  7. 7

    Add a new column to the data frame where the new column is the smallest date value of a group where another column matches a condition

  8. 8

    MySQL - Select rows where column value is only 0, group by another column?

  9. 9

    Display value in column although it is excluded by a WHERE condition over another column

  10. 10

    Filter rows where column value is in list in another column?

  11. 11

    Split pandas column into multiple rows, where splitting is on the value of another column

  12. 12

    Getting value of a column where another column is minimum from group

  13. 13

    count in between values in a column on condition say I need to count for only specific value and group them by another column using python pandas

  14. 14

    Count rows of a column (while prioritizing a certain name) where the sum value for the corresponding rows in another column does not exceed an amount

  15. 15

    Ordering by where one column is exactly the same value as another column

  16. 16

    MySQL get count of all rows in another table where the value equals column in current table

  17. 17

    Select rows with the same value in one column and specific values in another column

  18. 18

    How to get values in one column where another column equals a value with SQL, and store into an array in PHP?

  19. 19

    SQL - Replace column values with another table column ONLY where they match, else keep original value?

  20. 20

    Excel VBA Concatenate values in another column if the value in first column is the same

  21. 21

    Remove rows where value in one column equals value in another

  22. 22

    Check if each column values exist in another dataframe column where another column value is the column header

  23. 23

    count number of rows before a value group by another column in pandas dataframe

  24. 24

    Check if condition is true and if so add value to another column in sql

  25. 25

    Replace excel rows from another sheet where column value matches

  26. 26

    Find rows where value in column not found in another row

  27. 27

    SQL group by: select value where another column has its min/max

  28. 28

    find all rows whose one column belongs to the same value and also another column satisfy a condition on SQL server 2008

  29. 29

    SQL Select where a column contains one value but not another in another row

HotTag

Archive