我试图找出编写此Sql查询的最佳方法。 我需要获得客户已阅读的已发送给他们的促销电子邮件总数。因此,我们有一个存储客户信息的表,一个存储其登录信息的表和一个存储发送给客户的不同电子邮件的链接的表以及一个记录点击的表
这是我到目前为止所拥有的。
Select Customer.CustomerId,
Customer.FirstName,
Customer.LastName,
Customer.email,
Customer.Phone
From Customer
Join LoginInformation
ON customer.CustomerId = LoginInformation.CustomerId
JOIN ProductLinks
ON LoginInformation.LoginId= ProductLinks.LoginId
现在我需要获得每个客户所做的总点击,这些点击是一个包含以下列的点击表
ClickID
ProductLinkID
LoginId
我需要以下输出
Customer.CustomerId,
Customer.FirstName,
Customer.LastName,
Customer.email,
Customer.Phone,
Total Clicks ( this can be a sum of all the porductlinks every clicked)
使用SQL和C#
答案 0 :(得分:1)
你需要这种结构。注意2列LEFT JOIN到Click
Select
Cu.CustomerId,
Cu.FirstName,
Cu.LastName,
Cu.email,
Cu.Phone,
Count(Ck.LoginId) AS [Total Clicks]
From
Customer Cu
Join
LoginInformation LI ON Cu.CustomerId = LI.CustomerId
JOIN
ProductLinks PL ON LI.LoginId= PL.LoginId
LEFT JOIN
Click Ck ON LI.LoginId = Ck.LoginId ON PL.ProductLinkId = Ck.ProductLinkId
GROUP BY
Cu.CustomerId,
Cu.FirstName,
Cu.LastName,
Cu.email,
Cu.Phone;
答案 1 :(得分:0)
这应该可以解决问题:
Select Customer.CustomerId,
Customer.FirstName,
Customer.LastName,
Customer.email,
Customer.Phone,
Count(ClickId) [Total Clicks]
From Customer
Join LoginInformation
ON customer.CustomerId = LoginInformation.CustomerId
JOIN ProductLinks
ON LoginInformation.LoginId= ProductLinks.LoginId
JOIN Click
ON Click.LoginId = LoginInformation.LoginId
GROUP BY Customer.CustomerId,
Customer.FirstName,
Customer.LastName,
Customer.email,
Customer.Phone