Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Date Range or last 6 months of data Expand / Collapse
Author
Message
Posted Wednesday, January 02, 2013 12:03 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 03, 2014 12:58 PM
Points: 2, Visits: 147
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?


Post #1402022
Posted Wednesday, January 02, 2013 1:20 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:25 AM
Points: 7,070, Visits: 12,523
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
Post #1402060
Posted Wednesday, January 02, 2013 1:56 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 03, 2014 12:58 PM
Points: 2, Visits: 147
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!!!
Post #1402071
Posted Wednesday, January 02, 2013 2:30 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 1:11 PM
Points: 1,616, Visits: 16,480
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 !
__________________________________________________________________
Post #1402088
Posted Thursday, January 03, 2013 4:28 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, March 14, 2014 2:19 AM
Points: 2,820, Visits: 3,916
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
Post #1402290
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse