跨多个列的sql sum()

时间:2019-07-11 11:29:34

标签: sql

我有一个包含7列的表格:

Color1 (string)
Color2 (string)
Color3 (string)
Use1 (decimal)
Use2 (decimal)
Use3 (decimal)
Date


Col1  |  Col2  |  Col3 |  Use1  |  Use2  |  Use3  |  Date
---------------------------------------------------------------
Red   |  Green |  Red  |  2     |   6    |  8     |  30-01-2018
Red   |  Black |  Black|  5     |   7    |  9     |  25-02-2019
Green |  Red   |  Green|  1     |   3    |  3     |  30-05-2019

我需要将所有颜色和用途归入一个列表:

Color  |  Month  |  Usage  |
----------------------------
Red    |    01   |   10    |
Red    |    02   |    5    |
Red    |    05   |    3    |
Green  |    01   |    6    |
Green  |    05   |    4    |
Black  |    02   |    16   |

正在努力寻找一种在SQL中执行此操作的方法。 如果有人可以帮助我,我将非常感激。

3 个答案:

答案 0 :(得分:0)

一种方法是Sub myFooter() ' Paste a logo into the footer. 'CTRL+SHIFT+F Application.ScreenUpdating = False Dim img As String, shp As Shape, oWD As Word.Document, Sctn As Section On Error Resume Next img = "G:\Shared Drives\footer.jpg" Set oWD = ActiveDocument For Each Sctn In oWD.Sections With oWD.Sections(Sctn.Index).Footers(wdHeaderFooterPrimary).Shapes.AddPicture(img) ' for absolute positioning .Left = CentimetersToPoints(15.75) .Top = CentimetersToPoints(0.44) '.below = BottomMargin End With Next Sctn Set shp = Nothing Application.ScreenUpdating = True End Sub 。确切的语法可能因数据库而异,但其主意是:

union all

答案 1 :(得分:0)

在子查询的内部和外部将coluse的使用分别与summing upselect color, month, sum(usage) as usage from ( select col1 as color, extract(month from Date) as month, sum(use1) as usage from tab group by col1, extract(month from Date) union all select col2, extract(month from Date), sum(use2) from tab group by col2, extract(month from Date) union all select col3, extract(month from Date), sum(use3) from tab group by col3, extract(month from Date) ) q group by color, month; 的使用情况相对,考虑整个数据的颜色重复月份的可能性,在这里可能不会共享。 / p>

DBMS

通过我们不了解Oracle的方式。 MySQLPostgresextract()支持t0=[('Albania','Angola','Germany','UK'),('UK','France','Italy'),('Austria','Bahamas','Brazil','Chile'),('Germany','UK'),('US')] t1=[('Angola', 'UK'), ('Germany', 'UK'), ('UK', 'France'), ('UK', 'Italy'), ('France', 'Italy'), ('Austria', 'Bahamas')] t2=[('Angola:UK'), ('Germany:UK'), ('UK:France'), ('UK:Italy'), ('France:Italy'), ('Austria:Bahamas')] # We transform the lists of tuple to lists of sets for easier and faster computations # We transform the lists of tuple to lists of sets for easier and faster computations t0 = [set(x) for x in t0] t1 = [set(x) for x in t1] # We define a function that removes list of elements and adds an element # from a set def add_remove(set_, to_remove, to_add): result_temp = set_.copy() for element in to_remove: result_temp.remove(element) result_temp.add(to_add) return result_temp # We do the computation using a double list comprehension result = [[add_remove(y, x, z) if x.issubset(y) else y for y in t0] for x, z in zip(t1, t2)] 功能。

答案 2 :(得分:0)

我认为这是一个有趣的问题,可以尝试一下t-sql。不幸的是,unpivot无法将颜色和用法分为两列,因此我将颜色和用法合并为nvarchar。不幸的是,这并不理想。但也许它给其他人一些想法。

select date, col 
from (select mth, col1_use1 = colour1+convert(nvarchar, usage1)
           , col2_use2 = colour2+convert(nvarchar, usage2)
           , col3_use3 = colour3+convert(nvarchar, usage3)
        from colourusagemonth)  m
unpivot 
    (col for colour in (col1_use1, col2_use2, col3_use3) 
    ) as unpvt;