Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for Write a query to calculate the even and odd records from a table in postgreSql how can I solve this question

$
0
0

Use the modulus operator %. It returns the remainder from division. 1 % 2 is 1, 2 % 2 is 0, 3 % 2 is 1... so if x % 2 is true it is odd.

Then you can use a case to turn this into 'odd' or 'even'.

select  *,  case when EmpID % 2 then 'odd' else 'even' endfrom Employees

Or you can select only the odd rows.

select *from Employeeswhere EmpID % 2

Or the even rows.

select *from Employeeswhere not EmpID % 2

Viewing all articles
Browse latest Browse all 581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>