Pergunta

I'm trying to make a report in SSRS where I show some totals from the same table. I know I can use selects into select, but I've heard that could affect the performance and make it slow. That is why I decided to use store procedures but I'm not so familiar with it (I only did some basic SP) so some help will be apreciated:

This is what I need to get:

|--------------|------------------------- TOTALS AND PERCENTAGES ----------------------|   
|COMPANY       | PACKAGES | WEIGHT | PACKAGE_DELIVERED |% DELIVERED | ONTIME |% ONTIME |

These are the querys I did in a previous version of the report (using asp):

SELECT COMPANY_NAME, COUNT(ID) AS PACKAGES, SUM(WEIGHT) AS WEIGHT
FROM PACKAGE
WHERE ACTUAL_DELIVERY_DATE BETWEEN 'X' AND 'Y'
GROUP BY COMPANY_CODE, COMPANY_NAME

Then I put the results in arrays and then make a new select to get the rest of information adding the COMPANY as filter:

SELECT COMPANY_CODE, ESTIMATED_DELIVERY_DATE, ACTUAL_DELIVERY_DATE
FROM PACKAGE
WHERE ACTUAL_DELIVERY_DATE BETWEEN 'X' AND 'Y'
AND STATUS = 'DELIVERED'
AND COMPANY_CODE = 'DHL'
ORDER BY STATUS

For every row
  PACKAGES_DELIVERED = + 1
  IF ACTUAL_DELIVERY_DATE < ESTIMATED_DELIVERY_DATE THEN ONTIME = + 1  
Next

Then I calculate the percentages and show all together in a table.

Somebody that can help me to put all this in a Store Procedure or maybe have another idea.

Thanks in advance.

Foi útil?

Solução

I would add the following columns to the original SELECT, using SUM on a CASE statement:

, SUM ( CASE WHEN STATUS = 'DELIVERED' THEN 1 ELSE 0 END ) AS PACKAGES_DELIVERED , SUM ( CASE WHEN STATUS = 'DELIVERED' AND ACTUAL_DELIVERY_DATE < ESTIMATED_DELIVERY_DATE THEN 1 ELSE 0 END ) AS ONTIME

This doesnt seem complex enough to bother with a Stored Procedure.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top