Solved

sql querying selected columns.... non normalized table

Posted on 2002-04-16
4
483 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
  • 2
4 Comments
 
LVL 22

Expert Comment

by:DrSQL
Comment Utility
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
Comment Utility
brrrrrrr . . . .
0
 
LVL 22

Accepted Solution

by:
DrSQL earned 50 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now