Skip to main content
Known Participant
November 20, 2009
Question

How can I combine two queries ? QoQ does not work

  • November 20, 2009
  • 1 reply
  • 368 views

I have one query where I just count the total qty coming in per month, something like:

<cfquery name="qryIn" datasource="dbname">

select count(orderNO) as totalIN,month

where status = "IN"

group by month

</cfquery>

I then have a second query to count the total qty going out per month

<cfquery name="qryOut" datasource="dbname">

select count(orderNO) as totalOut,month

where status = "OUT"

group by month

</cfquery>

I then use QoQ to combine both:

<cfquery="qryTotal" dbtype="query">

select

totalIN,

totalOUT

from qryIN,qryOUT

where qryIN.month = qryOUT.month

</cfquery>

The problem I am running into is that QoQ does not allow LEFT JOIN, so that if the month is in one query but not the other, it will not pick up that record. And that is throwing off my counts.

How can I combine  both queries, and bypass QoQ to get a qty IN and qty Out value, per month ? and, for example, if qty in exists for one month and qty Out does not exists for that month, then qty Out will be zero for that month.

I need this data to plot a chart.

Thanks for any help provided.

This topic has been closed for replies.

1 reply

Inspiring
November 20, 2009

Do it in a single query to your database.  Here is part of it.

select month

, sum(case when when status = "IN" then 1 else 0 end) total_in