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 <>フィドルはこちら

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Use Spark to group by consecutive same values of one column, taking Max or Min value of another column for each group

分類Dev

Excel - Get value of a column in a row where match found in another column

分類Dev

Get the value of a column where one column is max and another is min

分類Dev

Is there a way in pandas to groupby and then count unique where another column has a specified value?

分類Dev

Finding Duplicate Data Only Where Another Column Has a Different Value

分類Dev

How to count the values corresponding to each unique value in another column in a dataframe?

分類Dev

Summing values from a column based on match in another column and first distinct occurrence of value in a third column

分類Dev

mysql update values from one column in same table based on another 3 column value

分類Dev

Combining text values in a pandas dataframe column based on same value in another column

分類Dev

SQL - Get column value based on another column's average between select rows

分類Dev

Get quantile of column only if value of another column satisfies condition

分類Dev

two same rows but one column with distinct value, how to combine them and get rid of another line

分類Dev

How to get Database-table entries where a certain column is not linked to a certain value by another table?

分類Dev

Create new column that carries up the last value of another column, by group

分類Dev

Select distinct values from a column based on highest value in another column

分類Dev

Select values from one column which share a value in another column

分類Dev

Set values of column in dataframe according to the order of another column value in python

分類Dev

Copy value from one column to another based on condition (using pandas)

分類Dev

Get value from another dataframe column based on condition

分類Dev

For keywords(strings) in one column and values(floats) in another, I want to count the number ok keywords that fall per unit range of the value

分類Dev

Value in one OR another column

分類Dev

How to create a new column using R which says TRUE/FALSE if another column displays a specific value?

分類Dev

How to find corresponding column value if value exists in another column of same table

分類Dev

Excel - sum values based on distinct value in another column

分類Dev

Get values of one column based on another columns value

分類Dev

select a column value that matches with a value of another column

分類Dev

How to remove duplicate ID rows. While removing, use the rows that has NULL value in another column

分類Dev

Pandas: Adding incremental numbers to the suffix of repeating values of a column that are grouped by the value of another column and ordered by index

分類Dev

How to populate or update column value in an excel sheet based on data values in another reference column

Related 関連記事

  1. 1

    Use Spark to group by consecutive same values of one column, taking Max or Min value of another column for each group

  2. 2

    Excel - Get value of a column in a row where match found in another column

  3. 3

    Get the value of a column where one column is max and another is min

  4. 4

    Is there a way in pandas to groupby and then count unique where another column has a specified value?

  5. 5

    Finding Duplicate Data Only Where Another Column Has a Different Value

  6. 6

    How to count the values corresponding to each unique value in another column in a dataframe?

  7. 7

    Summing values from a column based on match in another column and first distinct occurrence of value in a third column

  8. 8

    mysql update values from one column in same table based on another 3 column value

  9. 9

    Combining text values in a pandas dataframe column based on same value in another column

  10. 10

    SQL - Get column value based on another column's average between select rows

  11. 11

    Get quantile of column only if value of another column satisfies condition

  12. 12

    two same rows but one column with distinct value, how to combine them and get rid of another line

  13. 13

    How to get Database-table entries where a certain column is not linked to a certain value by another table?

  14. 14

    Create new column that carries up the last value of another column, by group

  15. 15

    Select distinct values from a column based on highest value in another column

  16. 16

    Select values from one column which share a value in another column

  17. 17

    Set values of column in dataframe according to the order of another column value in python

  18. 18

    Copy value from one column to another based on condition (using pandas)

  19. 19

    Get value from another dataframe column based on condition

  20. 20

    For keywords(strings) in one column and values(floats) in another, I want to count the number ok keywords that fall per unit range of the value

  21. 21

    Value in one OR another column

  22. 22

    How to create a new column using R which says TRUE/FALSE if another column displays a specific value?

  23. 23

    How to find corresponding column value if value exists in another column of same table

  24. 24

    Excel - sum values based on distinct value in another column

  25. 25

    Get values of one column based on another columns value

  26. 26

    select a column value that matches with a value of another column

  27. 27

    How to remove duplicate ID rows. While removing, use the rows that has NULL value in another column

  28. 28

    Pandas: Adding incremental numbers to the suffix of repeating values of a column that are grouped by the value of another column and ordered by index

  29. 29

    How to populate or update column value in an excel sheet based on data values in another reference column

ホットタグ

アーカイブ