SQL : Two Columns Count Third Column Sum of 1st and 2nd Column

Nixon

im trying to create two columns thats counting different items and then on the Third Column SUM up column 1 and 2. Any help is appreciated! Thanks!

SELECT 
    (SELECT COUNT(ecarrno) Conventional FROM L16T3
     WHERE (l16lcode = 46) AND
        (l46adr IN ('680', '657','693','623','639','704','644','679'))),
    (SELECT COUNT(admunit) Auto FROM L16T3
    WHERE (admunit= 16AP) AND
        (l46adr IN ('611','618','637','638'))),
    SUM (COUNT(ecarrno) + COUNT(admunit))
    FROM L16T3
    AND DATREG >= @('START DATE',datreg)  
    AND DATREG <= @('END DATE',datreg)
ORDER BY datreg 
DESC,l16seqno DESC
Gordon Linoff

You can use conditional aggregation and a subquery. The query looks something like this:

SELECT Conventional, Auto, (Conventional + Auto)
FROM (SELECT SUM(CASE WHEN l16lcode = 46 AND l46adr IN ('680', '657', '693', '623', '639', '704', '644', '679')
                      THEN 1 ELSE 0
                 END) as Conventional,
             SUM(CASE WHEN admunit = '16AP' AND l46adr IN ('611', '618', '637', '638')
                      THEN 1 ELSE 0
                 END) as Auto
      FROM L16T3 l
     WHERE DATREG >= @('START DATE', datreg)   AND DATREG <= @('END DATE', datreg)     
    ) l
ORDER BY datreg ;

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

How do I groupby with one key column, using condition on 2nd column and adding third column separately for weekday and weekend?

分類Dev

Compare two dataset and add coulm in 1st dataset if exists in 2nd dataset

分類Dev

pandas Third Column answer is based on column 1 and column 2

分類Dev

SQL statement - get 1st minimum then 2nd minimum from join

分類Dev

SQL Update a column dependent on other two columns

分類Dev

Show One Column Data as Two Columns in SQL

分類Dev

R loop to copy content from two columns, according a third column, and output into a fourth column

分類Dev

Python Pandas: Find Sum of Column Based on Value of Two other Columns

分類Dev

How can I print in one column the SUM of two columns

分類Dev

Turning 1 date column with 2 categories into two date columns with VBA

分類Dev

How can I Select 1st, 2nd and 3rd values in different columns - Ms Access

分類Dev

combine two columns in 1 column mysql

分類Dev

SQL column sum and difference

分類Dev

SQL: select distinct on column1 and sum up column2 values of merged rows

分類Dev

SQLビュー(id + count_table1_column1 + count_table2_column_1)

分類Dev

How to SQL PIVOT on two columns and with dynamic column names?

分類Dev

Oracle SQL - Show percentage of two columns in new column

分類Dev

Position of 2nd column in row in HTML table

分類Dev

How to split a column in two columns

分類Dev

Query 2nd table with value of the 1st

分類Dev

Move 1st column header to each row in certain width

分類Dev

dplyr:: Sum columns x1:x5 excluding column whose name == value of column y

分類Dev

dplyr:: Sum columns x1:x5 excluding column whose name == value of column y

分類Dev

PIVOT/count sum of values in a column - Mysql

分類Dev

Groupby a column and find its sum and count

分類Dev

How to count the sum of distinct Excel values in a column?

分類Dev

SQL Server - Rows to column based on third column value

分類Dev

Count occurrence of pair wise values in a column grouped by a unique combination of two other columns in r

分類Dev

How to merge 2 columns on 1 column

Related 関連記事

  1. 1

    How do I groupby with one key column, using condition on 2nd column and adding third column separately for weekday and weekend?

  2. 2

    Compare two dataset and add coulm in 1st dataset if exists in 2nd dataset

  3. 3

    pandas Third Column answer is based on column 1 and column 2

  4. 4

    SQL statement - get 1st minimum then 2nd minimum from join

  5. 5

    SQL Update a column dependent on other two columns

  6. 6

    Show One Column Data as Two Columns in SQL

  7. 7

    R loop to copy content from two columns, according a third column, and output into a fourth column

  8. 8

    Python Pandas: Find Sum of Column Based on Value of Two other Columns

  9. 9

    How can I print in one column the SUM of two columns

  10. 10

    Turning 1 date column with 2 categories into two date columns with VBA

  11. 11

    How can I Select 1st, 2nd and 3rd values in different columns - Ms Access

  12. 12

    combine two columns in 1 column mysql

  13. 13

    SQL column sum and difference

  14. 14

    SQL: select distinct on column1 and sum up column2 values of merged rows

  15. 15

    SQLビュー(id + count_table1_column1 + count_table2_column_1)

  16. 16

    How to SQL PIVOT on two columns and with dynamic column names?

  17. 17

    Oracle SQL - Show percentage of two columns in new column

  18. 18

    Position of 2nd column in row in HTML table

  19. 19

    How to split a column in two columns

  20. 20

    Query 2nd table with value of the 1st

  21. 21

    Move 1st column header to each row in certain width

  22. 22

    dplyr:: Sum columns x1:x5 excluding column whose name == value of column y

  23. 23

    dplyr:: Sum columns x1:x5 excluding column whose name == value of column y

  24. 24

    PIVOT/count sum of values in a column - Mysql

  25. 25

    Groupby a column and find its sum and count

  26. 26

    How to count the sum of distinct Excel values in a column?

  27. 27

    SQL Server - Rows to column based on third column value

  28. 28

    Count occurrence of pair wise values in a column grouped by a unique combination of two other columns in r

  29. 29

    How to merge 2 columns on 1 column

ホットタグ

アーカイブ