Solved

How Can I match tables on "similar dates" in SQL

Posted on 2010-11-30
2
287 Views
Last Modified: 2012-06-21
Hi All,

I have the query below. It is a filthy piece of code, but the general premis is that amongst other things, I amd trying to join 2 tables based on a date. Unfortunatly the dates do not match exactly, so I have to match on the closest date I can find, be it earlier or later, but with a maximum window of +/- 10 mins, and if it does not exists, create it.

This is throwing an error saying Multiple columns are specified in an aggregated expression containing an outer reference. If an expression being aggregated contains an outer reference, then that outer reference must be the only column referenced in the expression.


Can anybody help? It's driving me mad!
SELECT 

       ISNULL(C.[DATE], DR.[DATE]) [DATE]

      ,ISNULL(C.[CLIENT_ID], DR.[CLIENT_ID]) [CLIENT_ID]

      ,ISNULL(C.[CAMPAIGN_ID], DR.[CAMPAIGN_ID]) [CAMPAIGN_ID]

      ,ISNULL(C.[SITE_ID], DR.[SITE_ID]) [SITE_ID]

      ,ISNULL(C.[PLACEMENT_ID], DR.[PLACEMENT_ID]) [PLACEMENT_ID]

      ,ISNULL(C.[CREATIVE_ID], DR.[CREATIVE_ID]) [CREATIVE_ID]

      ,ISNULL(C.[RULE_ID], DR.[RULE_ID]) [RULE_ID]

      ,ISNULL(C.[MEDIAPLEX_COOKIE_ID], DR.[MEDIAPLEX_COOKIE_ID]) [MEDIAPLEX_ID]

      ,ISNULL(C.[PARAMETER_STRING], DR.[PARAMETER_STRING]) [PARAMETER_STRING]

      ,ISNULL(C.[PERFORMANCE_TRACKER_1], DR.[PERFORMANCE_TRACKER_1]) [PERFORMANCE_TRACKER_1]

      ,ISNULL(C.[PERFORMANCE_TRACKER_2], DR.[PERFORMANCE_TRACKER_2]) [PERFORMANCE_TRACKER_2]

      ,ISNULL(C.[PERFORMANCE_TRACKER_3], DR.[PERFORMANCE_TRACKER_3]) [PERFORMANCE_TRACKER_3]

      ,C.[Country_ID]

      ,C.[State/Province]

      ,C.[DMA]

      ,C.[UserAgentString]

      ,ISNULL(C.[Date], DR.[Event_Date]) [EventDate]

      ,DR.[TimeOfDayConversion]

FROM 

		[dbo].[Click] C

RIGHT OUTER JOIN [dbo].[DerivedROI] DR

	ON 

		ABS(DATEDIFF(second, C.Date, DR.Event_Date)) 

		= 

		( 

		SELECT MIN(ABS(DATEDIFF(second, C.[Date], DR2.[Event_Date]))) 

			FROM [dbo].[DerivedROI] AS DR2 

		)

	AND C.[MEDIAPLEX_COOKIE_ID] = DR.[MEDIAPLEX_COOKIE_ID]

	AND C.[CLIENT_ID] = DR.[CLIENT_ID]

WHERE	

	DR.[Date] > CAST(FLOOR(CAST(GETDATE() - 5 AS FLOAT)) AS DATETIME)

Open in new window

0
Comment
Question by:carter-greenan
2 Comments
 
LVL 7

Accepted Solution

by:
rmm2001 earned 500 total points
ID: 34240194
Try this
SELECT 
       ISNULL(C.[DATE], DR.[DATE]) [DATE]
      ,ISNULL(C.[CLIENT_ID], DR.[CLIENT_ID]) [CLIENT_ID]
      ,ISNULL(C.[CAMPAIGN_ID], DR.[CAMPAIGN_ID]) [CAMPAIGN_ID]
      ,ISNULL(C.[SITE_ID], DR.[SITE_ID]) [SITE_ID]
      ,ISNULL(C.[PLACEMENT_ID], DR.[PLACEMENT_ID]) [PLACEMENT_ID]
      ,ISNULL(C.[CREATIVE_ID], DR.[CREATIVE_ID]) [CREATIVE_ID]
      ,ISNULL(C.[RULE_ID], DR.[RULE_ID]) [RULE_ID]
      ,ISNULL(C.[MEDIAPLEX_COOKIE_ID], DR.[MEDIAPLEX_COOKIE_ID]) [MEDIAPLEX_ID]
      ,ISNULL(C.[PARAMETER_STRING], DR.[PARAMETER_STRING]) [PARAMETER_STRING]
      ,ISNULL(C.[PERFORMANCE_TRACKER_1], DR.[PERFORMANCE_TRACKER_1]) [PERFORMANCE_TRACKER_1]
      ,ISNULL(C.[PERFORMANCE_TRACKER_2], DR.[PERFORMANCE_TRACKER_2]) [PERFORMANCE_TRACKER_2]
      ,ISNULL(C.[PERFORMANCE_TRACKER_3], DR.[PERFORMANCE_TRACKER_3]) [PERFORMANCE_TRACKER_3]
      ,C.[Country_ID]
      ,C.[State/Province]
      ,C.[DMA]
      ,C.[UserAgentString]
      ,ISNULL(C.[Date], DR.[Event_Date]) [EventDate]
      ,DR.[TimeOfDayConversion]
FROM [dbo].[Click] C
RIGHT OUTER JOIN [dbo].[DerivedROI] DR ON ABS(DATEDIFF(minute, DR.[EventDate], c.[Date])) < 10
	AND C.[MEDIAPLEX_COOKIE_ID] = DR.[MEDIAPLEX_COOKIE_ID]
	AND C.[CLIENT_ID] = DR.[CLIENT_ID]
WHERE	
	DR.[Date] > CAST(FLOOR(CAST(GETDATE() - 5 AS FLOAT)) AS DATETIME)

Open in new window

0
 
LVL 2

Author Closing Comment

by:carter-greenan
ID: 34240309
You Sir, are a gent and a scholar!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
BULK LOGGED - log full 9 25
Stored Procedure 2 47
tempdb latch contention 12 47
SQL Query Conversion of IIF statement into CASE - Syntax issue 17 29
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now