sql server - aggregate query sum value used twice in results -


this query problem part of barcode scanning system. can't results need show. either aggregate has right math, , displayed wrong, or displayed in desired format wrong math.

i use sub-query aggregate "items pulled/scanned" , join order line items. problem in grouping. see examples.

this results of subquery count "pulled" items.

staff_id      sumpulls iec-c13-6ft      2 pj-5000-wuxga    1 sam-tm300        1 

desired format - grouped category, (and repeats use of sum value)

    category          parentid      item number      qty    sumpull   sorting 2-consoles        null          sam-tm300        1      1         sam-tm300 2-consoles        sam-tm300     iec-c13-6ft      1      **2**     sam-tm300iec-c13-6ft 4-cmputers        null          laptop           1      null      laptop 4-cmputers        laptop        laptop-ps-l90w   1      null      laptoplaptop-ps-l90w 4-cmputers        laptop        mouse-wired      1      null      laptopmouse-wired 4-general a/v     null          cue-ir           1      null      cue-ir 4-projection      null          pj-5000-wuxga    1      1         pj-5000-wuxga 4-projection      pj-5000-wuxga dvim-hdmif       1      null      pj-5000-wuxgadvim-hdmif 4-projection      pj-5000-wuxga evision-4500-rm  1      null      pj-5000-wuxgaevision-4500-rm 4-projection      pj-5000-wuxga iec-c13-6ft      1      **2**     pj-5000-wuxgaiec-c13-6ft 

note sumpull value of 2 ** around them. same subquery being applied twice. here sql.

select distinct tbljobslineitems.category     ,tbljobslineitems.parentid     ,tbljobslineitems.[item number]     ,quantity     ,qrypulledbyjob.sumpull     ,concat (         parentid         ,[item number]         ) sorting tbljobslineitems left join (     select tblfixedassets.staff_id         ,count(tblfixedassets.staff_id) sumpull         ,max(job_num) job_num     tblpulls     inner join tblfixedassets on tblpulls.itembarcode = tblfixedassets.fldbarcode     (             ((tblpulls.job_num) = 366973)             , ((tblpulls.pulledstatus) = 'o')             )     group tblfixedassets.staff_id     ) qrypulledbyjob on tbljobslineitems.[item number] = qrypulledbyjob.staff_id tbljobslineitems.job_num = 366973 order category     ,concat (         parentid         ,[item number]         )  

i tried , many others, can't grouping work.

with qrypulledbyjob (     select tblfixedassets.staff_id         ,count(tblfixedassets.staff_id) sumpull      tblpulls     inner join tblfixedassets on tblpulls.itembarcode = tblfixedassets.fldbarcode     (             ((tblpulls.job_num) = 366973)             , ((tblpulls.pulledstatus) = 'o')             )     group tblfixedassets.staff_id     ) select tbljobslineitems.[item number]     ,sum(tbljobslineitems.quantity) fldquantity     ,count(tbljobslineitems.[item number]) totqty     ,sum(distinct [sumpull]) fldpulled tbljobslineitems left join [qrypulledbyjob] on tbljobslineitems.[item number] = [qrypulledbyjob].staff_id (((tbljobslineitems.job_num) = 366973)) group tbljobslineitems.[item number] 


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -