MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 21st, 2012, 09:34 AM
kloud kloud is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 15 kloud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 48 sec
Reputation Power: 0
SQL Query Help

Hi,

I have 2 tables. Attendant and AttendanceRecord. I want to log attendance in the attendance record table (obviously!).

I want to present the user with a dataset which has the name of the attendant and a flag as to whether they have attended on a particular date or not. So the user will set a date in control and the query will return a set of all attendees and whether they attended or not.

Have tried a number of different queries and getting close but nothing definitive.

Help!

Reply With Quote
  #2  
Old June 21st, 2012, 10:20 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,349 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 10 m 28 sec
Reputation Power: 390
Code:
select Attendant.name,
       case when AttendanceRecord.AttendanceDate = <given date> then 'Present' else 'Not present' end
  from attendant
  left
  join AttendanceRecord
    on attendant.pk = AttendanceRecord.fk
   and AttendanceRecord.AttendanceDate = <given date>


Change column names as apropriate.

Reply With Quote
  #3  
Old June 21st, 2012, 03:26 PM
kloud kloud is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 15 kloud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 48 sec
Reputation Power: 0
Thanks a million - this does the trick.

Only thing is that in order to fit it into the tool I'm using the join isn't the easiest thing to use. Just wondering if there's any other way of doing this other than using a join on the date - is it possible to do using a where clause?

Why is the join necessary?

Reply With Quote
  #4  
Old June 25th, 2012, 09:47 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,349 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 10 m 28 sec
Reputation Power: 390
Code:
select Attendant.name,
       case when exists(select * 
                          from AttendanceRecord
                         where AttendanceDate = <given date>
                           and attendant.pk = AttendanceRecord.fk) then 
          'Present' else 'Not present' end
  from attendant

Reply With Quote
  #5  
Old June 25th, 2012, 10:31 AM
kloud kloud is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 15 kloud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 48 sec
Reputation Power: 0
Thanks SB.

That's perfect. Much appreciated.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > SQL Query Help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap