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.
| Author |
Topic |
|
stevil
Starting Member
3 Posts |
Posted - 2006-01-30 : 19:40:01
|
I'm currently working on a query which has me pulling my hair out and was wondering if anyone could help me out a little. What I have is 3 tables (only 3 involved anyways) one table I use to store dates, another stores host names, another stores how many referrals we get from them (its affialte tracking). I have it all working ok now an the saved data is coming back correctly but now people want to see the blank days in the data aswell. Originally I thought "Easy, just a LEFT/RIGHT join an job done" but the problem is I have to show an individual host so in the WHERE clause it has a condition saying what the host should be. Because of the fact there is no host for that day it fails the condition an doesn't return the correct data. Below is an example of what I mean.SELECT *FROM dates LEFT JOIN refs ON dates.date_id = refs.date_id INNER JOIN hosts ON hosts.host_id = refs.host_idWHERE refs.host_id = '58'GROUP BY refs.date_id I'm not sure if I've explained myself to well there. If it doesn't make sense please let me know an I'll try again. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-30 : 19:50:07
|
| can you post some sample data & expected result ?----------------------------------'KH' |
 |
|
|
stevil
Starting Member
3 Posts |
Posted - 2006-01-30 : 20:18:44
|
| [code]Table datesdate_id date_date--------------------------1 2006-01-102 2006-01-113 2006-01-12Table hostshost_id host_name-------------------------1 mock.com2 fake.com3 silly.comTable refsdate_id host_id ref_cnt---------------------------------------1 1 31 3 72 1 42 2 32 3 23 1 13 3 4desired result (searching for host 2)date_date host_id ref_cnt---------------------------------------2006-01-10 2 02006-01-11 2 32006-01-10 2 0[/code]Thanks |
 |
|
|
stevil
Starting Member
3 Posts |
Posted - 2006-01-30 : 20:50:53
|
Its ok, I got it figured out. Work on it for hours an then post it an 10 minutes later I solve, always has to happen like that doesn't it? For anyone else who has this problem the solution isSELECT *FROM dates LEFT JOIN refs ON dates.date_id = refs.date_id INNER JOIN hosts ON hosts.host_id = refs.host_id AND res.host_id = 2 I'm not sure if that exact syntax will work with what I posted as its all cut down versions to make it easier to read. You can see how its done tho. Thanks for your time an help. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
|
|
|