SQL SERVER - 选择列值的总和乘以其他列的计数

时间:2017-08-30 17:03:11

标签: sql sql-server

我正在尝试选择列值的总和乘以另一列的计数

library("shiny")
ui <- fluidPage(theme = shinytheme("united"),        
actionButton("go", "RUN"),

selectInput("PKmodel",
        "Select the PK Model:",
        choices = list(" 1 Compartment Model" = 1,
                       " 2 Compartment Model" = 2),

        selected = 1),
conditionalPanel(
condition = "input.PKmodel == 1",
numericInput("CL1", "CL:", 5, min = 0.000001, max = 10000,step = .01)
),
conditionalPanel(
condition = "input.PKmodel == 2",
numericInput("CL2", "CL:", 17, min = 0.000001, max = 10000,step = .01),
numericInput("VC2", "V:", 4.12, min = 0.0000001, max = 20,step = .01))
 )


server <- function(input, output) {

sim.data <- eventReactive(input$go,{
selectPKmod <- input$PKmodel 
if (selectPKmod == 1) {
  CLIN<-input$CL1    }
if (selectPKmod == 2) {
  CLIN<-input$CL2 
  VCIN<- input$VC2 }
} )
}
shinyApp(ui = ui, server = server)

我收到了这个错误:
select sum(CS.revenue * count(C.subid)) as [total_revenue] from campaigns_history CH LEFT OUTER JOIN campaign_sends CS ON CS.campaign_historyID = CH.id LEFT OUTER JOIN c C ON C.subid = cast(CS.id as nchar(255)) WHERE CH.id=19

我该怎么做?

谢谢你,快乐的节目! :)

4 个答案:

答案 0 :(得分:1)

您需要将SUMCOUNT分开 - 如下:

select
    SUM(CS.revenue) * COUNT(C.subid) AS [total_revenue]
FROM campaigns_history CH
LEFT OUTER JOIN campaign_sends CS ON CS.campaign_historyID = CH.id
LEFT OUTER JOIN c C ON C.subid = CAST(CS.id as NCHAR(255))
WHERE CH.id=19

答案 1 :(得分:0)

你的标题已经给你答案:)计算总和并乘以计数。

SELECT Sum(CS.revenue) * Count(C.subid) as [total_revenue]
FROM campaigns_history CH
  LEFT OUTER JOIN campaign_sends CS ON CS.campaign_historyID = CH.id
  LEFT OUTER JOIN c C ON C.subid = cast(CS.id as nchar(255))
WHERE CH.id = 19

答案 2 :(得分:0)

试试这个:

select
    sum(
        CS.revenue * 
        (select 
             count(c.subid) 
         from c 
         where c.subid = cast(CS.id as nchar(255))
        ) as [total_revenue]
from campaigns_history CH
LEFT OUTER JOIN campaign_sends CS ON CS.campaign_historyID = CH.id
WHERE CH.id=19

答案 3 :(得分:0)

对不起,伙计们,你的答案不适合我的需要 这主要是我的错,因为我没有让自己明白......

我真正需要的是CS.revenue值乘以C.subid计数的总和。这有意义吗?对不起我生锈的英语。

在单词中,首先将CS.revenue乘以C.subid的计数,然后才对该结果求和。

我提出了一个问题,它返回了我的期望。

SELECT
sum(css.total_revenue)
from (SELECT top 1 * from campaign_sends) as CS
INNER JOIN (
    select
    CS.revenue * count(C.subid) as [total_revenue]
    from campaigns_history CH
    LEFT OUTER JOIN campaign_sends CS ON CS.campaign_historyID = CH.id
    LEFT OUTER JOIN c C ON C.subid = cast(CS.id as nchar(255))
    WHERE
    CH.id=19
    GROUp BY CS.revenue
) as css ON css.total_revenue is not null