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 2, 2013 12:03 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 12:22 PM
Points: 2, Visits: 183
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 2, 2013 1:20 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:19 AM
Points: 7,127, Visits: 12,654
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
Post #1402060
Posted Wednesday, January 2, 2013 1:56 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 12:22 PM
Points: 2, Visits: 183
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 2, 2013 2:30 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:04 PM
Points: 1,919, Visits: 19,319
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
Post #1402088
Posted Thursday, January 3, 2013 4:28 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 12:26 AM
Points: 2,840, Visits: 3,963
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