Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7079893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:43:40+00:00 2026-05-28T06:43:40+00:00

I am using SQL Server 2008 R2, I am trying to create a dataset

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:43:40+00:00Added an answer on May 28, 2026 at 6:43 am

    I missed the bra_id, dpr_id, and psk_id joins from psk to tdc. All is working as expected now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to export records from SQL Server 2008 to mdb file using OpenDataSource.
I'm trying to create a simple trigger using TSQL (or SQL Server 2008). The
I'm using SQL Server 2008 (non-R2) and trying to use Report Builder 3.0. When
I am using SQL Server 2008 developer edition. I was trying to attach the
I am using SQL Server 2008 & 2005 (Express). I'm trying to extract part
I've just installed SQL Server 2008 Developer edition and I'm trying to connect using
I'm trying to setup the Entity Framework with SQL Server 2008. I'm using Guids
I'm using C# to pull spatial data from a sql server 2008 database. I'm
I was trying to create a backup for my SQL Server Database using SQL
I am trying to create a trigger in SQL Server 2008 which inserts a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.