1

这似乎非常多余。我正在做的是为特定员工和他们所在的部门分配任务,并根据 datediff 和后续日期相等的时间在两周内分配他们的任务,分配一个 1,这是真的,总和为结束。

$_2weeks = "select
        isnull(b.employee, 'Event Total') as Employee,
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))<='-2' then 1 else 0 end) '2+ Days Behind',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='-1' then 1 else 0 end) '1 Day Behind',
        sum(case when cast(a.follow_up as date)=cast(GETDATE() as date) then 1 else 0 end) 'Today<br>&nbsp;" . date('m/d') . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='1' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+1 day')), 0, 1).'<br>' . date('m/d', strtotime('+1 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='2' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+2 day')), 0, 2).'<br>' . date('m/d', strtotime('+2 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='3' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+3 day')), 0, 1).'<br>' . date('m/d', strtotime('+3 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='4' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+6 day')), 0, 1).'<br>' . date('m/d', strtotime('+6 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='5' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+7 day')), 0, 1).'<br>' . date('m/d', strtotime('+7 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='6' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+8 day')), 0, 1).'<br>' . date('m/d', strtotime('+8 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='7' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+9 day')), 0, 2).'<br>' . date('m/d', strtotime('+9 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='8' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+10 day')), 0, 1).'<br>' . date('m/d', strtotime('+10 day')) . "',
        sum(case when DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date))='9' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+13 day')), 0, 1).'<br>' . date('m/d', strtotime('+13 day')) . "',
        sum(1) AS 'Total'
    from " . event_table('event') . " a
    left outer join " . event_table('employee') . " b on a.employee_id=b.id
    where a.task_id like '%$task_id%'
    and b.department_id like '$dept_id'
    and a.status=1
    and a.task_id<>''
    group by b.employee with rollup";

以及 substr 和 strtotime php函数的更简洁的方法?任何帮助将不胜感激。提前致谢。

4

3 回答 3

1

您可以扩展group by以包括日差。例如:

select  b.employee
,       datediff(dd,cast(getdate() as date),cast(a.follow_up as date)) as DaysInPast
,       count(a.task_id) as TaskCount
group by
        b.employee
,       datediff(dd,cast(getdate() as date),cast(a.follow_up as date))

您必须在客户端生成列名,并可能旋转结果。最后,这可能比您当前的解决方案更复杂(当然就代码行而言)。

于 2013-04-30T19:39:30.753 回答
0

不一定是您要求的(简化 PHP 部分),但我认为如果您的数据库支持这样的子表,您可以减少 SQL 代码的“视觉混乱”:

select
        isnull(b.employee, 'Event Total') as Employee,
        sum(case when sub.dat_diff<='-2' then 1 else 0 end) '2+ Days Behind',
        sum(case when sub.dat_diff='-1' then 1 else 0 end) '1 Day Behind',
        sum(case when cast(a.follow_up as date)=cast(GETDATE() as date) then 1 else 0 end) 'Today<br>&nbsp;" . date('m/d') . "',
        sum(case when sub.dat_diff='1' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+1 day')), 0, 1).'<br>' . date('m/d', strtotime('+1 day')) . "',
        sum(case when sub.dat_diff='2' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+2 day')), 0, 2).'<br>' . date('m/d', strtotime('+2 day')) . "',
        sum(case when sub.dat_diff='3' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+3 day')), 0, 1).'<br>' . date('m/d', strtotime('+3 day')) . "',
        sum(case when sub.dat_diff='4' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+6 day')), 0, 1).'<br>' . date('m/d', strtotime('+6 day')) . "',
        sum(case when sub.dat_diff='5' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+7 day')), 0, 1).'<br>' . date('m/d', strtotime('+7 day')) . "',
        sum(case when sub.dat_diff='6' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+8 day')), 0, 1).'<br>' . date('m/d', strtotime('+8 day')) . "',
        sum(case when sub.dat_diff='7' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+9 day')), 0, 2).'<br>' . date('m/d', strtotime('+9 day')) . "',
        sum(case when sub.dat_diff='8' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+10 day')), 0, 1).'<br>' . date('m/d', strtotime('+10 day')) . "',
        sum(case when sub.dat_diff='9' then 1 else 0 end) '" . substr(date('l/m/d', strtotime('+13 day')), 0, 1).'<br>' . date('m/d', strtotime('+13 day')) . "',
        sum(1) AS 'Total'
FROM (
SELECT a.employee_id, a.task_id, DATEDIFF(dd,cast(GETDATE() as date),cast(a.follow_up as date)) as dat_diff
FROM " . event_table('event') . " a
WHERE a.status=1
) sub
LEFT JOIN ... (etc.)

此外,您可能应该避免多次调用“getdate”——最有可能调用一次,将值存储在变量中并将其粘贴到代码中就足够了,而且速度稍快。

我的 2 美分 ;)

于 2013-04-30T20:37:18.053 回答
0

. . 如果你只是需要避免这种重复的“视觉问题”,你可以创建一个 php 函数来输出正确的字符串,比如...

    function sqlline($i, $n) {
        $timestr = strtotime('+'. $i .' day');
        $d = date('l/m/d', $timestr);
        $d2 = date('m/d', $timestr);

        return 'SUM '.
            '(CASE WHEN '.
                'DATEDIFF (dd, cast(GETDATE() as date), cast(a.follow_up as date)) = "'. $i .'" '.
            'THEN 1 ELSE 0 END) '.
            '"'. substr($d, 0, $n) .'<br>'. $d2 .
            ', '
    }

. . 并用sqlline(1, 1) . sqlline(2, 1) (...).

. . 但我认为你真的应该多考虑一下代码的目标以及其中一些如何是自动的。

于 2013-04-30T20:05:29.880 回答