If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Query In Db2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-08-08, 03:56
DB2_NEW DB2_NEW is offline
Registered User
 
Join Date: Nov 2008
Posts: 9
Query In Db2

Below qry can be executed in sql server .. how to write in DB2 ?? does it need dummy tables??? without that anyway to write ????

SELECT COUNT(*)
FROM
(
SELECT 1 AS d
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
) weekdays
=======================

Acutal qry need to be converted in db2 ... for business days
------------------------------------------------------------


select (DATEDIFF(DAY, '2008/11/01', '2009/12/30')+1) / 7 * 5

+

( (DATEDIFF(DAY, '2008/11/01', '2008/12/30')+1) % 7 )

-

(
SELECT COUNT(*)
FROM
(
SELECT 1 AS d
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
) weekdays
WHERE d <= ( (DATEDIFF(DAY, '2008/11/01', '2008/12/30')+1) % 7 ) % 7
AND DATENAME(WEEKDAY, day('2008/12/30') - d + 1)
IN
(
'Saturday',
'Sunday'
)
)
Reply With Quote
  #2 (permalink)  
Old 12-08-08, 07:00
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
SELECT needs a FROM clause, always. You can either use the VALUES statement instead or select from a 1-row table SYSIBM.SYSDUMMY1.
Reply With Quote
  #3 (permalink)  
Old 12-08-08, 08:04
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
Did I understand you right ?

You have a start date (eg. 01.11.2008) and an end date (eg. 31.12.2009) and you want to know, how many mondays+tuesdays+wednesdays+thursdays+fridays are within that date range ?

This query will do it:

WITH ABCD ( DATUM ) AS
( SELECT DATE('01.11.2008') AS DATUM FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT ABCD.DATUM + 1 DAYS FROM ABCD X
WHERE ABCD.DATUM < DATE('31.12.2009')
)
SELECT COUNT(*) FROM ABCD
WHERE DAYOFWEEK(DATUM) IN (2,3,4,5,6)


the dates may be specified in one of these formats:
dd.mm.yyyy
mm/dd/yyyy
yyyy-mm-dd

Last edited by umayer; 12-08-08 at 08:10.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On