Mysql – thesql getting column data from one table to line up correctly with column data from another table

MySQL

I need the query to produce this:

person_id last_name first_name region_idregion name year month amount_sold

1   barnum phineas 1    maricopa 2016 1 800000  
1   barnum phineas 1    maricopa 2016 2 850000  
1   barnum phineas 1    maricopa 2016 3 990000  
2   loman willy 2   pima 2016 1 425000  
2   loman willy 2   pima 2016 2 440000  
2   loman willy 2   pima 2016 3 450000  
2   loman willy 3   pinal 2016 1    200000  
2   loman willy 3   pinal 2016 2    210000  
2   loman willy 3   pinal 2016 3    220000  
2   loman willy 4   santa cruz 2016 1   50000   
2   loman willy 4   santa cruz 2016 2   52000   
2   loman willy 4   santa cruz 2016 3   55000   
3   kay mary 5  cochise 2016 1  40000   
3   kay mary 5  cochise 2016 2  41000   
3   kay mary 5  cochise 2016 3  42000   
3   kay mary 6  gila 2016 1 3000    
3   kay mary 6  gila 2016 2 31000   
3   kay mary 6  gila 2016 3 32000   
3   kay mary 7  graham 2016 1   20000   
3   kay mary 7  graham 2016 2   21000   
3   kay mary 7  graham 2016 3   22000   
4   lillian vernon NULL NULL    NULL NULL   NULL    checksum    4,994,000 

my tables look like this:

Create database sales 
use sales 

create table if not exists 
`Sales_People` 
(`person_id` int primary key, 
`last_name` char(16) not null, 
`first_name` char(16) not null);

INSERT INTO Sales_people (`person_id`, `last_name`, `first_name`) 
values 
('1', 'barnum', 'phineas'), 
('2', 'loman', 'willy'), 
('3', 'kay', 'mary'), 
('4', 'lillian', 'vernon');

create table if not exists 
`Sales_Region` (
`region_id` int primary key, 
`name` char(16) not null
);

INSERT INTO Sales_Region (`region_id`, `name`) 
Values 
('1', 'maricopa'), 
('2', 'pima'), 
('3', 'pinal'), 
('4', 'santa cruz'), 
('5', 'cochise'), 
('6', 'gila'), 
('7', 'graham'); 

create table if not exists 
`Sales_People_Region` (
`person_id` int not null, 
`region_id` int not null, 
constraint spr_pk primary key(person_id, region_id),

constraint spr_fk1 foreign key(person_id) 
references Sales_People(person_id), 

constraint spr_fk2 foreign key(region_id) 
references Sales_Region(region_id)
); 

INSERT INTO Sales_People_Region (`person_id`, `region_id`) 
Values 
('1', '1'), 
('2', '2'), 
('2','3'), 
('2', '4'), 
('3', '5'), 
('3', '6'), 
('3','7'); 

create table if not exists 
`Sales` (`year` int not null, 
`month` int not null, 
`region_id` int not null, 
`amount_sold` decimal(11,2), 

constraint s_pk primary key(year, month, region_id), 

constraint s_fk foreign key(region_id) references 
Sales_Region(region_id)); 

INSERT INTO Sales (`year`, `month`, `region_id`, `amount_sold`) 
Values 

('2016', '01', '1', '800000'), 
('2016', '02', '1', '850000'), 
('2016', '03', '1', '990000'), 
('2016', '01', '2', '425000'), 
('2016', '02', '2', '440000'), 
('2016', '03', '2', '450000'), 
('2016', '01', '3', '200000'), 
('2016', '02', '3', '210000'), 
('2016', '03', '3', '220000'), 
('2016', '01', '4', '50000'), 
('2016','02', '4', '52000'), 
('2016', '03', '4', '55000'), 
('2016', '01', '5', '40000'), 
('2016', '02', '5', '41000'), 
('2016', '03', '5', '42000'), 
('2016', '01', '6', '3000'), 
('2016', '02', '6', '31000'), 
('2016','03', '6', '32000'), 
('2016', '01', '7', '20000'), 
('2016', '02', '7', '21000'), 
('2016', '03', '7', '22000'); 

after some help my code now looks like this:

SELECT 
    sales_people.person_id,
    sales_people.last_name,
    sales_people.first_name,
    sales_region.Region_id, 
    TRIM(sales_region.`name`) AS 'Region Name', 
    year,
    month,
    amount_sold
from sales
join sales_people
  LEFT OUTER JOIN sales_people_region
    on sales_people.person_id = sales_people_region.person_id
  LEFT OUTER JOIN sales_region
    ON sales_people_region.region_id = sales_region.region_id 
  GROUP BY  sales_people.person_id,sales_region.region_id, month asc
    HAVING sales_people.person_id != ''
  ORDER BY sales_people.person_id, sales_region.region_id ASC, month asc;

my results look like this:

person_id,last_name,first_name,Region_id,"Region Name",year,month,amount_sold

1,barnum,phineas,1,maricopa,2016,1,800000.00
1,barnum,phineas,1,maricopa,2016,2,850000.00
1,barnum,phineas,1,maricopa,2016,3,990000.00
2,loman,willy,2,pima,2016,1,800000.00
2,loman,willy,2,pima,2016,2,850000.00
2,loman,willy,2,pima,2016,3,990000.00
2,loman,willy,3,pinal,2016,1,800000.00
2,loman,willy,3,pinal,2016,2,850000.00
2,loman,willy,3,pinal,2016,3,990000.00
2,loman,willy,4,"santa cruz",2016,1,800000.00
2,loman,willy,4,"santa cruz",2016,2,850000.00
2,loman,willy,4,"santa cruz",2016,3,990000.00
3,kay,mary,5,cochise,2016,1,800000.00
3,kay,mary,5,cochise,2016,2,850000.00
3,kay,mary,5,cochise,2016,3,990000.00
3,kay,mary,6,gila,2016,1,800000.00
3,kay,mary,6,gila,2016,2,850000.00
3,kay,mary,6,gila,2016,3,990000.00
3,kay,mary,7,graham,2016,1,800000.00
3,kay,mary,7,graham,2016,2,850000.00
3,kay,mary,7,graham,2016,3,990000.00
4,lillian,vernon,NULL,NULL,2016,1,800000.00
4,lillian,vernon,NULL,NULL,2016,2,850000.00
4,lillian,vernon,NULL,NULL,2016,3,990000.00

i have tried moving around the group by and order by to see if i could get the right amount_sold to line up with the person_id but no luck so far. i am unsure why it is doing that.

Best Answer

If the checksum part isn't required in the output then give this a try. The arrangement of the ORDER BY statement makes a difference in the output of your data.

SELECT 
    sales_people.person_id,
    sales_people.last_name,
    sales_people.first_name,
    sales_region.Region_id, 
    TRIM(sales_region.`name`) AS 'Region Name', 
    year,
    month,
    amount_sold

FROM sales
  RIGHT OUTER JOIN sales_people_region
    ON sales.region_id = sales_people_region.region_id
  RIGHT OUTER JOIN sales_region
    ON sales_people_region.region_id = sales_region.region_id
  RIGHT OUTER JOIN sales_people
    ON sales_people_region.person_id = sales_people.person_id
  ORDER BY sales_people.person_id, sales_region.region_id ASC, month ASC,amount_sold;

What was giving you trouble was the GROUP BY and the fact that you are using LEFT OUTER JOINS.

Cheers!