Connecting Tech Pros Worldwide Help | Site Map

how to generate the following output in sql ?

  #1  
Old November 10th, 2008, 05:15 PM
skywalker
Guest
 
Posts: n/a
I am really really new to sql,please forgive my
ignorence.

here is my problem

I have this table named mis_outtrack

desc mis_outtrack;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| pub_code | varchar(3) | YES | | NULL | |
| status_flag | varchar(2) | YES | | NULL | |
| user_id | varchar(15) | YES | | NULL | |
| entry_date | date | YES | | NULL | |
| pub_date | date | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+

select * from mis_outtrack;
+----------+-------------+---------+------------+------------+
| pub_code | status_flag | user_id | entry_date | pub_date |
+----------+-------------+---------+------------+------------+
| SND | RE | AMY | 2008-11-01 | 2008-11-01 |
| SND | AU | AMY | 2008-11-05 | 2008-11-01 |
| SND | SE | AMY | 2008-11-10 | 2008-11-01 |
| MND | RE | AMY | 2008-11-03 | 2008-11-02 |
| MND | AU | AMY | 2008-11-15 | 2008-11-02 |
| MND | SE | AMY | 2008-11-15 | 2008-11-02 |
+----------+-------------+---------+------------+------------+

here pub_code is short name for a particular news paper
useid is the user who is updating the records
pub_date is the date of publication of that news paper
and
entry_date is the date when the user is updating the inserting
the status of that publication

here RE means recieved
AU means audited
SE means send
there can only be these three status flags


I have to write an sql to display this data in the following format
------------------------------------------------------------------------------------------------------------
pub_code |entry_date |entry_date |entry_date |
pub_date |
(for a |(when | (when |
(when |(publication |
particular |status was RE)|status was AU)|status was SE |date
for |
pub_code) | |
| |that news paper|
--------------------------------------------------------------------------------------------------------------
| SND 2008-11-01 2008-11-05 2008-11-10
2008-11-01
| MND 2008-11-03 2008-11-15 2008-11-15
2008-11-02
|
|


how can I do this in sql


any help will be appriciated.

Thanks in advance
  #2  
Old November 10th, 2008, 05:25 PM
Plamen Ratchev
Guest
 
Posts: n/a

re: how to generate the following output in sql ?


Here is how you can pivot the dates. Note the use of MAX for the
pub_date, seems it is the same for each status, so MAX or MIN will work.

SELECT pub_code,
MAX(CASE WHEN status_flag = 'RE' THEN entry_date END) AS
entry_date_re,
MAX(CASE WHEN status_flag = 'AU' THEN entry_date END) AS
entry_date_au,
MAX(CASE WHEN status_flag = 'SE' THEN entry_date END) AS
entry_date_se,
MAX(pub_date) AS pub_date
FROM mis_outtrack
GROUP BY pub_code;

--
Plamen Ratchev
http://www.SQLStudio.com
  #3  
Old November 10th, 2008, 10:55 PM
Erland Sommarskog
Guest
 
Posts: n/a

re: how to generate the following output in sql ?


skywalker (mail.supratim@gmail.com) writes:
Quote:
I am really really new to sql,please forgive my
ignorence.
>
here is my problem
>
I have this table named mis_outtrack
>
desc mis_outtrack;
+-------------+-------------+------+-----+---------+-------+
>| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
>| pub_code | varchar(3) | YES | | NULL | |
>| status_flag | varchar(2) | YES | | NULL | |
>| user_id | varchar(15) | YES | | NULL | |
>| entry_date | date | YES | | NULL | |
>| pub_date | date | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
Hm, are you using SQL Server at all? While the above would be a legal
table definition in SQL 2008, there is no DESC command in SQL Server,
and SQL Server you would not produce that output.

The query Plamen posted is 100% ANSI SQL as far as I know, so it
should work no matter the product. If not, you need to find a forum
for the engine you are using.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

  #4  
Old November 11th, 2008, 10:45 AM
Tony Mountifield
Guest
 
Posts: n/a

re: how to generate the following output in sql ?


In article <Xns9B52F1AB19486Yazorman@127.0.0.1>,
Erland Sommarskog <esquel@sommarskog.sewrote:
Quote:
skywalker (mail.supratim@gmail.com) writes:
Quote:
I am really really new to sql,please forgive my
ignorence.

here is my problem

I have this table named mis_outtrack

desc mis_outtrack;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| pub_code | varchar(3) | YES | | NULL | |
| status_flag | varchar(2) | YES | | NULL | |
| user_id | varchar(15) | YES | | NULL | |
| entry_date | date | YES | | NULL | |
| pub_date | date | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
>
Hm, are you using SQL Server at all? While the above would be a legal
table definition in SQL 2008, there is no DESC command in SQL Server,
and SQL Server you would not produce that output.
Looks like MySQL to me.

Cheers
Tony
--
Tony Mountifield
Work: tony@softins.co.uk - http://www.softins.co.uk
Play: tony@mountifield.org - http://tony.mountifield.org
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find first instance from another table paragpdoke answers 5 July 15th, 2009 12:16 PM
MS SQL compare columns to generate display name Yas answers 5 September 18th, 2007 01:15 PM
Generating SQL Scripts from SPUFI alex.mcshane@btinternet.com answers 2 March 9th, 2006 04:25 PM
Generating various statistics from data in MSSQL7 Justin Lebar answers 4 July 20th, 2005 02:09 AM