简体   繁体   English

查询以显示每月的总休假时间

[英]Query to show total leave hours per month

I have a Leave table as per below: 我有一个Leave表如下:

MONTH | NAME | DAY1 | DAY2 | DAY3 | ... | DAY31
------------------------------------------------      
      |      |      |      |      | ... |     

Columns DAY1 to DAY31 keep the number of hours the named person has not been in the office. DAY1DAY31保持的时间被点名者一直没有在办公室的电话号码。

I need a query to generate this output: 我需要一个查询来生成此输出:

MONTH | NAME | TOTAL_MONTH_HOURS
---------------------------------      
      |      |        

where TOTAL_MONTH_HOURS is sum of all hours in the month; 其中TOTAL_MONTH_HOURS是一个月中所有小时的总和; ie DAY1 + DAY2 + ... + DAY30+DAY31 DAY1 + DAY2 + ... + DAY30+DAY31

How should I write this query? 我应该如何编写此查询? Is there any way to escape having to sum all the columns as I did above? 有什么办法可以避免像我上面那样对所有列求和? I encounter similar problems from time to time where tables get even bigger! 我有时会遇到类似的问题,表格会变得更大!

Note: some columns may be NULL due to variant month lengths. 注意:由于月份的长度不同,某些列可能为NULL

The simplest, given the current table structure, is simply to do exactly that - 给定当前的表结构,最简单的方法就是完全做到这一点-

Select Month, Name, Day1+Day2+Day3....+Day31 Total_Month_Hours
  From Tbl

Now, if it is possible to have NULLs and that being the reason for the question, wrap them with COALESCE. 现在,如果可能有NULL,并且这是问题的原因,请用COALESCE将它们包装起来。 COALESCE is part of the ANSI SQL99 standard and has support on all major RDBMS ( MySQL Oracle SQLite PostgreSQL SQL Server ). COALESCE是ANSI SQL99标准的一部分,并且在所有主要的RDBMS( MySQL Oracle SQLite PostgreSQL SQL Server )上均受支持。

Select Month,
       Name,
       COALESCE(Day1,0)+COALESCE(Day2,0)+...+COALESCE(Day31,0) Total_Month_Hours
  From Tbl

Since you can't alter your tables, here: 由于您无法更改表格,因此请在此处:

SELECT `month`, `name`, (DAY1+DAY2+DAY3+DAY4+DAY5+DAY6+DAY7+DAY8+DAY9+DAY10+DAY11+DAY12+DAY13+DAY14+DAY15+DAY16+DAY17+DAY18+DAY19+DAY20+DAY21+DAY22+DAY23+DAY24+DAY25+DAY26+DAY27+DAY28+DAY29+DAY30+DAY31) AS `total_month_hours`
FROM `table_name`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM