SQL Server:如何从此表中获取此结果(示例内部)

时间:2014-05-15 23:22:45

标签: sql sql-server-2008

我想问你们,你们如何进行查询以显示该表的数据:

week name   total
==== ====== =====
1    jon     15.2
1    jon     10
1    susan   10
1    howard   9
1    ben     10
3    ben     30
3    susan   10
3    mary    10
5    jon     10
6    howard  12
7    tony    25.1
8    tony     7
8    howard  10
9    susan    6.2
9    howard   9
9    ben     10
11   howard  10
11   howard  10
像这样:

week name   total
==== ====== =====
1    ben     10
1    howard   9
1    jon     25.2
1    mary     0
1    susan   10
1    tony     0
3    ben     30
3    howard   0
3    jon      0
3    mary    10
3    susan   10
3    tony     0
5    ben      0
5    howard   0
5    jon     10
5    mary     0
5    susan    0
5    tony     0
6    ben      0
6    howard  12
6    jon      0
6    mary     0
6    susan    0
6    tony     0
7    ben      0
7    howard   0
7    jon      0
7    mary     0
7    susan    0
7    tony    25.1
8    ben      0
8    howard  10
8    jon      0
8    mary     0
8    susan    0
8    tony     7
9    ben     10
9    howard   9
9    jon      0
9    mary     0
9    susan    6.2
9    tony     0
11   ben      0
11   howard  20
11   jon      0
11   mary     0
11   susan    0
11   tony     0

我尝试过类似的事情:

select t1.week_id ,
       t2.name    ,
       sum(t1.total)
from xpto as t1 ,
     xpto as t2
where t1.week_id = t2.week_id
group by t1.week_id, t2.name
order by t1.week_id, t2.name

但是我无法理解“总和”部分,我无法弄清楚为什么......

非常感谢任何帮助。提前致谢,对不起我的英语。

3 个答案:

答案 0 :(得分:0)

我已经编辑了我的答案,因为我现在明白你想要做些什么。

我更喜欢在几个步骤中执行操作,而不是尝试使用单个连接或子查询进行多次数据转换。所以我会这样做:

;
with Weeks as (
  select distinct Week_id 
  from   xpto
)
,    Names as (
  select distinct Name
  from   xpto
)
,    Scores as (
  select  week_id
      ,   name
      ,   score = sum(t1.score)
  from    xpto t1
  group by 
          t1.week_id
      ,   t1.name
)
,   WeeksAndNames as (
  select  week_id
     ,    name
  from    Weeks
     cross join Names
 )
-- The final query!
select  wan.week_id
   ,    wan.name
   ,    total = COALESCE(s.total,0)
from    WeeksAndNames wan
   left join Scores s
       on  wan.week_id = s.week_id
       and wan.name = s.name
order by
       wan.week_id
   ,   wan.name

冗长,我会授予你,你可以压缩它。但是,这显示了将数据转换为所需列表所需的每个步骤。

答案 1 :(得分:0)

根据我的理解,你想要显示所有表格中的所有星期和所有名字,无论它们是否在本周输入。为此,您需要首先构建所有名称和周的列表,交叉加入它们,然后将它们连接到总计列表,如下所示:

;with names as (select distinct name from xpto),
weeks as (select distinct week from xpto),
totals as (select week, name, sum(total) as total 
           from xpto group by week, name)
select w.week, n.name, coalesce(total, 0) as total 
from names n cross join weeks w
left join totals t on t.name=n.name and w.week = t.week
order by 1,2

SQL Fiddle

答案 2 :(得分:0)

您可以尝试以下内容:

select week      = w.week ,
       name      = n.name ,
       sum_total = coalesce( sum( d.total ) , 0 )
from       ( select distinct week from my_table ) w
cross join ( select distinct name from my_table ) n
left  join my_table d on d.week = w.week
                    and  d.name = n.name
group by w.week ,
         n.name
order by 1,2

前两个派生表的cross join构建他们的笛卡尔积:整周和表中的所有名称,无论是否存在特定的周/名称组合。

然后,我们将其与实际数据行结合并汇总,使用coalesce()将聚合函数null中的任何sum()结果合并为0。