dbTalk Databases Forums  

Simple JOIN, INTERSECT query

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Simple JOIN, INTERSECT query in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
David
 
Posts: n/a

Default Simple JOIN, INTERSECT query - 04-25-2006 , 03:58 PM






Hi,

We are in the process of buying a new server to run mssql. However
before this as a tempory fix to using a msaccess backend i believe
through odbc i need to address the following issue:

SELECT ai.entry_date as CallTime,
ai.agent_login as AgentsLogin,
ai.campaign as MarketingCampaign,
ai.agent_input2 as ProductsSold,
ai.first_name as Cust_FirstName,
ai.last_name as Cust_LastName,
ai.agent_input1 as Cust_PersonalNumber,
ai.street_address as Cust_AddressStreet,
ai.city as Cust_AddressCity,
ai.state as Cust_AddressState,
ai.zip as Cust_AddressZIP,
rec.file_name as AgreementRecordingFile
FROM agent_input ai, leads l, recordings rec
WHERE ai.whole_phone_number = l.whole_phone_number AND
l.call_status = 1110 AND
rec.whole_phone_number = l.whole_phone_number AND
rec.last_name = l.last_name AND
rec.agent = ai.agent_login AND
rec.campaign = l.campaign AND
last_call_date between #04/24/2006 12:00 AM# and #04/25/2006 11:59 PM#
ORDER BY ai.agent_login, ai.entry_date

I want to make the recordings entry optional so the same results come
out whether it matches a recording or not. If it does i want it to
populate the AgreementRecordingFile column above, if not just put a ''
as you would with '' as AgreementRecordFile.

Does anyone know how you can do this, in a access based database system
using SQL through i believe ODBC?

Thanks

David


Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Simple JOIN, INTERSECT query - 04-25-2006 , 04:30 PM






David (david.goodyear (AT) gmail (DOT) com) writes:
Quote:
We are in the process of buying a new server to run mssql. However
before this as a tempory fix to using a msaccess backend i believe
through odbc i need to address the following issue:

SELECT ai.entry_date as CallTime,
ai.agent_login as AgentsLogin,
ai.campaign as MarketingCampaign,
ai.agent_input2 as ProductsSold,
ai.first_name as Cust_FirstName,
ai.last_name as Cust_LastName,
ai.agent_input1 as Cust_PersonalNumber,
ai.street_address as Cust_AddressStreet,
ai.city as Cust_AddressCity,
ai.state as Cust_AddressState,
ai.zip as Cust_AddressZIP,
rec.file_name as AgreementRecordingFile
FROM agent_input ai, leads l, recordings rec
WHERE ai.whole_phone_number = l.whole_phone_number AND
l.call_status = 1110 AND
rec.whole_phone_number = l.whole_phone_number AND
rec.last_name = l.last_name AND
rec.agent = ai.agent_login AND
rec.campaign = l.campaign AND
last_call_date between #04/24/2006 12:00 AM# and #04/25/2006 11:59 PM#
ORDER BY ai.agent_login, ai.entry_date

I want to make the recordings entry optional so the same results come
out whether it matches a recording or not. If it does i want it to
populate the AgreementRecordingFile column above, if not just put a ''
as you would with '' as AgreementRecordFile.
If I understand this correcly you need an outer join:


SELECT ai.entry_date as CallTime,
ai.agent_login as AgentsLogin,
ai.campaign as MarketingCampaign,
ai.agent_input2 as ProductsSold,
ai.first_name as Cust_FirstName,
ai.last_name as Cust_LastName,
ai.agent_input1 as Cust_PersonalNumber,
ai.street_address as Cust_AddressStreet,
ai.city as Cust_AddressCity,
ai.state as Cust_AddressState,
ai.zip as Cust_AddressZIP,
coalesec(rec.file_name, '') as AgreementRecordingFile
FROM agent_input ai
JOIN leads l ON ai.whole_phone_number = l.whole_phone_number
LEFT JOIN recordings rec ON
rec.whole_phone_number = l.whole_phone_number AND
rec.last_name = l.last_name AND
rec.agent = ai.agent_login AND
rec.campaign = l.campaign
WHERE l.call_status = 1110 AND
last_call_date >= '20060424' AND
last_call_date < '20060426'
ORDER BY ai.agent_login, ai.entry_date

Two disclaimers:

1) I don't know from which table last_call_date comes from. I've assumed
that it comes from agent_input or leads. If it comes from recordings,
the above query is not likely to be correct.

2) The syntax works on SQL Server, because that is all I know. If you want
syntax that works on Access, ask comp.databases.ms-access.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.