在聚合选择中需要帮助

时间:2010-05-31 14:31:49

标签: sql sql-server tsql

我在从数据库中选择一些值时遇到问题。 DB处于设计阶段,因此我可以对其进行一些重新设计。

您可以看到Diagram on this image

基本上我想要选择的是

select 
c.campaignID,
ct.campaignTypeName,
c.campaignName,
c.campaignDailyBudget,
c.campaignTotalBudget,
c.campaignCPC,
c.date,
cs.campaignStatusName
***impressions,
***clicks,
***cast(campaignTotalBudget-(clicks*campaignCPC) as decimal(18,1)) as remainingFunds 
from Campaigns as c
left join CampaignTypes as ct on c.campaignTypeID=ct.campaignTypeID
left join CampaignStatuses as cs on c.campaignStatusID=cs.campaignStatusID
left join CampaignVariants as cv on c.campaignID=cv.campaignID
left join CampaignVariants2Visitors as c2v on cv.campaignVariantID=c2v.campaignVariantID
left join Visitors as v on c2v.visitorID=v.visitorID
.....
order by c.campaignID desc

问题是Visitors表有名为isClick的列,所以我不知道用isClick = false分隔什么是印象,点击isClick = true是什么所以我可以显示包含有关广告系列和访问者的所有内容的精美表单...

我不认为将访客分成两个表格,如Impressions和Click是一个好主意,因为我需要让访问者有两个表

感谢

1 个答案:

答案 0 :(得分:1)

目前尚不清楚图表中的“展示次数”。但是,如果您想知道如何计算每个广告系列的点击次数,您可以执行以下操作:

Select ...
From Campaigns As c
    ...
    Left Join (
                Select V1.VisitorId
                    , CV2V1.CompaignVariantID
                    , Sum( Case When V1.IsClick = 1 Then 1 Else 0 End ) As ClickCount
                    , Count(*) TotalVisitorCount
                From Visitors As V1
                    Join CompaignVariant2Visitors As CV2V1
                        On CV2V1.VisitorId = V1.VisitorID
                Group By V1.VisitorId, CV2V1.CompaignVariantID
                ) As VisitTotals
        On VisitTotals.VisitorId= C2V.VisitorId
            And VisitTotals.CampaignVariantID = C2V.CampaignVariantID
...
Order By c.campaignID desc