当另一列在特定范围内时,尝试平均1列

时间:2017-12-14 21:20:41

标签: sql-server

我试图在销售之前找到一年中每个月的产品在货架上的平均天数。我需要根据不同栏目中产品的价格范围来隔离这些产品。预期的输出是这样的:

Year | Month | 400-599 | 600-799| 800-999| Over 1000
2016 | 01    |   23    |  13    |   32   |  7
2016 | 02    |   13    |  19    |   21   | 16

但是下面提到的查询并没有返回所需的结果。我错了什么?

USE DATABASE;  
GO
SELECT YEAR(datetime) AS 'YEAR', MONTH(datetime) AS 'MONTH',
CASE    WHEN price BETWEEN 400 AND 599 THEN AVG(days_on_shelf) AS '400-599',
        WHEN price BETWEEN 600 AND 799 THEN AVG(days_on_shelf) AS '600-799',
        WHEN price BETWEEN 800 AND 999 THEN AVG(days_on_shelf) AS '800-999',
        ELSE AVG(days_on_shelf) END AS 'Over 1000'
FROM TABLE1
GROUP BY MONTH(datetime), YEAR(datetime), price
ORDER BY YEAR, MONTH

1 个答案:

答案 0 :(得分:2)

input$plotA_brush

library(shiny) library(ggplot2) set.seed(42) runApp(shinyApp( ui = fluidPage(plotOutput('plotA', brush = brushOpts(id = 'plotA_brush')), plotOutput('plotZ')), server = function(input, output, session) { pollData <- reactivePoll(60 * 1000, session, checkFunc = function(){ Sys.time() }, valueFunc = function(){ data.frame(x = 1:100, y = cumsum(rnorm(100)))}) output$plotA <- renderPlot({ dt <- pollData() ggplot(dt, aes(x, y)) + geom_line() }) ranges <- reactiveValues(x = NULL, y = NULL) observe({ if (is.null(input$plotA_brush)) { brush <- structure(list(xmin = 14.313925002001, xmax = 39.942241912585, ymin = 1.1077251080591, ymax = 5.5028180250535, mapping = structure(list( x = "x", y = "y"), .Names = c("x", "y")), domain = structure(list( left = -3.95, right = 104.95, bottom = -4.07771077213569, top = 9.69030145758825), .Names = c("left", "right", "bottom", "top")), range = structure(list(left = 32.3904099935947, right = 674.020527857828, bottom = 368.859578048224, top = 5.47945189149413), .Names = c("left", "right", "bottom", "top")), log = structure(list(x = NULL, y = NULL), .Names = c("x", "y")), direction = "xy", brushId = "plotA_brush", outputId = "plotA"), .Names = c("xmin", "xmax", "ymin", "ymax", "mapping", "domain", "range", "log", "direction", "brushId", "outputId")) } else { brush <- input$plotA_brush } # dput(brush) # Useful for finding the initial brush if(!is.null(brush)) { ranges$x <- c(brush$xmin, brush$xmax) ranges$y <- c(brush$ymin, brush$ymax) } else { ranges$x <- NULL ranges$y <- NULL } }) output$plotZ <- renderPlot({ dt <- pollData() ggplot(dt, aes(x, y)) + geom_line() + coord_cartesian(xlim = ranges$x, ylim = ranges$y) }) } )) 似乎不能低于400?

我建议您阅读select year(dt) as "year", month(dt) as "month", avg(case when price between 400 and 599 then days_on_shelf end) as "400-599", avg(case when price between 600 and 799 then days_on_shelf end) as "600-799", avg(case when price between 800 and 999 then days_on_shelf end) as "800-999", avg(case when price > 999 then days_on_shelf end) as "Over 1000" from table1 group by year(dt), month(dt) order by "year", "month"; 表达式的文档。当你试图按price进行分组时,你可能会找到一个关于分组的好教程。尝试按价格分组但仍然在同一行中的不同价格范围内获得结果是没有意义的。

还有另一种使用旋转的方法,但这看起来有点矫枉过正。

case

http://rextester.com/PYBR16941