January 2, 2013 at 12:03 pm
Is it possible to run a query that uses a date range, but if the records do not have any data, to then go back six months before the start date?
January 2, 2013 at 1:20 pm
Your request could be taken multiple ways, I think. If you have a moment to spare please post a table definition (CREATE TABLE statement), some sample data (INSERT statements), your expected results per that sample data and what you have tried so far and I will be happy to help you find a query that will return the rows you need.
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
January 2, 2013 at 1:56 pm
Thanks for the reply!
Here is what I have been working with today.
DECLARE
@StartDate DATETIME = NULL
,@EndDate DATETIME = NULL
SET @StartDate = '2011-08-01'
SET @EndDate = '2011-08-31'
SELECT
pnl.monthdate
,customer.custCode
,cust.Revenue
,pnl.NetRevenue
,pnl.NetRevenue - cust.plannedRevenue AS Diff
,getYtd(pnl.familycode, pnl.monthDate) AS Ytd
FROM
Customer
LEFT OUTER JOIN pnl
ON customer.Code = pnl.code AND (pnl.monthdate >= @StartDate AND pnl.monthdate <= @EndDate)
LEFT OUTER JOIN MonCustomer AS Cust
ON customer.Code = cust.code AND pnl.monthdate = cust.MonthDate
WHERE customer.custCode IN (SELECT DISTINCT custcode FROM pnl WHERE YEAR(DATEADD(YEAR,-1,pnl)) < CAST(YEAR(@StartDate) AS NVARCHAR(20)))
This what I am getting so far.
Monthdate Customer Revenue NetRev Diff Ytd
2011-08-01 Customer1 140000.00 138000.00-2000 957000.00
NULL Customer2 0 0 0 0
For Customer2, if I change the date range to 03-01-2011 to 03-31-2011 I will get some data for that customer. I would like to see Customer2 because they had revenue in the past 6 months. There WHERE clause at the end we were trying to get it to pull all customers within a year, it does, but still no data for those particular customers.
Hope this helps. Thanks!!!
January 2, 2013 at 2:30 pm
opc.three (1/2/2013)
Your request could be taken multiple ways, I think. If you have a moment to spare please post a table definition (CREATE TABLE statement), some sample data (INSERT statements), your expected results per that sample data and what you have tried so far and I will be happy to help you find a query that will return the rows you need.
Dear HOG
you have given us your query...great and thanks,,,,but we cant see what you can see.
I am sure that this can be resolved for you....but it will be much easier for us to help you if you can do as OPC suggested above .....that is please :
provide create table statements for your three tables that you refer to
provide some sample data as insert statements to populate these tables
and based on the sample data...what results (and the reason why) you are expecting.
kind regards
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
January 3, 2013 at 4:29 am
HOG (1/2/2013)
Is it possible to run a query that uses a date range, but if the records do not have any data, to then go back six months before the start date?
Whats the requirement for this? if you talk about query readability or simplicity then date/time range hardly would make any difference. and if we talk about performance perspective then
1) it will search for say current 6 month if data is not there
2) search for last 6 month.
what i think here is , either you mention complete range (1+ 2) OR any logic which will first check in current 6 then last 6. in both cases resource consumption will be same (BUt if there is partioning setup then there will be boost in search process when data is lying on different disks )
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply