sql querying selected columns.... non normalized table

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.
llkhorAsked:
Who is Participating?
 
DrSQLCommented:
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
 
DrSQLCommented:
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
 
kretzschmarCommented:
brrrrrrr . . . .
0
 
llkhorAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.