SQL View: Lookup date in another table to get week number

Hello,
I have two tables as below. I'd like to create the view as below. I'm baffled how to do this in SQL. SQL Server is MS SQL 2012. Please help!
Thank you!

Table1
week      startDate      endDate
1      09/07/2015      09/13/2015
2      09/14/2015      09/20/2015
3      09/21/2015      09/27/2015
4      09/28/2015      10/04/2015

Table2
column1      | column2 | dateTime
test | 2 | 09/10/2015
dfgdfg | 3 | 09/25/2015

View required:
column1 | column2 | dateTime |weekNo
test | 2 | 09/10/2015 | 1
dfgdfgf | 3 | 09/25/2015 | 3
browswAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
You mean like this?

create table Table1 (weekNo smallint, startDate date, endDate date);

insert into Table1 select 1,      '09/07/2015',      '09/13/2015'
insert into Table1 select 2,      '09/14/2015',      '09/20/2015'
insert into Table1 select 3,      '09/21/2015',     '09/27/2015'
insert into Table1 select 4,      '09/28/2015',      '10/04/2015'

create table  Table2 (column1 sysname, column2 sysname, [dateTime] date);

insert into Table2 select 'test', 2 , '09/10/2015'
insert into Table2 select 'dfgdfg', 3,'09/25/2015'

View required:
column1 | column2 | dateTime |weekNo
test | 2 | 09/10/2015 | 1
dfgdfgf | 3 | 09/25/2015 | 3


create view V_test as
select column1,column2,[dateTime],weekNo from Table1,Table2
where [dateTime] between startDate and endDate;

select * from V_test;
ZberteocCommented:
Same idea but with a tweak and with LEFT JOIN instead of deprecated join syntax using comma:
CREATE TABLE weeks (wk int, startdate date, enddate date)
INSERT INTO weeks
SELECT * FROM
	   (values	  
		  (1      ,'09/07/2015'      ,'09/13/2015'),
		  (2      ,'09/14/2015'      ,'09/20/2015'),
		  (3      ,'09/21/2015'      ,'09/27/2015'),
		  (4      ,'09/28/2015'      ,'10/04/2015')
	   ) as v(wk,startdate,endddate)
	   
CREATE TABLE data (col1 varchar(50), col2 int, dte date)
INSERT INTO data
SELECT * FROM
	   (values	  
		  ('test',	   1      ,'09/10/2015'),
		  ('dfgdfgf',	   2      ,'09/25/2015 ')
	   ) as v(col1,col2,dte)
	   
SELECT * FROM weeks
SELECT * FROM data

go
CREATE VIEW vwDataWeeks
as
SELECT
    col1,
    col2,
    dte,
    wk
FROM 
    data d
    LEFT JOIN weeks w
	   ON d.dte BETWEEN w.startdate AND w.enddate
GO

SELECT * FROM vwDataWeeks

--DROP VIEW vwDataWeeks
--DROP TABLE data
--DROP TABLE weeks

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.