如何从两个不同的表中获取数据,将其链接到第三个表中的数据,并在一个报表上获取所有内容?

时间:2011-05-23 10:05:29

标签: sql database oracle

我在这里遇到了一些麻烦。我有两个不同的表,一个用于存储特定横幅每天和每个用户在网站上的展示次数,另一个用于存储任何用户在任何给定日期发送的电子邮件数量。

有点像这样:

BANNERS

DATE     USER_ID CLIENT_ID BANNER_ID IMPRESSIONS
________ _______ _________ ________ ___________
04/05/11 AX745   CC        CC45      4  
04/05/11 AX745   KC        KC66      1
04/07/11 XY555   DP        DP45      2

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

除了用户ID之外,这两个表都包含客户端ID和某种形式的日期信息。

我还需要引用客户端ID来获取实际客户端的名称,这些名称存储在第三个表中,如下所示:

CLIENTS

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

我需要一个查询,它会在每个用户列表的每个日期返回用户对每个横幅有多少展示次数,以及哪个客户拥有它,以及用户发送了多少封电子邮件一天,从哪个客户。

大致如此:

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

单独获取每组信息没有问题,但到目前为止,我还无法将所有数据合并到一个报告中。

非常感谢任何帮助。

谢谢!

C

3 个答案:

答案 0 :(得分:1)

下面的查询应该为您提供所需的输出。

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')

答案 1 :(得分:0)

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]

答案 2 :(得分:0)

因为您可以拥有没有EMAIL的BANNER和没有BANNERS的EMAILS,您需要使用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)

查询的完整版本包括一些示例数据如下所示:

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)