×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Wrong answer after aggregating

Wrong answer after aggregating

Wrong answer after aggregating

(OP)
Hello,

I am getting a wrong answer (lesser amount) after aggregation. The sum(quantity) in prod_fact_table is 2,089,033 but if I run the insert statement below the sum(quantity) in agg01_agg01_prod_fact_table will be 1,689,785 which is less. The two must be the same. Please what can be the problem? Any idea? See the insert statement below:

INSERT INTO agg01_prod_fact_table(
fact_count,
quantity,
machine_group_key,
location_key,
year,
quarter,
month,
day_of_month,
hour
)
SELECT
COUNT(*) AS fact_count,
SUM(BASE.quantity),
BASE.machine_group_key,
BASE.location_key,
DIM.year,
DIM.quarter,
DIM.month,
DIM.day_of_month,
DIM.hour
FROM prod_fact_table AS BASE, times AS DIM
WHERE DIM.times_key = BASE.times_key
GROUP BY
BASE.machine_group_key,
BASE.location_key,
DIM.year,
DIM.quarter,
DIM.month,
DIM.day_of_month,
DIM.hour;

Thanks,

RE: Wrong answer after aggregating

The obvious cause is , that by using an inner join, you are losing records from the facttable cause some times_key have no matching record from the BASE table.

Run the SQL using an outer join, like:

Quote:


INSERT INTO agg01_prod_fact_table(
fact_count,
quantity,
machine_group_key,
location_key,
year,
quarter,
month,
day_of_month,
hour
)
SELECT
COUNT(*) AS fact_count,
SUM(BASE.quantity),
BASE.machine_group_key,
BASE.location_key,
DIM.year,
DIM.quarter,
DIM.month,
DIM.day_of_month,
DIM.hour
FROM prod_fact_table AS BASE left outer join times AS DIM
on BASE.times_key = DIM.times_key
GROUP BY
BASE.machine_group_key,
BASE.location_key,
DIM.year,
DIM.quarter,
DIM.month,
DIM.day_of_month,
DIM.hour;

Ties Blom
 
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close