Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql querying selected columns.... non normalized table

Posted on 2002-04-16
4
Medium Priority
?
530 Views
Last Modified: 2008-02-26
Hi,
My table structure was designed as follows :

Field Name--->  Empno, Day1, Day2, Day3 ... Day30
Type      --->  (Int), Int,  Int,  Int, ... Int

assumption : Day1 = 1st day of the month
             Day2 = 2nd day of the month ...etc.

The query :
based on a given Date .... select only the required columns (Day? ... Day?)

for example :
   Input Date = 10th Jan.  to   12th Jan.

   the result set should be :
   Empno, Day10, Day11, Day12.

can this be done using SQL ?
any suggestions?


Note : If I normalized the table, it would solve all the problems .. but, I can't change the existing table structure, unfortunately.

Thanks.
0
Comment
Question by:llkhor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Expert Comment

by:DrSQL
ID: 6946721
llkhor,

   It depends on your RDBMS.  Most have a CASE function and some means of converting dates so that you can get the day of the month.  I'll use Oracle's to_char function to get the day of the month and select a case statement.

For a single date:

select empno,
case
  when to_char(InputDate, 'DD') = '01' then Day1
  when to_char(InputDate, 'DD') = '02' then Day2
  when to_char(InputDate, 'DD') = '03' then Day3
  when to_char(InputDate, 'DD') = '04' then Day4
  when to_char(InputDate, 'DD') = '05' then Day5
  when to_char(InputDate, 'DD') = '06' then Day6
  when to_char(InputDate, 'DD') = '07' then Day7
  when to_char(InputDate, 'DD') = '08' then Day8
  when to_char(InputDate, 'DD') = '09' then Day9
...
  when to_char(InputDate, 'DD') = '30' then Day30
  else Day31 end as DayInfo
from myTable;


If you want a date range, it would be similar, just a more complex condition.  And you'd need two inputs - "10th Jan.  to   12th Jan" isn't going to be understood too well.  Here's an example:

select empno,
case
  when '01' between to_char(InputStartDate, 'DD')
                  and to_char(InputEndDate, 'DD') then Day1

  when '01' between to_char(InputStartDate, 'DD')
                  and to_char(InputEndDate, 'DD') then Day2
  when '01' between to_char(InputStartDate, 'DD')
                  and to_char(InputEndDate, 'DD') then Day3
  when '01' between to_char(InputStartDate, 'DD')
                  and to_char(InputEndDate, 'DD') then Day4
...
  when '01' between to_char(InputStartDate, 'DD')
                 and to_char(InputEndDate, 'DD') then Day30
  else Day31 end as DayInfo
from myTable;


Good luck!

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6947001
brrrrrrr . . . .
0
 
LVL 22

Accepted Solution

by:
DrSQL earned 150 total points
ID: 6947755
llkhor,
   The solution I gave only works for a single column.  My second statement would only return the FIRST day that met the condition.  In order to return multiple columns you can do one of two things:

1) Decide on the maximum number of returns that you will allow and set up a case for each one.  You'll always return that many columns, even if you make the case return a null when there's no column (less than the maximum).

2) Return multiple rows - one for each date in the range - and "flatten" them into a single row via a join.  This also requires knowing how many you want to return.

It might be best to handle this in logic - either dynamically generate the select and execute it, or return multiple records and grab the values.  In order to return multiple records you need a "LOOPER" table.  This table has a column called SEQ and it has (for this example) the numbers 1 - 31 inserted as records.  Then, you would:

select empno,
case
 when looper.seq = 1 then Day1
 when looper.seq = 2 then then Day2
 when looper.seq = 3 then then Day3
 when looper.seq = 4 then then Day4
...
 when looper.seq = 3' then Day30
 else Day31 end as DayInfo
from myTable, looper
where looper.seq between
              to_number(to_char(InputStartDate, 'DD'))
          and to_number(to_char(InputEndDate, 'DD'));

In your example, this would give you three records.  How you would flatten them in SQL would depend on your RDBMS.  In Oracle, I'd use the rank() function:

select one.empno, one.dayinfo, two.dayinfo, three.dayinfo
from
(select empno,
case
 when looper.seq = 1 then Day1
 when looper.seq = 2 then then Day2
 when looper.seq = 3 then then Day3
 when looper.seq = 4 then then Day4
...
 when looper.seq = 3' then Day30
 else Day31 end as DayInfo,
 rank() over (order by looper.seq) as ranking
from myTable, looper
where looper.seq between
              to_number(to_char(InputStartDate, 'DD'))
          and to_number(to_char(InputEndDate, 'DD'))) one,
(<exactly the same SQL>) two,
(<same again>) three
where one.empno=two.empno and
      one.empno=three.empno and
      one.ranking = 1 and two.ranking=2 and three.ranking=3;

As you can see - messy and requires foreknowledge of how many returns.

I guess it means that the short answer to your question is no.  But, you can come close.

Sorry to add to any confusion (and draw any undeserved cool points).

Good luck!

0
 

Author Comment

by:llkhor
ID: 6949545
DrSQL,

first of all, thanks.

I'm using desktop database (Paradox - Delphi environment), which doesn't support Case or Rank.

I'd thought of 'normalizing' the table by generating a temp table (as you pointed out in option 2) and flatten it out with 'join'.  

Then again, I don't have to use SQL statement to do it .....direct table to table update handled by program logic will do.

I just wonder if it could be done with a single SQL statement.....

"I guess it means that the short answer to your question is no" .... I agreed.

Thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question