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 July 25th, 2012, 01:56 PM
kpraeger kpraeger is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 8 kpraeger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 25 sec
Reputation Power: 0
Query to display data a different way

I have a table with three fields. This table (confirmation) contains up to 4 records for each student noting what documents they authorized (code) and a timestamp (date).

So confirmation table data looks like:
StudentID,Code,Date
123,A,2012-05-01
123,B,2012-06-12
123,C,2012-06-13
123,D,2012-07-12

What I would like to accomplish is create a query to display the information in this way (first row would be column names, one line per student):
StudentID,CodeA,CodeB,CodeC,CodeD
123,2012-05-01,2012-06-12,2012-06-13,2012-07-12

Any help would be greatly appreciated, since I am still learning SQL. In case you need to know, we use MS-SQL. I apologize if I am not using the correct terminology. Thanks!

Reply With Quote
  #2  
Old July 25th, 2012, 08:41 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 32 m 9 sec
Reputation Power: 4140
Code:
SELECT StudentID
     , MAX(CASE WHEN Code = 'A' 
                THEN [Date]
                ELSE NULL END) AS CodeA
     , MAX(CASE WHEN Code = 'B' 
                THEN [Date]
                ELSE NULL END) AS CodeB
     , MAX(CASE WHEN Code = 'C' 
                THEN [Date]
                ELSE NULL END) AS CodeC
     , MAX(CASE WHEN Code = 'D' 
                THEN [Date]
                ELSE NULL END) AS CodeD
  FROM confirmation
GROUP
    BY StudentID
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old July 26th, 2012, 01:16 AM
kpraeger kpraeger is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 8 kpraeger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 25 sec
Reputation Power: 0
Thanks that worked perfectly! Now I just need to format the date as mm/dd/yy hh:mm pm. Is there a easy trick to do that?

Reply With Quote
  #4  
Old July 26th, 2012, 02:24 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 32 m 9 sec
Reputation Power: 4140
Quote:
Originally Posted by kpraeger
Thanks that worked perfectly! Now I just need to format the date as mm/dd/yy hh:mm pm. Is there a easy trick to do that?

1. do it in your application layer (best practice)

or

2. pick style 100 from http://msdn.microsoft.com/en-us/library/ms187928.aspx (although that uses Jul instead of 07, so you could use 12 nested REPLACE functions)

Reply With Quote
  #5  
Old July 26th, 2012, 10:06 AM
kpraeger kpraeger is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 8 kpraeger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 25 sec
Reputation Power: 0
Thanks. Do you have a suggestion on a good application layer? I simply am providing a spreadsheet type list that can be printed.

Reply With Quote
  #6  
Old July 26th, 2012, 10:54 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 32 m 9 sec
Reputation Power: 4140
Quote:
Originally Posted by kpraeger
Do you have a suggestion on a good application layer?
nope, sorry, i'm not a programmer

and anyhow, what the heck is wrong with printing 2012-07-26?

i don't think ~anybody~ will get confused by that

however, if you print 03/04/2012, many people will take it the wrong way

Reply With Quote
  #7  
Old July 26th, 2012, 11:03 AM
kpraeger kpraeger is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 8 kpraeger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 25 sec
Reputation Power: 0
Quote:
Originally Posted by r937
nope, sorry, i'm not a programmer

and anyhow, what the heck is wrong with printing 2012-07-26?

i don't think ~anybody~ will get confused by that

however, if you print 03/04/2012, many people will take it the wrong way


I'm fine with 2012-07-26, but I just don't want 2012-07-26 10:29:38.130 which is how it looks when I run the query. I'd rather just have 2012-07-26 10:29 AM, or something similar.

Reply With Quote
  #8  
Old July 26th, 2012, 01:01 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 32 m 9 sec
Reputation Power: 4140
Quote:
Originally Posted by kpraeger
... or something similar.
use style 100, then

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Query to display data a different way

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