计算SQL Server 2008 r2中的行

时间:2012-01-10 14:40:33

标签: sql sql-server sql-server-2008

我正在使用SQL Server 2008 R2,我正在尝试创建一个数据集来帮助我们管理现场服务面包车库存。从商业角度来看,我们希望将所有在两年内零呼叫的货车上的所有零件视为盈余。所有新零件,即刚放在货车上的零件,均可免于过剩1年。我的想法是提取不到一年的所有部件以及两年内调用次数超过零的所有部件,然后从每辆货车上的部件组中减去该部件以获得剩余部件。

但是,当我运行此脚本时,调用(脚本的count(*)部分)会计算所有调用,而不是针对每个特定面包的调用。如果两辆货车具有相同的部件(经常发生),则该部件与每辆货车一起列出但是呼叫是相同的。这是脚本:

    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

我原本以为通过在分组列表中包含货车号码(psk_id),可以为每个货车号码单独计算呼叫,但事实并非如此。

psk是产品库存表,用于指定库存的位置,无论是仓库还是服务车。

    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是产品说明表。

    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是产品价格表。

    pmf_id      (PK  FK  char(4)        not null)  --manufacturer
    pro_id      (PK  FK  char(25)       not null)  --part number

tdc是卡车来电和需求表

    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

所有这些表都在制造商(pmf_id)和部件号(pro_id)上加入。

例如,如果部件号123456在过去2年内有28次来电话,则28被列为每辆面包车的count(*),即使面包车{{1}可能有1001,面包车3可能有7051等等。


已解决:我在问题中添加其他信息后,发现了一些我最初错过的联接。

1 个答案:

答案 0 :(得分:0)

我错过了从bra_iddpr_id的{​​{1}},psk_idpsk联接。一切都按预期工作了。