Hey,
I'm doing a straight insert from one table to another with no where clause, and am noticing that the totals of each measure in both tables are different, when I would expect them to be the same in either table.
TableA was empty before running the statement. Now, when I check the sum of a particular column, I get:
..which is a loss of around 200,000. The group by shouldn't be affecting this, so what is the reason for it?
Cheers
I'm doing a straight insert from one table to another with no where clause, and am noticing that the totals of each measure in both tables are different, when I would expect them to be the same in either table.
Code:
INSERT INTO TableA
(Sales_Org_ID,
sales_org,
pl2_classid,
pl2_class,
pl6_ppnid,
pl6_ppn,
hq_id,
hq_name,
customer_sold_to_id,
customer_sold_to,
sales_district_id,
sales_district,
fquarter,
fweek,
bill_qty,
bill_amt_$,
ts_adjd_qty,
st_rptd_qty,
st_ttl_adjd_qty,
inv_rptd_qty,
inv_demo_qty,
inv_boh_qty,
inv_coh_qty,
inv_eoh_qty,
xfer_i_qty,
xfer_o_qty,
xfer_i_ttl_adjd,
xfer_o_ttl_adjd,
intran_ttl_adjd,
mdate)
SELECT
cast(Sales_Org_ID as smallint),
sales_org,
pl2_classid,
pl2_class,
pl6_ppnid,
pl6_ppn,
hq_id,
hq_name,
customer_sold_to_id,
customer_sold_to,
sales_district_id,
sales_district,
convert(decimal(18,1),right(fquarter,4) + '.' + left(fquarter,1)),
(
case when convert(int,round(fweek,1)) >= 10
then convert(decimal(18,2),right(fweek,4) + '.' + left(fweek,2))
else convert(decimal(18,2),right(fweek,4) + '.0' + left(fweek,1))
end
),
sum(convert(int,replace(bill_qty,',',''))),
sum(convert(money,bill_amt_$)),
sum(convert(int,replace(ts_adjd_qty,',',''))),
sum(convert(int,replace(st_rptd_qty,',',''))),
sum(convert(int,replace(st_ttl_adjd_qty,',',''))),
sum(convert(int,replace(inv_rptd_qty,',',''))),
sum(convert(int,replace(inv_demo_qty,',',''))),
sum(convert(int,replace(inv_boh_qty,',',''))),
sum(convert(int,replace(inv_coh_qty,',',''))),
sum(convert(int,replace(inv_eoh_qty,',',''))),
sum(convert(int,replace(xfer_i_qty,',',''))),
sum(convert(int,replace(xfer_o_qty,',',''))),
sum(convert(int,replace(xfer_i_ttl_adjd,',',''))),
sum(convert(int,replace(xfer_o_ttl_adjd,',',''))),
sum(convert(int,replace(intran_ttl_adjd,',',''))),
convert(varchar, GETDATE(), 112)
FROM TableB
GROUP BY
sales_org_id,
sales_org,
pl2_classid,
pl2_class,
pl6_ppnid,
pl6_ppn,
hq_id,
hq_name,
customer_sold_to_id,
customer_sold_to,
sales_district_id,
sales_district,
fquarter,
fweek
ORDER BY FQuarter,FWeek
TableA was empty before running the statement. Now, when I check the sum of a particular column, I get:
Code:
select sum(convert(int,replace(ts_adjd_qty,',',''))) from TableA (3283713)
select sum(convert(int,replace(ts_adjd_qty,',',''))) from TableB (3474840)
..which is a loss of around 200,000. The group by shouldn't be affecting this, so what is the reason for it?
Cheers