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

From Dev

MySQL - how do I do an outer join where either side is optional - left and right outer join

From Dev

Select query with left outer join and sum with group by

From Dev

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

From Dev

SUM mixed with LEFT JOIN and GROUP BY in MYSQL query

From Dev

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

From Dev

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

From Dev

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

From Dev

SUM, GROUP BY AND LEFT JOIN in query

From Dev

How to use if condition with left join in mysql

From Dev

How can I get a LEFT JOIN or FULL OUTER JOIN?

From Dev

Select rows with Left Outer Join and condition - MySQL

From Dev

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

From Dev

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

From Dev

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

From Dev

How to Use Where Condition With Join Query in Laravel

From Dev

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

From Dev

MySQL - endless running query on LEFT OUTER JOIN

From Dev

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

From Dev

In Join condition I want to use group by and having clause but get error? How to use group by and having clause

From Dev

How do I do a LEFT JOIN in MySQL, where I have missing key values in the RIGHT table?

From Dev

How to Write Left Outer join for this SQL query?

From Dev

Issue with join and sum in mysql query: invalid use of group function

From Dev

mysql - how to UPDATE after LEFT OUTER JOIN

From Dev

How to use left outer join in Linq

From Dev

MySQL double left join, condition OR and group

From Dev

How to do a Left Outer join with Laravel?

From Dev

How to do left outer join exclusion in pandas

From Dev

How to do LEFT OUTER JOIN with include

From Dev

Mysql LEFT JOIN same table with WHERE condition

Related Related

  1. 1

    MySQL - how do I do an outer join where either side is optional - left and right outer join

  2. 2

    Select query with left outer join and sum with group by

  3. 3

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

  4. 4

    SUM mixed with LEFT JOIN and GROUP BY in MYSQL query

  5. 5

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

  6. 6

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

  7. 7

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

  8. 8

    SUM, GROUP BY AND LEFT JOIN in query

  9. 9

    How to use if condition with left join in mysql

  10. 10

    How can I get a LEFT JOIN or FULL OUTER JOIN?

  11. 11

    Select rows with Left Outer Join and condition - MySQL

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

    How to Use Where Condition With Join Query in Laravel

  16. 16

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

  17. 17

    MySQL - endless running query on LEFT OUTER JOIN

  18. 18

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

  19. 19

    In Join condition I want to use group by and having clause but get error? How to use group by and having clause

  20. 20

    How do I do a LEFT JOIN in MySQL, where I have missing key values in the RIGHT table?

  21. 21

    How to Write Left Outer join for this SQL query?

  22. 22

    Issue with join and sum in mysql query: invalid use of group function

  23. 23

    mysql - how to UPDATE after LEFT OUTER JOIN

  24. 24

    How to use left outer join in Linq

  25. 25

    MySQL double left join, condition OR and group

  26. 26

    How to do a Left Outer join with Laravel?

  27. 27

    How to do left outer join exclusion in pandas

  28. 28

    How to do LEFT OUTER JOIN with include

  29. 29

    Mysql LEFT JOIN same table with WHERE condition

HotTag

Archive