Search code examples
axaptadynamics-ax-2009x++

Mixing temp table and real tables in query


Why does one of these queries work and the other does not? I wrote the working query, then made the table temporary and it has to be rewritten to have the temp table on the outside.

This is the data in the tables:

tmpTASItemQtyInventDim:

|ItemId | InventDimId  | InventQty |
|-------|--------------|-----------|
|73016  | Dim 11542913 | 0         |

SalesLine:

|Sales Id    |Line No | ItemId | InventDimId  | SalesQty  |
|------------|--------|--------|--------------|-----------|
|SO120036796 |       1| 73016  | Dim 11542913 | 2         |

Does Not work:

select firstonly tmpTASItemQtyInventDim
    join itemId, InventDimId, sum(salesQty) from salesLine
    group by itemId, InventDimId
    where salesLine.SalesId == 'SO120036796'  &&
          tmpTASItemQtyInventDim.ItemId == salesLine.ItemId     &&
          tmpTASItemQtyInventDim.InventDimId    == salesLine.InventDimId    &&
          tmpTASItemQtyInventDim.InventQty      < salesLine.SalesQty;

if (tmpTASItemQtyInventDim.ItemId)
    info("Insufficient Qty");
else
    info("Good qty");

Works:

select firstonly itemId, InventDimId, sum(salesQty) from salesLine
    group by itemId, InventDimId
    where salesLine.SalesId     == 'SO120036796'
    exists join tmpTASItemQtyInventDim
        where tmpTASItemQtyInventDim.ItemId == salesLine.ItemId     &&
              tmpTASItemQtyInventDim.InventDimId    == salesLine.InventDimId    &&
              tmpTASItemQtyInventDim.InventQty      < salesLine.SalesQty;

if (salesLine.ItemId)
    info("Insufficient Qty");
else
    info("Good Qty");

Both should output Insufficient Qty, but the first says Good Qty.


Solution

  • From memory I think you need to group by on the first table as well see http://axatluegisdorf.blogspot.co.uk/2010/07/select-group-by-and-join-order-by.html

    select firstonly ITEMID,InventDimId 
      FROM  tmpTASItemQtyInventDim
      GROUP BY ITEMID,InventDimId
    join itemId, InventDimId, sum(salesQty) 
    from salesLine
     group by itemId, InventDimId
    where salesLine.SalesId == 'SO120036796'  &&
          tmpTASItemQtyInventDim.ItemId == salesLine.ItemId     &&
          tmpTASItemQtyInventDim.InventDimId    == salesLine.InventDimId    &&
          tmpTASItemQtyInventDim.InventQty      < salesLine.SalesQty;
    
    if (tmpTASItemQtyInventDim.ItemId)
       info("Insufficient Qty");
    else
       info("Good qty");