Solved

SQL ODD EVEN or NULL Coloum needed

Posted on 2013-01-29
7
340 Views
Last Modified: 2013-01-29
Hi I have an SQL query and I want to add a coloum to it that shows if an employee is in the building.

At the moment Null is not in
Odd numbers are in and even they are out. Basicaly its how many times they have clocked in that day. This is what I want the table to look like after but at the moment only have the first 2 coloums.

Name          Count         InOrOut
name1             0             Day Off
name2             2                Out
name3             3                  In
name4             1                  In
name5             4                Out
0
Comment
Question by:taz8020
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38831882
>Null is not in.  Odd numbers are in and even they are out.
Use modulo to get the remainder of a divide by 2, then assign the label appropriately.

SELECT COALESCE(COUNT, 0) % 2 WHEN 0 THEN 'Out' WHEN 1 THEN 'In' END as InOrOut
0
 
LVL 11

Expert Comment

by:David Kroll
ID: 38831887
CASE
  WHEN (column IS NULL) OR ((column % 2) = 0)  THEN 'In'
  ELSE 'Out'
END as InOut
0
 
LVL 1

Expert Comment

by:DoutorApedeuta
ID: 38831899
Well, it would help if you showed the query that you have at the moment, but if I understand correctly, you could add the following column:

CASE WHEN COUNT(*) IS NULL THEN 'Out' WHEN COUNT(*) = 0 THEN 'Day Off' WHEN COUNT(*) % 2 = 0 THEN 'Out' ELSE 'In' END As InOrOut
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 66

Expert Comment

by:Jim Horn
ID: 38831902
<correction to my above SQL, but it looks like dkrollCTN has it too...>

SELECT CASE COALESCE(COUNT, 0) % 2 WHEN 0 THEN 'Out' WHEN 1 THEN 'In' END as InOrOut
0
 
LVL 3

Author Comment

by:taz8020
ID: 38831916
Sorry the coloum has got null in it, and this is the bit that I am stuck on

Name          Count         InOrOut
name1         NULL             Day Off
name2             2                Out
name3             3                  In
name4             1                  In
name5             4                Out
0
 
LVL 1

Accepted Solution

by:
DoutorApedeuta earned 500 total points
ID: 38831928
CASE WHEN Count IS NULL THEN 'Day Off' WHEN Count % 2 = 0 THEN 'Out' ELSE 'In' END As InOrOut
0
 
LVL 3

Author Closing Comment

by:taz8020
ID: 38831944
Your a star thanks for that, was driving me mad kept getting only 2 results on the 3 I wanted.
Thanks
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question