SQL Server union all不会显示所有需要的结果

问题描述 投票:0回答:3

我需要显示此查询的所有结果(更改+ servicerequest + servizio)

select data, cast((((ore*60.0)+minuti)/60) as decimal(10,2)) as change
              from mainGrid
              where username = 'marco_polo' and
              YEAR(data) = 2020 and
              MONTH(data) = 5 and
              eventType = 'Change' 
              union all
select data, cast((((ore*60.0)+minuti)/60) as decimal(10,2)) as servicerequest
              from mainGrid
              where username = 'marco_polo' and
              YEAR(data) = 2020 and
              MONTH(data) = 5 and
              eventType = 'Service request'
              union all
select data, cast((((ore*60.0)+minuti)/60) as decimal(10,2)) as servizio
              from mainGrid
              where username = 'marco_polo' and
              YEAR(data) = 2020 and
              MONTH(data) = 5 and
              eventType = 'Servizio'
order by data

正在运行的查询中,我仅获得“更改”值:

   data       change
---------------------
2020-05-30     9.15
2020-05-12     8.13

有什么建议吗?

sql sql-server union-all
3个回答
0
投票

那是因为其他类型被过滤掉了-您可能名称错误。

在任何情况下,您都可以使用in来写得更简单:

select m.event_type, m.data,
       cast((((m.ore*60.0)+m.minuti)/60) as decimal(10,2)) as change
from mainGrid m
where username = 'marco_polo' and and
      data >= '2020-05-01' and
      data < '2020-06-01' and
      eventType in ('Change' , ''Service request', 'Servizio');

这不会解决问题。


0
投票

如果要在单独的列中输入3种类型,则可以编写:

 select data, 
  case when eventType = 'Change'  then cast((((ore*60.0)+minuti)/60) as decimal(10,2)) else 0 end as change,
  case when eventType = 'Service request' then cast((((ore*60.0)+minuti)/60) as decimal(10,2)) else 0 end as servicerequest, 
  case when eventType = 'Servizio' then cast((((ore*60.0)+minuti)/60) as decimal(10,2)) else  0 end as servizio
from mainGrid
where username = 'marco_polo' and
   YEAR(data) = 2020 and
   MONTH(data) = 5 and
   (eventType = 'Change' or eventType = 'Service request' or eventType = 
    'Servizio')
order by data

如果要在一列中输入3种类型,则可以编写:

 select data, cast((((ore*60.0)+minuti)/60) as decimal(10,2)) as calcColumn
from mainGrid
where username = 'marco_polo' and
   YEAR(data) = 2020 and
   MONTH(data) = 5 and
   (eventType = 'Change' or eventType = 'Service request' or eventType = 
    'Servizio')
order by data

0
投票

现在我明白了:

   data          change            servicerequest    servizio
2020-05-30        0.00                  0.00          9.15
2020-05-12        0.00                  0.00          8.13
2020-05-30        0.00                 10.02          0.00

但是我想按“数据”分组:在这种情况下,只有一行数据= 2020-05-30。问题是,如果我仅添加“分组数据”,则会收到一条错误消息,告诉我未使用other列。因此,我添加了其他类似的列:

select data, 
  case when eventType = 'Change' then cast((((ore*60.0)+minuti)/60) as decimal(10,2)) else 0 end as change,
  case when eventType = 'Service request' then cast((((ore*60.0)+minuti)/60) as decimal(10,2)) else 0 end as servicerequest, 
  case when eventType = 'Servizio' then cast((((ore*60.0)+minuti)/60) as decimal(10,2)) else  0 end as servizio
from mainGrid
where username = 'marco_polo' and
   YEAR(data) = 2020 and
   MONTH(data) = 5 and
   (eventType = 'Change' or eventType = 'Service request' or eventType = 'Servizio')
group by data, eventType, ore, minuti
order by data

但是我得到的结果相同,没有分组。有什么建议么?谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.