英文:
Join 3 Tables with disparate counts
问题
我有三个表格:
- 健康提供者(WellnessProviders)
- 健康访问(WellnessVisits)
- 健康推荐(WellnessReferrals)
它们的样子如下(示例):
| 提供者姓名 | 提供者电子邮件 |
|--------------|-----------------|
| 史蒂夫 | steve@steve.com |
| 杰夫 | jeff@jeff.com |
和:
| 推荐编号 | 分配给电子邮件 |
|----------------|-----------------|
| 1 | steve@steve.com |
| 2 | jeff@jeff.com |
| 3 | jeff@jeff.com |
以及:
| 访问编号 | 结账提供者电子邮件 |
|-------------|-----------------------|
| 1 | steve@steve.com |
| 2 | steve@steve.com |
我们试图创建一个查询,统计每个提供者的总推荐数和总访问数。如果我分别编写查询,结果就如预期一样:
SELECT wp.ProviderName, count(wr.AssignedToEmail)
FROM dbo.WellnessProviders wp
LEFT JOIN dbo.WellnessReferrals wr ON wp.ProviderEmail=wr.AssignedToEmail
GROUP BY wp.ProviderName
返回提供者名称以及所有推荐的计数。
SELECT wp.ProviderName, count(wv.CheckInProviderEmail)
FROM dbo.WellnessProviders wp
LEFT JOIN dbo.WellnessVisits wv ON wp.ProviderEmail=wv.CheckOutProviderEmail
GROUP BY wp.ProviderName;
返回提供者名称以及所有访问的计数。我试图将这两个查询合并到一个表中,以便得到提供者的名称、推荐计数和访问计数。我尝试通过编写以下查询来实现:
SELECT wp.ProviderName, count(wr.AssignedToEmail), count(wv.CheckOutProviderEmail)
FROM dbo.WellnessProviders wp
LEFT JOIN dbo.WellnessReferrals wr
ON wp.ProviderEmail=wr.AssignedToEmail
LEFT JOIN dbo.WellnessVisits wv
ON wv.CheckOutProviderEmail=wp.ProviderEmail
GROUP BY wp.ProviderName;
虽然这会返回所有提供者,但计数都是错误的。我需要帮助将这两个查询合并到一个单独的表格中。
期望的结果如下:
| 提供者姓名 | 总推荐数 | 总结账次数 |
|--------------|-----------------|------------------|
| 杰夫 | 2 | 0 |
| 史蒂夫 | 1 | 2 |
英文:
I have three tables:
- WellnessProviders
- WellnessVisits
- WellnessReferrals
Which look like this (sample):
| ProviderName | ProviderEmail |
|--------------|-----------------|
| Steve | steve@steve.com |
| Jeff | jeff@jeff.com |
And:
| ReferralNumber | AssignedToEmail |
|----------------|-----------------|
| 1 | steve@steve.com |
| 2 | jeff@jeff.com |
| 3 | jeff@jeff.com |
And:
| VisitNumber | CheckOutProviderEmail |
|-------------|-----------------------|
| 1 | steve@steve.com |
| 2 | steve@steve.com |
We are trying to create a query that counts the total referrals by provider AND the total visits by provider. If I write the queries separately, things work as expected:
SELECT wp.ProviderName, count(wr.AssignedToEmail)
FROM dbo.WellnessProviders wp
LEFT JOIN dbo.WellnessReferrals wr ON wp.ProviderEmail=wr.AssignedToEmail
GROUP BY wp.ProviderName
Returns all providers by name and a count of all referrals.
SELECT wp.ProviderName, count(wv.CheckInProviderEmail)
FROM dbo.WellnessProviders wp
LEFT JOIN dbo.WellnessVisits wv ON wp.ProviderEmail=wv.CheckOutProviderEmail
GROUP BY wp.ProviderName;
Returns all providers by name and a count of all visits. I am trying to combine the two sets so we get the provider's name, the count of referrals, and the count of visits. I tried to do this by writing
SELECT wp.ProviderName, count(wr.AssignedToEmail), count(wv.CheckOutProviderEmail)
FROM dbo.WellnessProviders wp
LEFT JOIN dbo.WellnessReferrals wr
ON wp.ProviderEmail=wr.AssignedToEmail
LEFT JOIN dbo.WellnessVisits wv
ON wv.CheckOutProviderEmail=wp.ProviderEmail
GROUP BY wp.ProviderName;
And while that produced all of the providers, the counts were all wrong. I need help joining the first two queries into a single table.
The desired result would be this:
| ProviderName | Total Referrals | Total Check-Outs |
|--------------|-----------------|------------------|
| Jeff | 2 | 0 |
| Steve | 1 | 2 |
答案1
得分: 3
在SQL连接中,行数会增加,因此如果尝试进行计数(或任何聚合操作),在连接操作中执行这些聚合操作时,聚合结果可能会不准确,尤其是在多次连接操作中。解决这个问题的简单方法是将聚合操作作为子查询执行,然后将它们连接到单个查询中,例如:
SELECT
wp.ProviderName
, wr.count_assigned
, wv.count_visits
FROM dbo.WellnessProviders wp
LEFT JOIN (
SELECT
AssignedToEmail
, count(*) count_assigned
FROM dbo.WellnessReferrals
GROUP BY AssignedToEmail
) wr ON wp.ProviderEmail = wr.AssignedToEmail
LEFT JOIN (
SELECT
CheckOutProviderEmail
, count(*) AS count_visits
FROM dbo.WellnessVisits
GROUP BY CheckOutProviderEmail
) wv ON wv.CheckOutProviderEmail = wp.ProviderEmail
英文:
In SQL joins multiply the number of rows, so if attempting to count (or any aggregation) you always run the risk of the aggregates being incorrect when performed over joins, especially over multiple joins. The simple solution to this is to do the aggregations as subqueries and join those together into a single query e.g:
SELECT
wp.ProviderName
, wr.count_assigned
, wv.count_visits
FROM dbo.WellnessProviders wp
LEFT JOIN (
SELECT
AssignedToEmail
, count(*) count_assigned
FROM dbo.WellnessReferrals
GROUP BY AssignedToEmail
) wr ON wp.ProviderEmail = wr.AssignedToEmail
LEFT JOIN (
SELECT
CheckOutProviderEmail
, count(*) AS count_visits
FROM dbo.WellnessVisits
GROUP BY CheckOutProviderEmail
) wv ON wv.CheckOutProviderEmail = wp.ProviderEmail
答案2
得分: 0
你可以使用以下代码:
SELECT ProviderName,
COUNT(DISTINCT wr.AssignedToEmail) AS 总推荐数,
COUNT(DISTINCT wv.CheckOutProviderEmail) AS 总签出次数
FROM WellnessProviders wp
LEFT JOIN WellnessReferrals wr ON wp.ProviderEmail = wr.AssignedToEmail
LEFT JOIN WellnessVisits wv ON wp.ProviderEmail = wv.CheckOutProviderEmail
GROUP BY ProviderName;
我在这里使用了DISTINCT关键字,以确保每个推荐和访问只计算一次。
希望这能帮助你!
英文:
You can use this code as:
SELECT ProviderName,
COUNT(DISTINCT wr.AssignedToEmail) AS TotalReferrals,
COUNT(DISTINCT wv.CheckOutProviderEmail) AS TotalCheckOuts
FROM WellnessProviders wp
LEFT JOIN WellnessReferrals wr ON wp.ProviderEmail = wr.AssignedToEmail
LEFT JOIN WellnessVisits wv ON wp.ProviderEmail = wv.CheckOutProviderEmail
GROUP BY ProviderName;
I have used here distinct keyword to ensure each referral and visit is counted once.
I hope this will help you!!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论