旋转具有多个值的表

时间:2018-12-07 18:01:28

标签: sql ms-access pivot

我正在尝试透视具有多个结果值的访问表,例如下图中的第一个表。任何人都有关于如何获得第二张表所示结果的想法吗?

Pivot table sample

2 个答案:

答案 0 :(得分:-1)

您不能在数据透视表中使用多个聚合,但是可以轻松解决问题

通过创建两个数据透视表并将两个表(以价格和权重分隔)连接起来是可能的。

/*PRICE*/

SELECT  MONTH
       ,ITEM
       ,STORE
       ,MANAGER
       ,[TY] AS TY_PRICE
       ,[LY] AS LY_PRICE
       ,[PY] AS PY_PRICE
INTO ##TMP_PRICE
FROM(
      select MONTH
            ,ITEM
            ,STORE
            ,MANAGER
            ,TYPE
            ,SUM(PRICE) AS PRICE
      FROM TABLE_X
      GROUP BY MONTH
            ,ITEM
            ,STORE
            ,MANAGER
            ,TYPE) AS TMP
PIVOT(
MAX(PRICE)
FOR TYPE IN([TY],[LY],[PY])
) AS PVT

/*WEIGHT*/

SELECT  MONTH
       ,ITEM
       ,STORE
       ,MANAGER
       ,[TY] AS TY_WEIGHT
       ,[LY] AS LY_WEIGHT
       ,[PY] AS PY_WEIGHT
INTO ##TMP_WEIGHT
FROM(
      select MONTH
            ,ITEM
            ,STORE
            ,MANAGER
            ,TYPE
            ,SUM(WEIGHT) AS WEIGHT
      FROM TABLE_X
      GROUP BY MONTH
            ,ITEM
            ,STORE
            ,MANAGER
            ,TYPE) AS TMP
PIVOT(
MAX(WEIGHT)
FOR TYPE IN([TY],[LY],[PY])
) AS PVT

创建两个表后,将它们加入

SELECT ISNULL(TP.month,TW.month)  as MONTH
      ,ISNULL(TP.ITEM,TW.ITEM) AS ITEM
      ,ISNULL(TP.STORE,TW.STORE) AS STORE
      ,ISNULL(TP.MANAGER,TW.MANAGER) AS MANAGER
      ,TY_WEIGHT
      ,LY_WEIGHT
      ,PY_WEIGHT
      ,TY_PRICE
      ,LY_PRICE
      ,PY_PRICE
FROM ##TMP_PRICE TP
FULL JOIN ##TMP_WEIGHT TW ON (TP.MONTH = TW.MONTH AND TP.ITEM = TW.ITEM 
                            AND TP.STORE = TW.ITEM AND TP.MANAGER = TW.MANAGER)

答案 1 :(得分:-1)

由于不能在Microsoft Access的查询中将数据透视查询用作子查询,因此必须保存两个数据透视查询以分隔Microsoft Access查询对象:

  
    

枢轴价格

  
col1 = df['Col_1']
mean = np.mean(col1, axis=0)
sd = np.std(col1, axis=0)

include = [i for i in col1 if (i > mean - 2 * sd)]
include = [i for i in col1 if (i < mean + 2 * sd)]
print(include)
  
    

枢轴重量

  
library(ggplot2)
#test data
number_sightings<-structure(list(date = c("31.07.2018", "01.08.2018", "02.08.2018", "03.08.2018", "06.08.2018", "07.08.2018", "08.08.2018", "09.08.2018", "13.08.2018", "15.08.2018", "17.08.2018", "22.08.2018", "23.08.2018", "24.08.2018", "25.08.2018"), number = c(2.7, 0.99, 2.11, 1.63, 1.16, 1, 3.57, 1, 1.84, 3.25, 2.25, 2, 1.88, 2.67, 3.04)), class = "data.frame", row.names = c(NA, -15L))

#convert from character to Date Object (notice the format string)
number_sightings$date = as.Date(number_sightings$date, format = "%d.%m.%Y")

#number_sightings$date <- factor(number_sightings$date, ordered = T)
plot <-ggplot(number_sightings, aes(x=date, y=number, group=1)) + geom_point(stat="identity") + geom_line(linetype="dashed")

plot <- plot +  theme(panel.background = element_rect(fill = "white"))
plot <- plot + theme(panel.grid.minor=element_blank())
plot <- plot + theme(panel.grid.minor.x=element_blank(),panel.grid.major.x=element_blank())

#Corrected this line (%y for 2 digit year, %Y or 4 digit year)
plot <- plot + scale_x_date(expand=c(0,0), date_labels=("%d.%m.%y"), date_breaks = "4 day")
plot <- plot+ggtitle("")+(ylab("number of harbour porpoise sightings/hour"))
print(plot)

然后将它们加入第三个查询中:

TRANSFORM Sum([Price]) AS SumPrice
SELECT [month], item, store, manager
FROM Table1
GROUP BY [month], item, store, manager
PIVOT [type];