Merhaba,
Eski tarihli sorgulardan bir tanesi: son alış fiyatı, karşı sipariş fiyatı ve tanımlı alış fiyatlarını, satış fiyatının yanına koyarak kar/zarar inceleyebileceğiniz bir sorgu:
SELECT
CH.DEFINITION_,
MALZ.NAME,
MALZHAR.AMOUNT,
BRM.CODE AS BIRIM,
ROUND(MALZHAR.VATMATRAH/MALZHAR.AMOUNT,4) AS BIRIMFIYAT,
ISNULL((SELECT TOP (1) ROUND(VATMATRAH/NULLIF(AMOUNT,0),4) FROM LG_507_01_STLINE WHERE TRCODE=1 AND MALZHAR.DATE_>=DATE_ AND MALZHAR.STOCKREF=STOCKREF AND CANCELLED=0 AND BILLED=1 ORDER BY DATE_ DESC),0)
AS 'SON ALIS FIYATI',
ISNULL((SELECT TOP (1) (BRM1.CODE) FROM LG_507_01_STLINE STL LEFT JOIN LG_507_UNITSETL BRM1 ON STL.UOMREF=BRM1.LOGICALREF WHERE TRCODE=1 AND MALZHAR.DATE_>=DATE_ AND MALZHAR.STOCKREF=STOCKREF AND CANCELLED=0 AND BILLED=1 ORDER BY DATE_ DESC),0)
AS 'SONALIS BIRIM',
CASE WHEN MALZHAR.ORDTRANSREF=0 THEN 0 WHEN MALZHAR.ORDTRANSREF <>0 THEN ISNULL(ROUND(MALZHAR2.VATMATRAH/MALZHAR2.AMOUNT,4),0) END AS 'KARSISIP FIYATI',
CASE WHEN MALZHAR.ORDTRANSREF <> 0 THEN ISNULL((SELECT CODE FROM LG_507_UNITSETL WHERE LG_507_UNITSETL.LOGICALREF=MALZHAR2.UOMREF),'') ELSE '' END AS 'KARSISIP BIRIM',
CASE WHEN MALZHAR.AMOUNT=0 THEN 0
WHEN MALZHAR.AMOUNT<>0 THEN
ISNULL((SELECT TOP (1)
CASE
WHEN CURRENCY=0 THEN ROUND(PRICE,4)
WHEN CURRENCY=160 THEN ROUND(PRICE,4)
WHEN CURRENCY=1 THEN ROUND(PRICE*(SELECT TOP (1) RATES1 FROM LG_EXCHANGE_507 WHERE EDATE<=MALZHAR.DATE_ AND CRTYPE=1 ORDER BY EDATE DESC),4)
WHEN CURRENCY=20 THEN ROUND(PRICE*(SELECT TOP (1) RATES1 FROM LG_EXCHANGE_507 WHERE EDATE<=MALZHAR.DATE_ AND CRTYPE=20 ORDER BY EDATE DESC),4)
WHEN CURRENCY IS NULL THEN ISNULL(ROUND(PRICE,4),0)
END AS 'TANIMLI ALIS-TL'
FROM LG_507_PRCLIST WHERE CARDREF=MALZHAR.STOCKREF AND MALZHAR.DATE_>=BEGDATE AND PTYPE=1 ),0)
END AS 'TANIMLI ALIS-TL',
CASE WHEN 'TANIMLI ALIS-TL' = '0' THEN '' WHEN 'TANIMLI ALIS-TL' <>'0' THEN
ISNULL((
SELECT TOP (1) BRM3.CODE FROM LG_507_PRCLIST AS FL LEFT JOIN LG_507_UNITSETL AS BRM3 ON FL.UOMREF=BRM3.LOGICALREF WHERE CARDREF=MALZHAR.STOCKREF AND MALZHAR.DATE_>=BEGDATE AND PTYPE=1
),'') END AS 'TANIMLI ALIS BIRIM'
FROM LG_507_01_STLINE AS MALZHAR
LEFT JOIN LG_507_UNITSETL AS BRM ON MALZHAR.UOMREF=BRM.LOGICALREF
LEFT JOIN LG_507_ITEMS AS MALZ ON MALZHAR.STOCKREF=MALZ.LOGICALREF
LEFT JOIN LG_507_01_INVOICE AS FAT ON MALZHAR.INVOICEREF=FAT.LOGICALREF
LEFT JOIN LG_507_CLCARD AS CH ON MALZHAR.CLIENTREF=CH.LOGICALREF
LEFT JOIN LG_507_01_ORDPEGGING AS KARSIP ON MALZHAR.ORDTRANSREF=KARSIP.PARENTREF
LEFT JOIN LG_507_01_STLINE AS MALZHAR2 ON KARSIP.PURORDLNREF=MALZHAR2.ORDTRANSREF
WHERE MALZ.ACTIVE=0 AND MALZ.CARDTYPE=1 AND MALZHAR.LINETYPE=0 AND MALZHAR.CANCELLED=0 AND MALZHAR.BILLED=1 AND MALZHAR.TRCODE IN (7,8)
ORDER BY MALZ.NAME, MALZHAR.DATE_
Umarım işinize yarar.