I am using SQL Server 2008 R2, I am trying to create a dataset to help us manage our field service van inventories. From a business perspective, we want to treat all parts on all vans that have zero calls in two years as surplus. All new parts, meaning parts that were just put on a van are exempt from being surplus for 1 year. My thought is to extract all parts that are less than a year old and all parts with more than zero calls in two years, then subtract that set from the set of parts on each van to get the surplus parts.
However, when I run this script, the calls, which is the count(*) portion of the script, count all calls, not calls for each specific van. If two vans have the same part, which happens frequently, then the part is listed with each van but the calls are the same. Here is the script:
declare @cutoff date, -- 2 years prior to run date
@year int, -- integer year of @cutoff
@month int, -- integer month of @cutoff
@month_string varchar(2), -- @month converted to varchar
@year_string varchar(4) -- @year converted to varchar
set @cutoff = DATEADD(MONTH, -24, CONVERT(date, getdate()))
set @year = YEAR(@cutoff)
set @month = MONTH(@cutoff)
set @year_string = CONVERT(varchar(4), @year)
-- append a '0' to the beginning of 1 digit months
set @month_string = case when @month < 10
then '0' + CONVERT(varchar(2), @month)
else CONVERT(varchar(2), @month)
end
select psk.bra_id branch, -- branch number
psk.psk_id van_num, -- service van number
psk.pmf_id mfg, -- part manufacturer
psk.pro_id part_num, -- part number
-- first 40 characters of description
convert(varchar(40), pdi.pdi_desc) part_desc,
-- date portion of datetime created
convert(date, psk.psk_d_cre) date_new,
max(ppd.ppd_net) net, -- net price of part
-- this was being used to calc calls but gets the same value as count(*)
--tdc.tdc_yyyymm call_date,
--sum(case when tdc.tdc_dem_ord > 0
-- then 1
-- else 0
--end) calls,
-- this is where I think the problem is
COUNT(*) calls
from psk inner join pdi on psk.pmf_id = pdi.pmf_id
and psk.pro_id = pdi.pro_id
inner join ppd on psk.pmf_id = ppd.pmf_id
and psk.pro_id = ppd.pro_id
inner join tdc on psk.pmf_id = tdc.pmf_id
and psk.pro_id = tdc.pro_id
-- range of applicable van numbers
where psk.psk_id between '1000' and '9999'
-- min greater than zero, meaning nonstock parts are not included
and psk.psk_mini > 0
-- van number length = four
and LEN(psk.psk_id) = 4
-- calls are greater than zero
and tdc.tdc_dem_ord > 0
-- new in service date is greater than 1 year ago or the date of the
-- call is in the last two years
and (psk.psk_d_cre > DATEADD(year, -1, getdate()) or
tdc.tdc_yyyymm > @year_string + @month_string)
group by psk.bra_id,
psk.psk_id,
psk.pmf_id,
psk.pro_id,
pdi.pdi_desc,
psk.psk_d_cre --,
--ppd.ppd_net
-- I only want those records that have a count greater than zero
having COUNT(*) > 0
order by psk.psk_id,
psk.pmf_id,
psk.pro_id
I would have thought that by including the van number (psk_id) in the group by list that the calls would be counted separately for each van number, but that isn’t the case.
psk is the product stock table that specifies where a part is stocked at, whether the warehouse or a service van.
pmf_id (PK FK char(4) not null) --manufacturer
pro_id (PK FK char(25) not null) --part number
bra_id (PK FK char(4) not null) --branch id
dpr_id (PK FK char(4) not null) --department id
psk_id (PK char(10) not null) --stock location
psk_stktype (PK decimal(1, 0) not null) --stock or non-stock
pdi is the product description table.
pmf_id (PK FK char(4) not null) --manufacturer
pro_id (PK FK char(25) not null) --part number
lng_id (PK FK char(3) not null) --language
ppd is the product price table.
pmf_id (PK FK char(4) not null) --manufacturer
pro_id (PK FK char(25) not null) --part number
tdc is the truck call and demand table
pmf_id (PK FK char(4) not null) --manufacturer
pro_id (PK FK char(25) not null) --part number
bra_id (PK FK char(4) not null) --branch id
dpr_id (PK FK char(4) not null) --department id
psk_id (PK char(10) not null) --stock location
tdc_yyyymm (PK char(6) not null) --year and month of call
All of these tables are joined on manufacturer (pmf_id) and part number (pro_id).
For instance if part number 123456 has 28 calls in the last 2 years then 28 is listed as the count(*) for every van, even though van 1001 may have 3, van 7051 may have 2 and so on.
Solved: I found some joins that I initially missed when I was adding additional information to the question.
I missed the
bra_id,dpr_id, andpsk_idjoins frompsktotdc. All is working as expected now.