The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MS SQL Development
|
Query to display data a different way
Discuss Query to display data a different way in the MS SQL Development forum on Dev Shed. Query to display data a different way MS SQL Development forum discussing administration, MS SQL queries, and other MS SQL-related topics. SQL Server is Microsoft's enterprise database engine.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 25th, 2012, 01:56 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 8
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!
|

July 25th, 2012, 08:41 PM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
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
|

July 26th, 2012, 01:16 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 8
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?
|

July 26th, 2012, 02:24 AM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
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)
|

July 26th, 2012, 10:06 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 8
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.
|

July 26th, 2012, 10:54 AM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
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
|

July 26th, 2012, 11:03 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 8
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.
|

July 26th, 2012, 01:01 PM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
Quote: | Originally Posted by kpraeger ... or something similar. | use style 100, then
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|