sql server union all不显示所有需要的结果

7ivaypg9  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(312)

我需要显示这个查询的所有结果(change+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

有什么建议吗?

vojdkbi0

vojdkbi01#

这是因为其他类型都被过滤掉了——你可能把名字搞错了。
在任何情况下,您都可以使用 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');

这不能解决问题。

wixjitnu

wixjitnu2#

如果要在分隔列中包含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
k3fezbri

k3fezbri3#

现在我明白了:

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

但是我想按“数据”分组:例如,在本例中,我只想看到一行data=2020-05-30。问题是,如果我简单地添加“groupbydata”,就会得到一个错误,告诉我groupby子句中没有使用others列。所以我在GROUPBY子句中添加了其他三列(eventtype、ore、minuti),如下所示:

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

但我得到了相同的结果,没有分组。
我还需要添加一个“total”列,给出每个日期的三种类型的总数。最后我想得到这样的东西:

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

有什么建议吗?谢谢。

相关问题