Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / July 2008

Tip: Looking for answers? Try searching our database.

Find date ranges

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AHartman - 23 Jul 2008 01:41 GMT
How can I select the dates in question based upon these tables. The pounds
tables has history for the past 3 years. The workdays table has current year
plus 09 and 2010. I need to grab from the pounds table the customer
shipments based based upon the next 6 months from the workdays table. I'm
doing some analysis based upon that same time period for the previous 2
years.

I need to get the next 6 months from workdays..

08 -2008
09 -2008
10 -2008
11 -2008
12 -2008
01-2009

then use those(months) as a driver to pull activity from the pounds table
for the previous 2 years.

CREATE TABLE pounds (
customer VARCHAR(10),
shipdate datetime,
fctlbs INT);

INSERT INTO pounds  VALUES( 'jim',08/01/2006, 100);
INSERT INTO pounds  VALUES( 'dave',10/01/2006, 100);
INSERT INTO pounds  VALUES( 'abc',01/01/2007, 100);
INSERT INTO pounds  VALUES( 'abc',11/01/2006, 100);
INSERT INTO pounds  VALUES( 'jjh',11/01/2007, 100);
INSERT INTO pounds  VALUES( 'abc',09/01/2007, 100);

CREATE TABLE workdays(
work_month CHAR(2),
work_year char(4),
work_days char(2));

INSERT INTO workdays  VALUES( '01',2008,21);
INSERT INTO workdays  VALUES( '02',2008,21);
INSERT INTO workdays  VALUES( '03',2008,21);
INSERT INTO workdays  VALUES( '04',2008,21);
INSERT INTO workdays  VALUES( '05',2008,21);
INSERT INTO workdays  VALUES( '06',2008,21);
INSERT INTO workdays  VALUES( '07',2008,21);
INSERT INTO workdays  VALUES( '08',2008,21);
INSERT INTO workdays  VALUES( '09',2008,21);
INSERT INTO workdays  VALUES( '10',2008,21);
INSERT INTO workdays  VALUES( '11',2008,21);
INSERT INTO workdays  VALUES( '12',2008,21);
INSERT INTO workdays  VALUES( '01',2009,21);
INSERT INTO workdays  VALUES( '02',2009,21);
INSERT INTO workdays  VALUES( '03',2009,21);
INSERT INTO workdays  VALUES( '04',2009,21);
INSERT INTO workdays  VALUES( '05',2009,21);
INSERT INTO workdays  VALUES( '06',2009,21);
INSERT INTO workdays  VALUES( '07',2009,21);
INSERT INTO workdays  VALUES( '08',2009,21);
INSERT INTO workdays  VALUES( '09',2009,21);
INSERT INTO workdays  VALUES( '10',2009,19);
INSERT INTO workdays  VALUES( '11',2009,21);
INSERT INTO workdays  VALUES( '12',2009,20);

Thanks.
Plamen Ratchev - 23 Jul 2008 03:53 GMT
It is not clear what result set you expect. The following query will give
you the next 6 months from the Workdays table and the matching dates in the
Pounds table for the past two year.

SELECT work_year,
         work_month,
         customer,
         shipdate,
         fctlbs
FROM (SELECT CAST(work_year AS CHAR(4)) +
                    '-' +
                    work_month AS year_month,
                    work_year,
                    work_month
         FROM Workdays) AS W
LEFT JOIN Pounds AS P
 ON W.work_month = MONTH(P.shipdate)
AND W.work_year BETWEEN YEAR(P.shipdate) + 1
                                AND YEAR(P.shipdate) + 2
WHERE year_month > CONVERT(CHAR(7), CURRENT_TIMESTAMP, 126)
   AND year_month < CONVERT(CHAR(7),
                              DATEADD(MONTH, 7, CURRENT_TIMESTAMP), 126);

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.