Link to home
Start Free TrialLog in
Avatar of kenuk110
kenuk110

asked on

Exclude records based on another records value?

Hi,

I have a a table which has entries per day for different services we have in our business.

We have a product that will have, on a Monday the following services:

UPC         ServiceDay         ServiceType
12345       Monday               VMF
12345       Monday               SRR
12345       Monday               IDR
12345       Monday               DCS

Now, if a client has our IDR service selected then I need to be able to say on the IDR report, do NOT include the UPC codes that have a ServiceType of SRR next to it. The connection here is the UPC and the ServiceDay I believe. The way our data is retrieved is from a CTE and this creates 'batches' of prints based on a value that is added up, which equals 750.

I have attached the CTE that I run to get the data back in the first place but I need to say for ALL the UPC codes in THIS store bring me back the IDR record ONLY if the SRR record for the same product for the same day is not present and this also needs to be confined to batches so batch 1 will only get batch one UPC codes.

In fact we could make sure it only selects the items from the GridCodeX reference for that batch - if that makes sense.

I'm just looking at the code I already have and maybe it's better just to not retrieve the records for the IDR if the same record exists for the SRR?

Any further clarification or help I can give anybody, please just ask.

Any help will be much appreciated.


Regards,

Ken


ALTER function [dbo].[ServiceViewBatch](@date date)
returns table
as

RETURN WITH cte AS (
  SELECT [ServiceValue]
       , [ItemName]
       , [countryCode]
       , [storeCode]
       , [cityCode]
       , [itemCode]
       , [storeType]
       , [vendorCode]
       , [upcCode]
       , [rc]
       , [gridCodeX]
       , [gridCodeY]
       , [facings]
       , [brand]
       , [serviceDay]
       , [serviceType]
       , [ARABIC]
       , [TREF]
       , [serviceValueTotal]
       , ROW_NUMBER() 
            OVER (ORDER BY CountryCode, CityCode, StoreCode, gridcodeX) rnum
  FROM [T1].[dbo].[ServiceSchedule2] 
  WHERE serviceday = DATENAME(WEEKDAY, @date)
  
) 
-- final selection
SELECT a.*
     , rt.svSum as RunTotal
     , convert(int, ceiling(rt.svSum/750)) as BatchGroup
FROM cte a
CROSS APPLY(
  SELECT SUM(serviceValueTotal) svSum
  FROM cte b
  WHERE b.rnum <= a.rnum
) rt

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

what about:
ALTER function [dbo].[ServiceViewBatch](@date date)
returns table
as

RETURN WITH cte AS (
  SELECT [ServiceValue]
       , [ItemName]
       , [countryCode]
       , [storeCode]
       , [cityCode]
       , [itemCode]
       , [storeType]
       , [vendorCode]
       , [upcCode]
       , [rc]
       , [gridCodeX]
       , [gridCodeY]
       , [facings]
       , [brand]
       , [serviceDay]
       , [serviceType]
       , [ARABIC]
       , [TREF]
       , [serviceValueTotal]
       , ROW_NUMBER() 
            OVER (ORDER BY CountryCode, CityCode, StoreCode, gridcodeX) rnum
  FROM [T1].[dbo].[ServiceSchedule2]  s
  WHERE serviceday = DATENAME(WEEKDAY, @date)
    AND ( ServiceType <> 'IDR' 
        OR NOT EXIST( select null from [T1].[dbo].[ServiceSchedule2] o
                   where o.UPC = s.UPC         
                     and o.ServiceDay = s.ServiceDay
                     AND o.ServiceType = 'SRR'
                     )
        )
  
) 
-- final selection
SELECT a.*
     , rt.svSum as RunTotal
     , convert(int, ceiling(rt.svSum/750)) as BatchGroup
FROM cte a
CROSS APPLY(
  SELECT SUM(serviceValueTotal) svSum
  FROM cte b
  WHERE b.rnum <= a.rnum
) rt

Open in new window

Avatar of kenuk110
kenuk110

ASKER

Hi,

I get this back when I run it?

Msg 156, Level 15, State 1, Procedure ServiceViewBatch, Line 35
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Procedure ServiceViewBatch, Line 39
Incorrect syntax near ')'.

I think the first error is because of the OR NOT EXIST( select - bit

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect!

Just one thing....and I'll ask in another question if needs be....

Adding this code has almost ground the results to a halt, I have four reports that use the same output from this CTE, the problem is that someone could print today, tomorrow, the next day if they choose so I don't thin kI can have this in a view or table all the time as it will change.

Is there an other way to increase or make this more efficient?

Small question, I'm sure a big answer will be needed. Just thought I'd ask, I'll post again in a new question though.

Cheers

>Is there an other way to increase or make this more efficient?

I would check if the table has a (single) (clustered) index on UPC + ServiceDay + ServiceType  ...
                     
Perfect, really appreciate it.