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

发布于 2024-11-09 07:31:02 字数 1410 浏览 0 评论 0原文

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

有点像这样:

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

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 IMPRESSIONS
________ _______ _________ ________ ___________
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多孤肩上扛 2024-11-16 07:31:02

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

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

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')
软甜啾 2024-11-16 07:31:02
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]
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]
删除→记忆 2024-11-16 07:31:02

因为您可能有没有电子邮件的横幅,也可能有没有横幅的电子邮件,因此您需要使用完整外部联接。

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文