MySQL Query - How do I get a SUM with GROUP BY and WHERE condition and use LEFT OUTER JOIN?

compcentral

I'm not sure how to get the result that I expect and I've tried everything. I also need to be able to get the expected results using one query.

Here are the two tables with some sample data:

--
-- Table structure for table `accounts`
--

CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(11) NOT NULL,
`name` varchar(200) NOT NULL,
`type_id` int(11) NOT NULL,
`description` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Data for table `accounts`
--

INSERT INTO `accounts` (`id`, `name`, `type_id`, `description`) VALUES
(100, 'DUES', 0, NULL),
(101, 'NET WEEKLY PAYROLL', 0, NULL),
(111, 'FEDERAL TAX DEPOSITS', 0, 'tax stuff'),
(113, 'UNITED ASSOCIATION PAYMENTS', 0, NULL),
(114, 'OFFICERS MEETING ALLOWANCES', 0, NULL);

--
-- Table structure for table `checks`
--

CREATE TABLE IF NOT EXISTS `checks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `batch_id` int(11) DEFAULT NULL,
  `entry_date` date NOT NULL,
  `account_id` int(11) NOT NULL,
  `amount` decimal(10,2) NOT NULL,
  `description` varchar(200) DEFAULT NULL,
  `posted` tinyint(4) NOT NULL DEFAULT '0',
  `vendor_id` int(11) DEFAULT NULL,
  `check_num` int(11) NOT NULL,
  `voided` tinyint(4) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `checks`
--

INSERT INTO `checks` (`id`, `batch_id`, `entry_date`, `account_id`, `amount`, `description`, `posted`, `vendor_id`, `check_num`, `voided`) VALUES
(1, NULL, '2013-01-21', 111, '77.44', 'Last Year', 0, 1, 100, 0),
(2, NULL, '2014-01-21', 111, '521.11', 'Test Stuff', 0, 1, 101, 0),
(3, NULL, '2014-01-20', 101, '121.11', 'More Tests', 0, 1, 222, 0),
(4, NULL, '2014-01-02', 101, '150.00', 'test', 0, 4, 213, 0);

I want to create a list of all accounts with a month-to-date sum as an added field. Here is the query to get the month-to-date sum without joining the accounts table:

SELECT *, SUM(amount) as mtd FROM `checks` WHERE `entry_date` > '2014-01-01' GROUP BY `account_id`

... and here is what i used to get all the accounts joined to checks table:

SELECT * FROM `accounts` LEFT OUTER JOIN `checks` ON `checks.account_id` = `accounts.id`

I just can't seem to combine these two correctly to get the expected results. Please help!

Gordon Linoff

I think this solves your problem:

SELECT a.*, SUM(c.amount) as mtd
FROM accounts a left outer join
     checks c
     ON (a.id = c.account_id) and c.entry_date >= '2014-01-01'
GROUP BY a.account_id;

This will return all accounts, even those with no activity in January. I changed the date condition to >=, because that "feels" better as a month-to-date cutoff.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sequelize: how to do a WHERE condition on joined table with left outer join

How to get field "bikinMain" values from left outer join on multi sum in master table mysql query

how do I use JOIN, WHERE AND GROUP BY in the same query

How to use IN condition along with LEFT OUTER JOIN and ON

SUM mixed with LEFT JOIN and GROUP BY in MYSQL query

How to do this query in MySQL which one should I use using left join or right join or inner join?

MySQL LEFT JOIN with GROUP BY and WHERE clause query

LEFT JOIN not working if I use WHERE condition

How to form complex mysql query that has left outer join, aggregate data with group by using SQLAlchemy?

How to add where condition in left join in mysql

How to use WHERE condition with the LEFT JOIN?

LEFT JOIN on mysql query do not get results like i want

SQL left outer join with where condition

MySQL: Limiting LEFT OUTER JOIN / Left outer join where id = #

SUM, GROUP BY AND LEFT JOIN in query

How to use if condition with left join in mysql

Select rows with Left Outer Join and condition - MySQL

Executing a query using sum, count, group by and multiple left join MySQL

MySQL Multiplied query result (LEFT JOIN, SUM, COUNT, GROUP BY)

In python pandas,How to use outer join using where condition?

How to do a double left outer join in Linq query syntax(or fluent)

How to Use Where Condition With Join Query in Laravel

MySQL Left outer join with where clause

MySQL Query LEFT JOIN, SUM()

MySQL - LEFT JOIN - How do it get the value that i need

How can I use if else in where condition in mysql query?

How to left outer join with extra condition in Django

MySQL - endless running query on LEFT OUTER JOIN

sum doesn't work while I use left join and group by