这是我的情况。我需要创建一个报表,显示每个打开的工作单,还显示上次劳动日期(如果有)以及自上次劳动日期以来经过的天数。下面是创建数据集的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
需要注意的两件事是,我将日期1900-01-01添加到所有非人工行的clock_date中,即:不是第3行。在我的报告中,我按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
由于数据集的每一行都是一个订单行,因此订单总金额的总和是正确的,但是报告将数据集的所有行都求和为total DALL值,这是错误的。我只想对报告中显示的DALL值求和。在DALL字段的表达式中,如果clock_date = '1900-01-01‘,则插入一个"0“。我需要所有行,因为服务经理想要所有的工作订单,无论上面是否有人工,并且他希望这些订单表示为0 DALL。我已经和他谈过这将如何扭曲他的结果,显然他喜欢扭曲的结果。我想我已经提供了足够的信息,如果你还需要知道什么,请告诉我。
发布于 2012-06-29 04:06:43
我很久以前就想出来了,只是太忙了没时间把它放在这里。我想既然在SSRS中没有办法做到这一点,我就求助于SQL。我没有将1900-01-01
放在labor_date
字段中,而是允许它放入null。现在,当它将所有内容相加时,空值将被忽略。
https://stackoverflow.com/questions/11034202
复制相似问题