INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Count Several fields in same row

Count Several fields in same row

(OP)
I'm trying to count records that are past due.

table has 5 dates and a field called type

e.g.

date_1, date_2, date_3, date_4, date_5, type

I'm trying to count the above dates based on <getdate() and group them by type. so the results would show something like this

CODE

type  date_1   date_2   date_3   date_4   date_5
---------------------------------------------------
A     10       5        1        0        5
B     2        1        3        5        10 

I can't seem to figure this out.

any help would be appreciated

thanks

RE: Count Several fields in same row

Let's start with date_1

CODE

SELECT type, SUM(CASE WHEN date_1<GETDATE() THEN 1 ELSE 0 END) as date_1_past_count FROM yourtable GROUP BY type 

The rest now is just repetition, Copy&Paste and substitution date_2,3, etc.

Bye, Olaf.

PS: You better had the data not in its pivoted state. SQL works best on normalized data and your data is pivoted, not normal. A normalized schema would only have a date column and then 5 records instead of 1.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close