mysql - Select where with a select? -
i have query finding people have outstanding balance not equal 0. though ok having expanded sample data available have discover not quite there (not far off think).
the code below:
select bookingref, leadname, departuredate, balanceduedate, totaldue, totalreceived, (totaldue - totalreceived) outstandingbalance, (select sum(case when(totaldue - totalreceived) <> 0 1 else 0 end) bookings) numberoutstanding, (select sum(totaldue) bookings) grossdue, (select sum(totalreceived) bookings) grossreceived, (select sum(totaldue - totalreceived) bookings) grossoutstanding bookings (amendeddate between '2015-09-30' , '2016-09-30') having outstandingbalance <> 0;
what's happening when date across complete range results correct. when doing year above still sums total range, ignoring clause sums.
i think need add clause each sum select keep running run errors.
how achieve this.
many thanks
note: rusty mysql (8-10yrs without using it).
a solution without window functions (for mysql)
unfortunately, mysql doesn't support window functions, more convenient query. instead, can use cross join in case. won't around applying predicates twice, though, mysql doesn't support common table expressions:
select b.bookingref, b.leadname, b.departuredate, b.balanceduedate, b.totaldue, b.totalreceived, (b.totaldue - b.totalreceived) outstandingbalance, a.* bookings b, cross join ( select sum(case when(totaldue - totalreceived) <> 0 1 else 0 end numberoutstanding, sum(totaldue) grossdue, sum(totalreceived) grossreceived, sum(totaldue - totalreceived) grossoutstanding bookings (amendeddate between '2015-09-30' , '2016-09-30') , (totaldue - totalreceived) <> 0 ) (b.amendeddate between '2015-09-30' , '2016-09-30') , (b.totaldue - b.totalreceived) <> 0;
alternatively, have duplicated predicate each of subqueries in select
clause, approach perform worse.
a solution window functions
for completeness' sake, in other databases, writing query instead:
select bookingref, leadname, departuredate, balanceduedate, totaldue, totalreceived, (totaldue - totalreceived) outstandingbalance, sum(case when(totaldue - totalreceived) <> 0 1 else 0 end) over() numberoutstanding, sum(totaldue) over() grossdue, sum(totalreceived) over() grossreceived, sum(totaldue - totalreceived) over() grossoutstanding bookings (amendeddate between '2015-09-30' , '2016-09-30') , (b.totaldue - b.totalreceived) <> 0;
a side-note on having
i suspect you're using having
clause in order able re-use column name select
clause. pretty mysql-specific , doesn't intended. having
applies after group by
(and thus, after aggregation). in query, don't want group by
clause, avoid using having
Comments
Post a Comment