Som lijkt / triple tellen dubbel wanneer de linkerzijde twee tabellen samenvoegt

stemmen
0

Ik probeer om twee tabellen (voorraad en verkoop) op een identificatienummer, en tel de hoeveelheid van de goederen in de inventaris, de inventaris waarde, en de waarde van de verkoop / volume. Dit is gegroepeerd op onderdeelnummer.

Hier is wat ik doe:

`select inv.[num]
, sum(inv.[Value]) as [Inventory Value]
, sum(inv.[qty]) as [Inventory Qty]
, sum(sales.[Value]) as [Sales Value]
, sum(sales.[qty]) as [Sales qty]
from Inv
left join sales
on inv.[num]=sales.[num]
group by inv.[num]`

De inventariswaarde sommatie is ongeveer 50x te groot, de omzet ongeveer 3 of 4 keer te groot. Hoe kan ik meer dan optellen?

De vraag is gesteld op 22/07/2014 om 15:35
user
In andere talen...                            


1 antwoorden

stemmen
0

Bij het gebruik van group bymet joins die many-to-one of many-to-many, moet u eerst verzamelen de maatregelen en mee op de afmetingen.

select
    inv_agg.inv_num as 'Inventory Number'
    ,inv_agg.inv_value as 'Inventory Value'
    ,inv_agg.inv_qty as 'Inventory Qty'
    ,sales_agg.sales_value as 'Sales Value'
    ,sales_agg.sales_qty as 'Sales Qty'
from (select 
        inv.num as inv_num
        ,sum(inv.Value) as inv_value
        ,sum(inv.qty) as inv_qty
    from inv
    group by inv.num) inv_agg
left join (select
        sales.num sales_num
        ,sum(sales.Value) as sales_value
        ,sum(sales.qty) as sales_qty
    from sales
    group by sales.num) sales_agg
on inv_agg.inv_num = sales_agg.sales_num

Of, met CTE

with inv_agg as (select 
        inv.num as inv_num
        ,sum(inv.Value) as inv_value
        ,sum(inv.qty) as inv_qty
    from inv
    group by inv.num),
sales_agg as (select
        sales.num sales_num
        ,sum(sales.Value) as sales_value
        ,sum(sales.qty) as sales_qty
    from sales
    group by sales.num)
select
    inv_agg.inv_num as 'Inventory Number'
    ,inv_agg.inv_value as 'Inventory Value'
    ,inv_agg.inv_qty as 'Inventory Qty'
    ,sales_agg.sales_value as 'Sales Value'
    ,sales_agg.sales_qty as 'Sales Qty'
from inv_agg left join sales_agg
    on inv_agg.inv_num = sales_agg.sales_num
antwoordde op 03/04/2015 om 03:11
bron van user

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more