Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 peaks date category

Author  Topic 

bsy
Starting Member

2 Posts

Posted - 2009-11-29 : 23:58:26
Hi all,

I got a table called 'peakprice'.

FromDate ToDate PriceUSD Peak
2010-09-01 2010-09-15 1,220.00 High
2010-09-16 2010-12-16 977.00 Low

i want to query from this 01-Sept-2010 to 21-Sept-2010, the amount must be "USD23,185" for both peaks if its falls on that date category....

Anyone can help?

vikky
Yak Posting Veteran

54 Posts

Posted - 2009-11-30 : 01:24:39
hi,

post ur required output.

Thanks,
vikky.
Go to Top of Page

bsy
Starting Member

2 Posts

Posted - 2009-11-30 : 01:39:41
E.g:

For the rental period from 1st Sept – 21st Sept 2010

1st – 15th Sept 2010 - USD 1,220 (High) USD 1,220 x 15 Days = 18,300

16th – 21st Sept 2010– USD 977 (Low) USD 977 x 5 Days = 4,885

Total: USD 23,185
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-11-30 : 02:06:54
is this u want

DECLARE @t TABLE (FromDate DATETIME,ToDate DATETIME,priceusd Decimal(18,2),peak VARCHAR(32))
INSERT INTO @t
SELECT
'2010-09-01' AS FromDate, '2010-09-15' AS ToDate, 1220.00 AS priceusd, 'High' AS peak UNION ALL
SELECT
'2010-09-16', '2010-12-16', 977.00, 'Low'

DECLARE @startDate DATETIME, @ToDate DATETIME

SELECT @startDate = '9/1/2010',@todate = '9/21/2010'

SELECT *,DATEDIFF(D,fromdate, CASE WHEN todate <= @ToDate THEN todate ELSE @todate END ) AS NoofDays
FROM @t

SELECT SUM(DATEDIFF(D,fromdate, CASE WHEN todate <= @ToDate THEN todate ELSE @todate END )*priceusd) AS NoofDays
FROM @t
Go to Top of Page
   

- Advertisement -