|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, February 21, 2013 3:10 PM
Points: 2,
Visits: 115
|
|
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?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 6,737,
Visits: 11,791
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, February 21, 2013 3:10 PM
Points: 2,
Visits: 115
|
|
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!!!
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Today @ 1:06 PM
Points: 1,456,
Visits: 14,281
|
|
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 ! __________________________________________________________________
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, March 26, 2013 8:41 AM
Points: 2,562,
Visits: 3,451
|
|
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---------- While 1 = 1 (Learning SQL....) Click to get fast response of your post
|
|
|
|