开发者

How can I get data from two different tables, link it to data from a third table, and get everything on a single report?

开发者 https://www.devze.com 2023-03-07 14:13 出处:网络
I\'m having a bit of trouble here. I have two different tables, one that stores the number of impressions a particular banner has had on the website per day and per user, and another the number of e-m

I'm having a bit of trouble here. I have two different tables, one that stores the number of impressions a particular banner has had on the website per day and per user, and another the number of e-mails that have been sent any user any given date.

A bit like this:

BANNERS

DATE     USER_ID CLIENT_ID BANNER_ID IMPRESSI开发者_StackOverflow中文版ONS
________ _______ _________ ________ ___________
04/05/11 AX745   CC        CC45      4  
04/05/11 AX745   KC        KC66      1
04/07/11 XY555   DP        DP45      2

And:

EMAILS

DATE     USER_ID EMAIL_ID CLIENT_ID
________ _______ ________ _________
04/05/11 AX745   EM001    CC
04/05/11 AX745   EM005    BK
04/07/11 XY555   EM008    DP

Both tables contain the client ID, and some form of date information, besides the user id.

I also need to reference the client ID to get the actual client's name, and those are stored in yet a third table, like so:

CLIENTS

CLIENT_ID CLIENT
_________ ________________________
CC        COCA-COLA
KC        KFC
DP        DOMINOS
BK        BURGER KING

I need a query that will return, on a per date per user listing, how many impressions has the user had of each banner, as well as which client owns it, and how many e-mails was the user sent that particular day, and from which client.

Something roughly like so:

DATE     USER_ID BANNER_CLIENT_ID BANNER_ID IMPRESSIONS EMAIL_ID EMAIL_CLIENT_ID
________ _______ ________________ _________ ___________ ________ _______________
04/05/11 AX745   COCA-COLA        CC45      4           EM001    COCA-COLA
04/05/11 AX745                                          EM005    BURGER-KING
04/05/11 AX745   KFC              KC66      1
04/07/11 XY555   DOMINOS          DP45      2           EM008    DOMINOS

I have no problem getting each set of information separately, but so far I haven't been able to merge all the data into a single report.

Any help will be greatly appreciated.

Thanks!

C


The query below should give you the desired output.

SELECT A.DATE, A.USER_ID, A.BANNER_ID, A.IMPRESSIONS, B.EMAIL_ID, C.CLIENT AS EMAIL_CLIENT_ID
FROM BANNERS A
LEFT JOIN EMAILS B ON A.CLIENT_ID=B.CLIENT_ID
LEFT JOIN CLIENTS C ON A.CLIENT_ID=C.CLIENT_ID
WHERE A.DATE=TO_DATE('04/07/11', 'MM-DD-YY')


Declare @impressions table
( [DATE]        date,
  [USER_ID]     varchar(10),
  [CLIENT_ID]   varchar(10),
  [BANNER_ID]   varchar(10),
  [IMPRESSIONS] int
)

Declare @ClientEmails table
( [DATE]        date,
  [USER_ID]     varchar(10),
  [EMAIL_ID]    varchar(10),
  [CLIENT_ID]   varchar(10)
)

Declare @Clients table
(  [CLIENT_ID]   varchar(10),
   [CLIENT]      varchar(50)
)   


Insert Into  @impressions ([DATE], [USER_ID], [CLIENT_ID], [BANNER_ID], [IMPRESSIONS])
Values ('2011/04/05', 'AX745', 'CC', 'CC45', 4),
       ('2011/04/05', 'AX745', 'KC', 'KC66', 1),
       ('2011/04/07', 'XY555', 'DP', 'DP45', 2)


Insert Into  @ClientEmails ([DATE],[USER_ID],[EMAIL_ID], [CLIENT_ID])
Values ('2011/04/05', 'AX745', 'EM001', 'CC'),
       ('2011/04/05', 'AX745', 'EM005', 'BK'),
       ('2011/04/07', 'XY555', 'EM008', 'DP')

