Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 确定SQL查询中的剩余分钟数_Sql Server_Wpf_C# 4.0 - Fatal编程技术网

Sql server 确定SQL查询中的剩余分钟数

Sql server 确定SQL查询中的剩余分钟数,sql-server,wpf,c#-4.0,Sql Server,Wpf,C# 4.0,我对此有点困惑。我可以得到小时数或总分钟数,但我同时需要小时和分钟(如果工作需要60分钟以上)。开始时间和结束时间(datetime数据类型)存储在数据库元组中的字段中 我需要几个小时(如果超过60分钟)和剩余的几分钟。我的sql查询中有以下计算。我知道总的时间是几分钟,我只需要在几个小时后再做剩余的东西 如果开始时间是上午11:15,结束时间是下午12:25,我需要1小时10分钟 sqlQuery = "select distinct(job_#1) as 'J

我对此有点困惑。我可以得到小时数或总分钟数,但我同时需要小时和分钟(如果工作需要60分钟以上)。开始时间和结束时间(datetime数据类型)存储在数据库元组中的字段中

我需要几个小时(如果超过60分钟)和剩余的几分钟。我的sql查询中有以下计算。我知道总的时间是几分钟,我只需要在几个小时后再做剩余的东西

如果开始时间是上午11:15,结束时间是下午12:25,我需要1小时10分钟

                sqlQuery = "select distinct(job_#1) as 'Job Number', " +
                "client_name as 'Client Name', " +
                "Convert(varchar,min(date),101) as 'Start Date', " +
                "convert(varchar, max(date), 101) as 'End Date', " +
                "FORMAT(sum(datediff(mi, START_TIME,END_TIME)%(60*24)/60),'#','en-us' ) as 'Hours', " + // calculate the total time taken on the job - formatted numeric
                "FORMAT(sum(datediff(mi, START_TIME,END_TIME)%(60*24)%60),'#','en-us' ) as 'Minutes', " + // calculate the total time taken on the job - formatted numeric
                "FORMAT(sum((datediff(MINUTE,start_time,end_time))/60.00)*25.00, 'C', 'en-us') as 'Total Labor' " + // multiple the job time by a $$ per hour rate - formatted as currency
                "from " + ssqltable + " " +                                      // table from which data is pulled 
                "where job_#1 IS NOT NULL " +                        // find all jobs that have a job number leaving out blank job numbers
                "and date>='" + myDate + "' " +                      // filled in when selected or passes null and shows all data
                "group by job_#1, Client_Name " +                    // aggregate grouping 
                "order by job_#1;"; // column(s) to order the data
下面是我的输出示例

237452  AADC        05/18/2017  05/18/2017  1   **208** $111.67
237353  Wolverine   05/18/2017  05/18/2017      **110** $45.83
237492  Beeman      05/11/2017  05/16/2017      **74**  $30.83
如果您有总分钟数,DIV(/)和MOD/rements(%)函数将完成此任务

DECLARE @TotalMinutes int
SET @TotalMinutes = 70

SELECT @TotalMinutes / 60 AS Hours, 
       @TotalMinutes % 60 AS Minutes
这将返回以下结果:

Total Minutes    Hours    Minutes
     70            1         10
    121            2          1

您的代码应该如下所示。请注意括号中小时和分钟的顺序变化。首先将所有分钟相加,然后除以60(整数)或将其模化,得到小时和分钟

      sqlQuery = "select distinct(job_#1) as 'Job Number', " +
            "client_name as 'Client Name', " +
            "Convert(varchar,min(date),101) as 'Start Date', " +
            "convert(varchar, max(date), 101) as 'End Date', " +
            "FORMAT(sum(datediff(mi, START_TIME,END_TIME)/60),'#','en-us' ) as 'Hours', " + // calculate the total time taken on the job - formatted numeric
            "FORMAT(sum(datediff(mi, START_TIME,END_TIME))%60),'#','en-us' ) as 'Minutes', " + // calculate the total time taken on the job - formatted numeric
            "FORMAT(sum(datediff(MINUTE,start_time,end_time)/60.00)*25.00, 'C', 'en-us') as 'Total Labor' " + // multiple the job time by a $$ per hour rate - formatted as currency
            "from " + ssqltable + " " +                                      // table from which data is pulled 
            "where job_#1 IS NOT NULL " +                        // find all jobs that have a job number leaving out blank job numbers
            "and date>='" + myDate + "' " +                      // filled in when selected or passes null and shows all data
            "group by job_#1, Client_Name " +                    // aggregate grouping 
            "order by job_#1;"; // column(s) to order the data
您可以将
datediff()
hour
配合使用数小时(如果您没有求和),将
minute
配合使用数分钟,并根据需要进行除法或模运算:

select 
    [Job Number] = [job_#1]
  , [Client Name]= client_name
  , [Start Date] = convert(char(10),min(date),101)
  , [End Date]   = convert(char(10),max(date),101)
  , Hours        = sum(datediff(minute,Start_Time,End_Time))/60
  , Minutes      = sum(datediff(minute,Start_Time,End_Time))%60
  , [Total Labor]= (sum(datediff(minute,Start_Time,End_Time))/60.00) * 25.00
from tbl
where job_#1 is not null
  and [date] >= @date_parameter
group by [job_#1], client_name
order by [job_#1]
注:

  • 使用参数,不要将变量连接到直接执行的sql;这就是打开代码进行sql注入的方式
  • 不要使用字符串文字作为别名(它已经被弃用了一段时间)
  • format()
    性能非常差:
  • 为什么要用SQL格式化日期,而不是将日期带回应用程序并在表示层格式化

    • 我认为简单除以60就足够了,如下所示:

              sum( datediff(mi,START_TIME,END_TIME)/60 ) as [Hours]
              sum( datediff(mi,START_TIME,END_TIME)%60 ) as [Minutes]
      

      格式不是高性能的。对于您的场景,您可以避免

      谢谢,@SqlZim,这是我在应用程序中的建议。我已经添加了你作为书签的链接,以备将来阅读。我按照建议删除了format(),并清理了我的XAML以进行格式化。@lseting真棒!快乐编码!哦对小鲍比桌子…:-)