我有两个表-Table1。带卷的项目。和表2。映射表。
表格1。带卷的项目。

Item   |  Small  | Medium  |  Large  |  XLarge|  Volume
-------|---------|---------|---------|--------|-----------
Shirt  |---10----|----0----|---20----|---0----|-----------
-------|---------|---------|---------|--------|-----------
Pants  |----0----|----30---|---10----|---20---|-----------
-------|---------|---------|---------|--------|-----------
Skirts |----0----|----30---|---10----|---20---|-----------
-------|---------|---------|---------|--------|-----------


表2.映射表

Item  |Size
------|-------
Shirt |small
------|-------
Shirt |medium
------|-------
Shirt |large
------|-------
Pants |large
------|-------
Skirts|medium


我需要使用映射表1中的体积。例如,
对于衬衫,我看一下映射表,发现我们有小号,中号和大号。因此,我需要总结表1中所有项目(不只是衬衫)的Small,Medium和Large列,这将是110。

输出表:

Item   |  Small  | Medium  |  Large  |  XLarge|  Volume
-------|---------|---------|---------|--------|-----------
Shirt  |---10----|----0----|---20----|---0----|----110----
-------|---------|---------|---------|--------|-----------
Pants  |----0----|----30---|---10----|---20---|-----40----
-------|---------|---------|---------|--------|-----------
Skirts |----0----|----30---|---10----|---20---|-----60----
-------|---------|---------|---------|--------|-----------

select step1.Item
from
(select t1.Item
,t1.Small
,t1.Medium
,t1.Large
from t1) step1
full outer join
t2
on t1.Item=t2.Item
group by t1.Item
,t1.Small
,t1.Medium
,t1.Large


无法弄清案件陈述。请帮忙!

最佳答案

因为您的要求是“总结表1中所有项目的(特定尺寸)”,所以获得每个尺寸的总和是有益的。一个简单的求和查询(select sum(Small) as Small, sum(Medium)...)将作为子查询包含在最终解决方案中,以便可以轻松地“查找”按尺寸总计。

为了计算每个项目的体积,我们需要旋转映射表,以便每个项目只有一行,并且我们可以将每个映射大小的大小总和相加。对上面的总计使用cross join,并使用case语句将Mapping.Size值转换为相应的总计值。

作为最后一步,从Table1中选择具有特定大小卷的源项;并以子查询的形式加入上述内容,以获取相应的总销量。

下面的查询使用SQL Server表变量来演示如何使用示例数据。您可以使用等效的mysql替代进行确认。

declare @Table1 table (
   Item varchar(10),
   Small int, Medium int, Large int, XLarge int)
insert into @Table1 values
('Shirt', 10, 0, 20, 0),
('Pants', 0, 30, 10, 20),
('Skirts', 0, 30, 10, 20)

declare @Mapping table(
   Item varchar(10),
   Size varchar(10))
insert into @Mapping values
('Shirt', 'small'),
('Shirt', 'med'),
('Shirt', 'large'),
('Pants', 'large'),
('Skirts', 'med')

select  a.*, v.Volume
from    @Table1 a
        /* Join main table back to mapping based summary for final result */
        left join (
        /* The following is a "manual pivot" technique used to
           transform rows into aggregated columns. */
        select  m.Item,
                /* Aggregate totals accoring to the mapping */
                sum(
                case m.Size
                    when 'large' then t.Large
                    when 'med' then t.Medium
                    when 'small' then t.Small
                    when 'xlarge' then t.XLarge
                    else 0
                end
                ) Volume
        from    @Mapping m
                cross join (
                     /* Get totals of each column */
                     select  sum(Small) Small,
                             sum(Medium) Medium,
                             sum(Large) Large,
                             sum(XLarge) XLarge
                     from    @table1
                ) t
        group by m.Item
        ) v on v.Item = a.Item

关于mysql - 基于映射的计算,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44641323/

10-11 22:36
查看更多