Insert Into  @Clients([CLIENT_ID], [CLIENT])
Values ('CC', 'COCA-COLA'),
       ('KC', 'KFC'),
       ('DP', 'DOMINOS'),
       ('BK', 'BURGER KING')


Select isNull(imp.[DATE], eml.[DATE]) as [DATE],
       imp.[USER_ID],
       cli.[CLIENT] as [BANNER_CLIENT_ID],
       imp.[USER_ID] as [BANNER_ID],
       imp.[IMPRESSIONS],
       eml.[EMAIL_ID],
       emc.[CLIENT]  as [EMAIL_CLIENT_ID]
  from @impressions imp
  Left Join @Clients cli 
    on imp.[CLIENT_ID]=cli.[CLIENT_ID]
  Full outer Join @ClientEmails eml 
    on imp.[CLIENT_ID]=eml.[CLIENT_ID] 
   and imp.[USER_ID]=eml.[USER_ID]
   and imp.[DATE]=eml.[DATE]
  Left Join @Clients emc 
    on eml.CLIENT_ID=emc.CLIENT_ID  
 Order By [DATE]


Because you could have either BANNERs without EMAILs and also EMAILS without BANNERS you will need to user a FULL OUTER JOIN.

SELECT 
  nvl(A.DATE, b.date) as DATE,
  nvl(A.USER_ID, b.user_id) as USER_ID,
  c.client as banner_client_id,
  A.BANNER_ID, 
  A.IMPRESSIONS,  
  B.EMAIL_ID,  
  D.CLIENT AS EMAIL_CLIENT_ID
FROM 
  BANNERS A
  FULL OUTER JOIN EMAILS B ON (A.USER_ID=B.USER_ID AND 
                         a.DATE = b.DATE and
                         A.CLIENT_ID = B.CLIENT_ID)
  LEFT OUTER JOIN CLIENTS C ON (C.CLIENT_ID= a.CLIENT_ID)
  LEFT OUTER JOIN CLIENTS D ON (D.CLIENT_ID= B.CLIENT_ID)

A full version of the query including some sample data is shown below:

with banners as (
  select date '2011-04-05' as date_col, 'AX745' as user_id, 'CC' as client_id, 'CC45' as banner_id, 4 as impressions from dual union all
  select date '2011-04-05' as date_col, 'AX745' as user_id, 'KC' as client_id, 'KC56' as banner_id, 1 as impressions from dual union all
  select date '2011-04-07' as date_col, 'XY555' as user_id, 'DP' as client_id, 'CC45' as banner_id, 2 as impressions from dual
),
emails as (
  select date '2011-04-05' as date_col, 'AX745' as user_id, 'EM001' as email_id, 'CC' as client_id from dual union all
  select date '2011-04-05' as date_col, 'AX745' as user_id, 'EM005' as email_id, 'BK' as client_id from dual union all
  select date '2011-04-07' as date_col, 'XY555' as user_id, 'EM008' as email_id, 'DP' as client_id from dual
),
clients as (
  select 'CC' as client_id, 'Coca-Cola' as client_name from dual union all
  select 'KC' as client_id, 'KFC' as client_name from dual union all
  select 'DP' as client_id, 'Dominos' as client_name from dual union all
  select 'BK' as client_id, 'Burger King' as client_name from dual
)
select 
  nvl(A.DATE_col, b.date_col) as DATE_col,
  nvl(A.USER_ID, b.user_id) as USER_ID,
  c.client_name as banner_client_id,
  A.BANNER_ID, 
  A.IMPRESSIONS,  
  B.EMAIL_ID,  
  D.CLIENT_name AS EMAIL_CLIENT_ID
from 
  banners a
  full outer join emails b on (A.USER_ID=B.USER_ID AND 
                         a.DATE_col = b.DATE_col and
                         A.CLIENT_ID = B.CLIENT_ID)
  left outer join clients c on (a.client_id = c.client_id)
  left outer join clients d on (b.client_id = d.client_id)
0

精彩评论

暂无评论...
验证码 换一张
取 消