![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
select ptNum, epiNum, ptStmtDir from episodes where (ptnum, epiNum) in (select ptNum, epiNum from transactions where postingDate between cast('$firstdate' as date) and cast('$lastdate' as date)) union (select ptNum, epiNum from paymentDistribution join payment on paymentDistribution.paymentDNumber = payment.paymentNumber where verifyDate between cast('$firstdate' as date) and cast('$lastdate' as date)) union (select ptNum, epiNum from beginningBalances where date between cast('$firstdate' as date) and cast('$lastdate' as date)) |
#3
| |||
| |||
|
|
On 2010-07-04 13:24, bill wrote: select ptNum, epiNum, ptStmtDir from episodes where (ptnum, epiNum) in (select ptNum, epiNum from transactions where postingDate between cast('$firstdate' as date) and cast('$lastdate' as date)) union (select ptNum, epiNum from paymentDistribution join payment on paymentDistribution.paymentDNumber = payment.paymentNumber where verifyDate between cast('$firstdate' as date) and cast('$lastdate' as date)) union (select ptNum, epiNum from beginningBalances where date between cast('$firstdate' as date) and cast('$lastdate' as date)) Your first part is not union compatible with the two latter ones. If you don't have something that corresponds to episodes.ptStmtDir, you can add a dummy placeholder as: |
#4
| |||
| |||
|
|
On 2010-07-04 14:11, Lennart Jonsson wrote: On 2010-07-04 13:24, bill wrote: select ptNum, * * * * *epiNum, * * * * *ptStmtDir * * * *from episodes * * * *where (ptnum, epiNum) in * * * * * * * * * (select ptNum, * * * * * * * * * * * * * * epiNum * * * * * * * * * * * * * * from transactions * * * * * * * * * * * * * * where postingDate between cast('$firstdate' as date) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * and cast('$lastdate' as date)) * * * * * *union * * * * * * * * * (select ptNum, * * * * * * * * * * * * * * epiNum * * * * * * * * * * * * * * from paymentDistribution * * * * * * * * * * * * * * * *join payment on paymentDistribution.paymentDNumber = payment.paymentNumber * * * * * * * * * * * * * * * *where *verifyDate between cast('$firstdate' as date) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * and cast('$lastdate' as date)) * * * *union * * * * * * * * * *(select ptNum, * * * * * * * * * * * * * * * epiNum * * * * * * * * * * * * * * * from beginningBalances * * * * * * * * * * * * * * * *where date between cast('$firstdate' as date) * * * * * * * * * * * * * * * * * * * * * * * * * * * *and cast('$lastdate' as date)) Your first part is not union compatible with the two latter ones. If you don't have something that corresponds to episodes.ptStmtDir, you can add a dummy placeholder as: Wait a minute, I did not read your text carefully enough. Your problem probably lies in the closing ) of the in predicate: select ptNum, epiNum, ptStmtDir from episodes where (ptnum, epiNum) in ( * * select ptNum, epiNum * * from transactions * * where postingDate between cast('$firstdate' as date) * * * * * * * * * * * * * and cast('$lastdate' as date) -- BEGIN: Should this really be here? ) |
|
-- END: union I'd say this is easier to spot with indented code, agreed ;-) However, try avoiding the in predicate. Your union query is a derived table, i.e. you can join that as with any other table. |
#5
| |||
| |||
|
|
On 2010-07-04 13:24, bill wrote: select ptNum, * * *epiNum, * * *ptStmtDir * *from episodes * *where (ptnum, epiNum) in * * * * * * * * * (select ptNum, * * * * * * * * * * * * * * epiNum * * * * * * * * * * * * * * from transactions * * * * * * * * * * * * * * where postingDate between cast('$firstdate' as date) * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * and cast('$lastdate' as date)) * * * * * *union * * * * * * * * * (select ptNum, * * * * * * * * * * * * * * epiNum * * * * * * * * * * * * * * from paymentDistribution * * * * * * * * * * * * * * * *join payment on paymentDistribution.paymentDNumber = payment.paymentNumber * * * * * * * * * * * * * * * *where *verifyDate between cast('$firstdate' as date) * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * and cast('$lastdate' as date)) * *union * * * * * * * * * *(select ptNum, * * * * * * * * * * * * * * * epiNum * * * * * * * * * * * * * * * from beginningBalances * * * * * * * * * * * * * *where date between cast('$firstdate' as date) * * * * * * * * * * * * * * * * * ** * * * * * * * * *and cast('$lastdate' as date)) Your first part is not union compatible with the two latter ones. If you don't have something that corresponds to episodes.ptStmtDir, you can add a dummy placeholder as: cast(null as <typeof episodes.ptStmtDir>) in the other two. As for efficiency, mysql does not seem to handle in predicates very well. It is often suggested to replace it with a join where possible: select ptNum, epiNum, ptStmtDir from episodes where (ptnum, epiNum) in ( * * select ptNum, epiNum * * from transactions * * where postingDate between cast('$firstdate' as date) * * * * * * * * * * * * * and cast('$lastdate' as date) * * * * * * * * * * * * *) would then become: select x.ptNum, x.epiNum, x.ptStmtDir from episodes x join transactions y * * on (x.ptnum, x.epiNum) = (y.ptnum, y.epiNum) where y.postingDate between cast('$firstdate' as date) * * * * * * * * * * * * and cast('$lastdate' as date) /Lennart |
#6
| |||
| |||
|
|
On 2010-07-04 14:11, Lennart Jonsson wrote: On 2010-07-04 13:24, bill wrote: select ptNum, * * * * *epiNum, * * * * *ptStmtDir * * * *from episodes * * * *where (ptnum, epiNum) in * * * * * * * * * (select ptNum, * * * * * * * * * * * * * * epiNum * * * * * * * * * * * * * * from transactions * * * * * * * * * * * * * * where postingDate between cast('$firstdate' as date) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * and cast('$lastdate' as date)) * * * * * *union * * * * * * * * * (select ptNum, * * * * * * * * * * * * * * epiNum * * * * * * * * * * * * * * from paymentDistribution * * * * * * * * * * * * * * * *join payment on paymentDistribution.paymentDNumber = payment.paymentNumber * * * * * * * * * * * * * * * *where *verifyDate between cast('$firstdate' as date) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * and cast('$lastdate' as date)) * * * *union * * * * * * * * * *(select ptNum, * * * * * * * * * * * * * * * epiNum * * * * * * * * * * * * * * * from beginningBalances * * * * * * * * * * * * * * * *where date between cast('$firstdate' as date) * * * * * * * * * * * * * * * * * * * * * * * * * * * *and cast('$lastdate' as date)) Your first part is not union compatible with the two latter ones. If you don't have something that corresponds to episodes.ptStmtDir, you can add a dummy placeholder as: Wait a minute, I did not read your text carefully enough. Your problem probably lies in the closing ) of the in predicate: select ptNum, epiNum, ptStmtDir from episodes where (ptnum, epiNum) in ( * * select ptNum, epiNum * * from transactions * * where postingDate between cast('$firstdate' as date) * * * * * * * * * * * * * and cast('$lastdate' as date) -- BEGIN: Should this really be here? ) -- END: union I'd say this is easier to spot with indented code, agreed ;-) However, try avoiding the in predicate. Your union query is a derived table, i.e. you can join that as with any other table. |
#7
| |||
| |||
|
|
On Jul 4, 8:19 am, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com wrote: On 2010-07-04 14:11, Lennart Jonsson wrote: On 2010-07-04 13:24, bill wrote: select ptNum, epiNum, ptStmtDir from episodes where (ptnum, epiNum) in (select ptNum, epiNum from transactions where postingDate between cast('$firstdate' as date) and cast('$lastdate' as date)) union (select ptNum, epiNum from paymentDistribution join payment on paymentDistribution.paymentDNumber = payment.paymentNumber where verifyDate between cast('$firstdate' as date) and cast('$lastdate' as date)) union (select ptNum, epiNum from beginningBalances where date between cast('$firstdate' as date) and cast('$lastdate' as date)) Your first part is not union compatible with the two latter ones. If you don't have something that corresponds to episodes.ptStmtDir, you can add a dummy placeholder as: Wait a minute, I did not read your text carefully enough. Your problem probably lies in the closing ) of the in predicate: select ptNum, epiNum, ptStmtDir from episodes where (ptnum, epiNum) in ( select ptNum, epiNum from transactions where postingDate between cast('$firstdate' as date) and cast('$lastdate' as date) -- BEGIN: Should this really be here? ) That paren closes the first select that is unioned. |
|
The 3 later selects form the subquery, each unioned to the prior one. The code after the "in" runs fine by itself. -- END: union I'd say this is easier to spot with indented code, agreed ;-) However, try avoiding the in predicate. Your union query is a derived table, i.e. you can join that as with any other table. |
#8
| |||
| |||
|
|
However, try avoiding the in predicate. Your union query is a derived table, i.e. you can join that as with any other table. How would I join the union derrived table, by putting parens around the union selects ? |
#9
| |||
| |||
|
|
On 2010-07-04 15:29, bill wrote: On Jul 4, 8:19 am, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com wrote: On 2010-07-04 14:11, Lennart Jonsson wrote: On 2010-07-04 13:24, bill wrote: select ptNum, * * * * *epiNum, * * * * *ptStmtDir * * * *from episodes * * * *where (ptnum, epiNum) in * * * * * * * * * (select ptNum, * * * * * * * * * * * * * * epiNum * * * * * * * * * * * * * * from transactions * * * * * * * * * * * * * * where postingDate between cast('$firstdate' as date) * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * and cast('$lastdate' as date)) * * * * * *union * * * * * * * * * (select ptNum, * * * * * * * * * * * * * * epiNum * * * * * * * * * * * * * * from paymentDistribution * * * * * * * * * * * * * * * *joinpayment on paymentDistribution.paymentDNumber = payment.paymentNumber * * * * * * * * * * * * * * * *where *verifyDate between cast('$firstdate' as date) * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * and cast('$lastdate' as date)) * * * *union * * * * * * * * * *(select ptNum, * * * * * * * * * * * * * * * epiNum * * * * * * * * * * * * * * * from beginningBalances * * * * * * * * * * * * * * * *where date between cast('$firstdate' as date) * * * * * * * * * * * * * * * * ** * * * * * * * * * *and cast('$lastdate' as date)) Your first part is not union compatible with the two latter ones. Ifyou don't have something that corresponds to episodes.ptStmtDir, you canadd a dummy placeholder as: Wait a minute, I did not read your text carefully enough. Your problem probably lies in the closing ) of the in predicate: select ptNum, epiNum, ptStmtDir from episodes where (ptnum, epiNum) in ( * * select ptNum, epiNum * * from transactions * * where postingDate between cast('$firstdate' as date) * * * * * * * * * * * * * and cast('$lastdate' as date) -- BEGIN: Should this really be here? ) That paren closes the first select that is unioned. Let's start by indenting the code: select ptNum, epiNum, ptStmtDir from episodes where (ptnum, epiNum) in ( * * select ptNum, epiNum * * from transactions * * where postingDate between cast('$firstdate' as date) * * * * * * * * * * * * * and cast('$lastdate' as date) ) union select ptNum, epiNum from paymentDistribution join payment * * on paymentDistribution.paymentDNumber = payment.paymentNumber where *verifyDate between cast('$firstdate' as date) * * * * * * * * * * * and cast('$lastdate' as date) union select ptNum, epiNum from beginningBalances where date between cast('$firstdate' as date) * * * * * * * *and cast('$lastdate' as date) Do you see a problem here? The result of first part of your query has type - say - int * int * string, whilst second and third part has type int * int. I.e. part 2 and 3 are not union compatible with part 1, which is what mysql is complaining about. In my first answer I suggested that you make the second and third part union compatible via: [...] union select ptNum, epiNum, cast(null as ...) from paymentDistribution join payment * * on paymentDistribution.paymentDNumber = payment.paymentNumber where *verifyDate between cast('$firstdate' as date) * * * * * * * * * * * and cast('$lastdate' as date) union select ptNum, epiNum, cast(null as ...) [...] But when I reread your post I was pretty sure that what you meant was: select ptNum, epiNum, ptStmtDir from episodes where (ptnum, epiNum) in ( * * select ptNum, epiNum * * from transactions * * where postingDate between cast('$firstdate' as date) * * * * * * * * * * * * * and cast('$lastdate' as date) * * union * * select ptNum, epiNum * * from paymentDistribution * * join payment * * * * on paymentDistribution.paymentDNumber = payment.paymentNumber * * where *verifyDate between cast('$firstdate' as date) * * * * * * * * * * * * * and cast('$lastdate' as date) * * union * * select ptNum, epiNum * * from beginningBalances * * where date between cast('$firstdate' as date) * * * * * * * * * *and cast('$lastdate' as date) ) Before we continue this discussion, we need to straighten out which query to continue with. |
#10
| |||
| |||
|
|
On 2010-07-04 15:32, bill wrote: [...] However, try avoiding the in predicate. Your union query is a derived table, i.e. you can join that as with any other table. How would I join the union derrived table, by putting parens around the union selects ? I'll answer this once we have established what it is you are trying to do, see my other reply. /Lennart |
![]() |
| Thread Tools | |
| Display Modes | |
| |