TSQL: Find odd and even numbers in each column for all rows

Rocco

I have a table with 6 columns containing random numbers from 1 to 90.


nr1 nr2 nr3 nr4 nr5 nr6
-----------------------
12  26  70  74  84  87
6   13  19  37  67  72
29  31  35  49  69  74
3   14  30  50  66  87
6   10  13  17  22  46
28  29  33  35  65  80
25  31  43  61  63  86
12  20  22  39  55  72
9   12  28  71  82  85
5   13  28  30  42  63
33  37  48  65  83  84
3   10  40  54  69  85
6   19  30  53  55  76
17  41  42  43  66  76
2   22  28  39  61  79
26  37  53  81  86  90
2   51  55  57  61  82
1   18  30  34  65  75
18  28  40  63  68  86

I need to produce a query that will provide the following result:

Total ODD numbers in a table: 
Total EVEN numbers in a table: 
Total number of rows containing 6 even numbers:
Total number of rows containing 6 odd numbers:
Total number of rows containing 5 even and 1 odd number:
Total number of rows containing 5 odd and 1 even number:
Total number of rows containing 4 even and 2 odd numbers:
Total number of rows containing 4 odd and 2 even numbers:
Total number of rows containing 3 even and 3 odd numbers:

So far I could figure out only how to calculate number of rows containing only even or odd numbers, but I'm stuck on the rest.

SELECT COUNT(*) AS ROWS_ODDS FROM table
  WHERE nr1 %2!=0 AND nr2 %2!=0 AND nr3 %2!=0 AND nr4 %2!=0 AND nr5 %2!=0 AND nr6 %2!=0

  SELECT COUNT(*) AS ROWS_EVENS FROM table
  WHERE nr1 %2=0 AND nr2 %2=0 AND nr3 %2=0 AND nr4 %2=0 AND nr5 %2=0 AND nr6 %2=0

Any ideas? Thank you!

dnoeth

Simply sum the modulo results:

with cte as 
 (
   SELECT 
      nr1 %2 + nr2 %2 + nr3 %2 + nr4 %2 + nr5 %2 + nr6 %2 as odd_values
   FROM tab
 )
select odd_values, 6-odd_values as even_values, count(*)
from cte 
group by odd_values
-- this adds the grand total
union all
select
   sum(odd_values), sum(6-odd_values), -1
from cte

Formatting the output is up to you :-)

See fiddle

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

Get even / odd / all numbers between two numbers

分類Dev

Get even / odd / all numbers between two numbers

分類Dev

Find the sum of all odd Fibonacci numbers less than 2 million

分類Dev

Given two even numbers, find the sum of the squares of all even numbers between them

分類Dev

Combine odd and even indexed rows in pandas

分類Dev

Help with script reading numbers from a file and determining if they are even or odd numbers

分類Dev

TSQL Multiple column unpivot with named rows possible?

分類Dev

Multi threaded java program to print even and odd numbers alternatively

分類Dev

"Removing" even numbers from array and moving odd to the front

分類Dev

How to read until EOF and print the even/odd numbers entered?

分類Dev

How to specify the number of odd and even numbers from a random generator in R?

分類Dev

Duplicate each element of a vector and add "a" to odd and "b" to even elements

分類Dev

upperCase even characters and lowerCase odd characters in each element of an array in JavaScript

分類Dev

Calculate difference of adjacent rows (decimal numbers) in a data frame for each group defined in a different column

分類Dev

How to print even numbers in ascending order and odd numbers in descending order without using collection

分類Dev

find and print each unique combination that sums to 100 and return a count of all such combinations for numbers between 1 and 100

分類Dev

function doesnt print out all the even numbers?

分類Dev

Convert all rows of a padas dataframe column to comma separated values with each value in single quote

分類Dev

SQL - summing up minutes in the table for all the rows with the same month as their date and store it in a column for each row

分類Dev

Even/Odd parity for Even/Odd values

分類Dev

TSQL: Join columns, but rows in one column have multiple values

分類Dev

How to return all rows even if the query is NULL

分類Dev

How do I find all rows such that the string in a column is a substring of a given string in Pandas

分類Dev

How do I determine how many odd and even numbers are in a text file?

分類Dev

How to find three column values at different levels using TSQL?

分類Dev

How can I skip even/odd rows while reading a csv file?

分類Dev

How to select from SQL table so even and odd rows would be in separate columns?

分類Dev

Different Column number for each rows in GridView android

分類Dev

How to find all the ranges in an array of numbers

Related 関連記事

  1. 1

    Get even / odd / all numbers between two numbers

  2. 2

    Get even / odd / all numbers between two numbers

  3. 3

    Find the sum of all odd Fibonacci numbers less than 2 million

  4. 4

    Given two even numbers, find the sum of the squares of all even numbers between them

  5. 5

    Combine odd and even indexed rows in pandas

  6. 6

    Help with script reading numbers from a file and determining if they are even or odd numbers

  7. 7

    TSQL Multiple column unpivot with named rows possible?

  8. 8

    Multi threaded java program to print even and odd numbers alternatively

  9. 9

    "Removing" even numbers from array and moving odd to the front

  10. 10

    How to read until EOF and print the even/odd numbers entered?

  11. 11

    How to specify the number of odd and even numbers from a random generator in R?

  12. 12

    Duplicate each element of a vector and add "a" to odd and "b" to even elements

  13. 13

    upperCase even characters and lowerCase odd characters in each element of an array in JavaScript

  14. 14

    Calculate difference of adjacent rows (decimal numbers) in a data frame for each group defined in a different column

  15. 15

    How to print even numbers in ascending order and odd numbers in descending order without using collection

  16. 16

    find and print each unique combination that sums to 100 and return a count of all such combinations for numbers between 1 and 100

  17. 17

    function doesnt print out all the even numbers?

  18. 18

    Convert all rows of a padas dataframe column to comma separated values with each value in single quote

  19. 19

    SQL - summing up minutes in the table for all the rows with the same month as their date and store it in a column for each row

  20. 20

    Even/Odd parity for Even/Odd values

  21. 21

    TSQL: Join columns, but rows in one column have multiple values

  22. 22

    How to return all rows even if the query is NULL

  23. 23

    How do I find all rows such that the string in a column is a substring of a given string in Pandas

  24. 24

    How do I determine how many odd and even numbers are in a text file?

  25. 25

    How to find three column values at different levels using TSQL?

  26. 26

    How can I skip even/odd rows while reading a csv file?

  27. 27

    How to select from SQL table so even and odd rows would be in separate columns?

  28. 28

    Different Column number for each rows in GridView android

  29. 29

    How to find all the ranges in an array of numbers

ホットタグ

アーカイブ