これが私の状況です。未処理の各作業指示を表示するレポートを作成する必要があります。このレポートには、最後の作業日 (ある場合) と、最後の作業日からの経過日数も表示されます。データセットを作成した SQL を次に示します。
select segments.date_created,
headers.order_number
segments.segment_id
lines.line_type
lines.line_qty * lines.unit_price line_amt,
case when lines.line_type = 3
then max(clocking.clock_date)
else convert(date, '1900-01-01')
end last_clock_date,
case when lines.line_type = 3
then datediff(day, max(clocking.clock_date), getdate())
else datediff(day, convert(date, '1900-01-01'), getdate())
end DALL
from segments inner join headers on segments.header_id = headers.header_id
left join lines on header.header_id = lines.header_id
left join clocking on header.header_id = clocking.header_id and
segments.segment_id = clocking.segment_id and
lines.line_id = clocking.line_id
where headers.status = 0
and segments.branch = @branch
and headers.folder_id in ('400', '401')
and headers.order_number not like 'WP%'
group by headers.order_number, segments.segment_id,
lines.line_type, segments.date_created, lines.line_qty,
lines.unit_price
サンプル出力は次のとおりです。
date_created order_number segment_id line_type line_amt clock_date DALL
2012-05-10 HA025050 1 1 288.58 1900-01-01 41072
2012-05-10 HA025050 1 3 81.00 2012-05-10 35
2012-05-10 HA025050 2 1 22.90 1900-01-01 41072
2012-04-26 W7184315 1 3 1062.50 2012-05-08 37
2012-04-26 W7184315 1 1 69.68 1900-01-01 41072
2012-04-26 W7184315 1 1 61.96 1900-01-01 41072
2012-04-27 W7184357 1 1 682.11 1900-01-01 41072
注意すべき 2 つの点は、1900-01-01 という日付を、レイバー ラインではないすべてのライン (ライン 3 ではない) の clock_date に詰め込んでいるということです。私のレポートでは、order_number と segment_id でグループ化しています。
レポート出力は次のようにする必要があります。
order_number segment_id amount date_created clock_date DALL
HA025050 1 369.58 2012-05-10 2012-05-10 35
HA025050 2 22.90 2012-05-10 0
W7184315 1 1194.14 2012-04-26 2012-05-08 37
W7184357 1 682.11 2012-04-27 0
Count: 4 Total: 2268.73 Days: 72 Avg: 18
レポート出力は次のとおりです。
order_number segment_id amount date_created clock_date DALL
HA025050 1 369.58 2012-05-10 2012-05-10 35
HA025050 2 22.90 2012-05-10 0
W7184315 1 1194.14 2012-04-26 2012-05-08 37
W7184357 1 682.11 2012-04-27 0
Count: 4 Total: 2268.73 Days: 205432 Avg: 51358
データセットの各行は注文行であるため、注文の合計金額は正しく合計されますが、レポートは合計 DALL 値のデータセットのすべての行を合計していますが、これは正しくありません。レポートに表示される DALL 値のみを合計したいと考えています。DALL フィールドの式では、clock_date = '1900-01-01' の場合に「0」を挿入しています。サービス マネージャーは、労働者の有無にかかわらずすべての作業指示書を必要としており、これらの指示書を 0 DALL として表示する必要があるため、すべての行が必要です。それが彼の結果をどのようにゆがめるかについて、私はすでに彼と話し合っています。どうやら彼はゆがんだ結果が好きなようです。十分な情報を提供したと思いますが、他に何か知っておく必要がある場合はお知らせください。