Skip to main content
Inspiring
August 31, 2011
Question

Combining data from SQL query

  • August 31, 2011
  • 1 reply
  • 7276 views

Hoping there is a SQL guru out there that can help me with this

I have two tables that I want to pull data from and display a report, based on a particular date range, for this example let's say 08/01/2011 to 08/31/2011

In one table there would be an affiliate ID - AFF_UID and also the date and a counter for that date, IMP_COUNTER , now there may only be a few records not every day might be covered, say

TABLE IMP

date,aff_uid,imp_counter

08/01/2011,999,2000

08/02/2011,999,2050

08/20/2011,999,2030

etc

I then have another date, similar set up only there are multiple records in here for a single day, again not all days covered, I would need to total up each row for each day to get a count. So it might be:

TABLE LEAD

date,aff_uid

08/01/2011,999

08/01/2011,999

08/01/2011,999

08/01/2011,999

08/12/2011,999

So we have different dates covered, sometimes the same date, I need the counter from the first table, and from the second table I need to add them up for a total, and then display any date that has a value and put both counter and lead count together

Result from above

date,imp total,lead total

08/01/2011,2000,4

08/02/2011,2050,0

08/12,2011,0,1

08/20/2011,2030,0

I am sure there must be a SQL command that can gel all of this together with some grouping? Last thing I need is a ton of SQL statements in a loop!

My alternative is to add a counter to the IMP table for each lead, so every time I update the lead table with a record, I will also update the counter, but that's unncessary storage of data I already have an an extra update statement that I might be able to do without, but maybe it would make generating reports faster, just pulling to counters with no 'addition' required.

Appreciate any feedback

Thanks

Mark

This topic has been closed for replies.

1 reply

Inspiring
August 31, 2011

select imp.date

, imp.aff_uid

, count(lead.aff_uid)

, counter

from imp join lead on imp.date = lead.date and imp.aff_uid = lead.aff_uid

where imp.date >= somedate

and imp.date < someotherdate

group by imp.date, imp.aff_uid

This is pretty basic stuff.  Since you had to ask, I've heard good things about the book, Teach Yourself SQL in 10 Minutes by Ben Forta

ACS LLCAuthor
Inspiring
August 31, 2011

Thanks Dan, I'll give it a shot later on. I've done grouping and joins before but often seem run into problems, it was the ability to join more than one item that got me there, should have known that!

Thanks

Mark