开发者

tsql ssrs percentage of late orders

I'm very new to tsql and ssrs, I've only had 3 days training so far so be gentle! I'm trying to write a report that shows the number of times for each month where an order has been late and show the percentage we've been on time. So far I have got this:

SELECT
    COUNT(*) AS NoOfWo, 
    CASE WHEN DATEDIFF(d, CompletionDate, LastActualCompDate) <= 0 THEN 'OnTime' ELSE 'Late' END AS Performance, 
    DATENAME(mm, LastActualCompDate) AS month
FROM         WipHeader
WHERE     (LastActualCompDate >= CAST(DATEPART(year, GETDATE()) AS varchar) + '-01-01')
GROUP BY
    CASE WHEN DATEDIFF(d, CompletionDate, LastActualCompDate) <= 0 THEN 'OnTime' ELSE 'Late' END, MONTH(LastActualCompDa开发者_StackOverflow社区te), DATENAME(mm, LastActualCompDate)
ORDER BY
    MONTH(LastActualCompDate)

This returns the number late and number on time and total number of orders for each month this year so far but I cant work out how to return the percentage? Any help would be greatly appreciated! Thanks.


The easiest way is to add this COUNT with OVER to give total of Late+OnTime, then do the percentage in SSRS

SELECT
    COUNT(*) OVER () AS Total,
    COUNT(*) AS NoOfWo, 
    CASE WHEN DATEDIFF ... blah

Or, if you want it in SQL (may require some nesting, not tested)

SELECT
    COUNT(*) OVER () AS Total,
    COUNT(*) AS NoOfWo, 
    CAST(100 AS float) * COUNT(*) / COUNT(*) OVER () AS percentage
    CASE WHEN DATEDIFF ... blah


Thanks gbn but neither worked. Maybe it was me being a newbie and didn't know quite how to paste it in. The SQL option did but the data was bad, it returned percentages over 100% which is not possible. I fixed it like this. The query now looks like:

SELECT     COUNT(*) AS NoOfWo, DATENAME(mm, LastActualCompDate) AS month, LastActualCompDate, CompletionDate, JobNo
FROM         WipHeader
WHERE     (LastActualCompDate >= CAST(DATEPART(year, GETDATE()) AS varchar) + '-01-01') AND (NOT (LastActualCompDate IS NULL))
GROUP BY MONTH(LastActualCompDate), DATENAME(mm, LastActualCompDate), LastActualCompDate, CompletionDate, JobNo
HAVING      (NOT (LastActualCompDate IS NULL)) AND (NOT (LastActualCompDate IS NULL)) AND (NOT (MONTH(LastActualCompDate) IS NULL))
ORDER BY MONTH(LastActualCompDate)

Then in SSRS I added a new Calculated field into the dataset called CalcDateDiff with this expression:

=datediff("d",Fields!CompletionDate.Value,Fields!LastActualCompDate.Value)

Then In the table I added New columns called 'Late', 'On Time' and '% On Time' and added the expressions underneath respectively:

=count(iif (fields!CalcDateDiff.Value>0,1,nothing))
=count(iif (fields!CalcDateDiff.Value<=0,1,nothing))
=count(iif (fields!CalcDateDiff.Value<=0,1,nothing))/Sum(Fields!NoOfWo.Value)

Then set the text box properties of the last one to Percentage and added a group by month.

And hey presto. It only took me 4 days!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