I agree with Maciej that this doesn't make much sense. In the real world you either need to deal with multi-currency invoices (and get the money!) OR not allow the multi-currency scenario in the first place.
However, you can get a count of the number of currencies used on each invoice with the following
select InvoiceNo, count(distinct currencyID) as CurrCount
FROM QuotationMaster A
INNER JOIN QuoteItems B ON A.QuoteID = B.QuoteID GROUP BY InvoiceNo
You could use that snippet in a CTE to feed into your main query e.g.
;with NoCurrs as
(
select InvoiceNo, count(distinct currencyID) as CurrCount
FROM QuotationMaster A
INNER JOIN QuoteItems B ON A.QuoteID = B.QuoteID GROUP BY InvoiceNo
)
SELECT A.QuoteID, datename(m,[Date]), A.InvoiceNo, CustomerId,
Currency = CASE WHEN N.CurrCount = 1 THEN MAX(CurrencyCode) ELSE '' END,
Amount = CASE WHEN N.CurrCount = 1 THEN SUM(Amount) ELSE 0.00 END
FROM NoCurrs N
INNER JOIN QuotationMaster A ON A.InvoiceNo = N.InvoiceNo
INNER JOIN QuoteItems B ON A.QuoteID = B.QuoteID
INNER JOIN CurrencyMaster C ON B.CurrencyID = C.CurrencyID
GROUP BY A.QuoteID, datename(m,[Date]), A.InvoiceNo, CustomerId,CurrCount
Points to note:
- Spot the use of CASE to determine whether or not I need the currency and amount
- I've used MAX(CurrencyCode) just to handle the "group by" scenario (i.e. I needed some sort of function in there)
- I haven't joined to other tables to get things like Customer Name - that's for you to add.
- You could use a sub-query instead, I just happen to like CTE's