1

非常に冗長に思えます。私がやっていることは、特定の従業員と彼らが所属する部門のタスクを取得し、datediff とフォローアップの日付が等しい場合に基づいて 2 週間にわたってタスクを割り当てることです。true である 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

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 に答える
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 に答える