Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Join Help

Author  Topic 

gavakie
Posting Yak Master

221 Posts

Posted - 2010-08-18 : 11:24:27
table that has all days between a certain days in a column.

In another table i have persons,with sales, and sales date. They may have 4 sales between the dates on the first table but between all the people all the dates show.

What im looking for is to show all the dates for that person but a simple left join doesnt work because at some point all the dates are in that table.

Any suggestions?

X002548
Not Just a Number

15586 Posts

Posted - 2010-08-18 : 11:36:24
RIGHT JOIN???

EDIT: We need WAY more details

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

MSquared
Yak Posting Veteran

52 Posts

Posted - 2010-08-18 : 15:28:55
here's a stab at what I think you're looking for, but you'll need to clarify or provide more information


declare @dates table (TransDate smalldatetime)
insert into @dates
select '2010-01-01' union all
select '2010-01-02' union all
select '2010-01-03' union all
select '2010-01-04' union all
select '2010-01-05' union all
select '2010-01-06' union all
select '2010-01-07' union all
select '2010-01-08' union all
select '2010-01-09' union all
select '2010-01-10' union all
select '2010-01-11' union all
select '2010-01-12' union all
select '2010-01-13' union all
select '2010-01-14' union all
select '2010-01-15' union all
select '2010-01-16' union all
select '2010-01-17' union all
select '2010-01-18' union all
select '2010-01-19' union all
select '2010-01-20' union all
select '2010-01-21' union all
select '2010-01-22' union all
select '2010-01-23' union all
select '2010-01-24' union all
select '2010-01-25' union all
select '2010-01-26' union all
select '2010-01-27' union all
select '2010-01-28' union all
select '2010-01-29' union all
select '2010-01-30' union all
select '2010-01-31'

declare @emp table (EmpID int, EmpName varchar(20))
insert into @emp
select 1, 'Jim' union all
select 2, 'John' union all
select 3, 'Mike'

declare @sales table (EmpID int, TransDate smalldatetime, Sales decimal (8,2))
insert into @sales
select 1, '2010-01-10', 100 union all
select 1, '2010-01-14', 200 union all
select 1, '2010-01-15', 300 union all
select 1, '2010-01-22', 400 union all
select 1, '2010-01-28', 500 union all
select 2, '2010-01-01', 1000 union all
select 2, '2010-01-24', 2000 union all
select 3, '2010-01-31', 3000


-- not sure if you want all days and employees and only show the sales they had on
-- those days
select d.TransDate, s.Sales, E.EmpID
from @dates d
cross apply @emp e
left outer join @sales s
on d.TransDate = s.TransDate
AND e.empID = s.EmpID

-- or just the dates they worked which is just a straight select
select TransDate, Sales, EmpID
from @sales



For Faster results please follow the posting guidelines here

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-19 : 13:26:49
sounds like what you need is cross join between person and dates first and then use this derived table to join to actual table.But as asked before we need to see more details before we are sure on that

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -