SELECT CLCARD.DEFINITION_, CLCARD.CITY, ITEMS.NAME, SUM(ORFLINE.AMOUNT-ORFLINE.SHIPPEDAMOUNT), CLCARD.SPECODE, GNTOTST.ONHAND-ACTSORDER, CLCARD.DELIVERYFIRM, ORFLINE.LINETYPE
FROM LKSDB.dbo.LG_008_CLCARD CLCARD, LKSDB.dbo.LG_008_05_GNTOTST GNTOTST, LKSDB.dbo.LG_008_ITEMS ITEMS, LKSDB.dbo.LG_008_05_ORFLINE ORFLINE
WHERE CLCARD.LOGICALREF = ORFLINE.CLIENTREF AND ITEMS.LOGICALREF = ORFLINE.STOCKREF AND ITEMS.LOGICALREF = GNTOTST.STOCKREF AND ((GNTOTST.INVENNO='0') AND (ORFLINE.LINETYPE In (0,1)) AND (ORFLINE.AMOUNT-ORFLINE.SHIPPEDAMOUNT>0) AND (ITEMS.CARDTYPE=1))
GROUP BY CLCARD.DEFINITION_, CLCARD.CITY, ITEMS.NAME, CLCARD.SPECODE, GNTOTST.ONHAND-ACTSORDER, CLCARD.DELIVERYFIRM, ORFLINE.LINETYPE
ORDER BY CLCARD.DEFINITION_