Skip to content
Advertisement

SQL: if value exists then show else hide

I have an SQL query that filters down purchase credit and invoices and removes any zero tax amounts, it has just come to light that there is some i need to include for me to get the correct figure. Luckily these few invoices and credit notes can be identified on there reference number, unfortunately i don not know how to write such a query. I am guessing it should be an if statement. Any help would be much appreciated, query is below.

As you can see i have commented out the AND T_PURCHASECREDITNOTE.C_TAXAMOUNT <> 0 and AND T_PURCHASEINVOICE.C_TAXAMOUNT <> 0 as that will remove every entry that has a zero tax amount.

The references i need to show are as follows purchase credit: MRCR and purchase invoice: MCINV. Below is what needs to be added to the other query, if C_TAXAMOUNT is zero don’t show unless C_ALTERNATIVEREFERENCE LIKE ‘%MRCR%’, that’s how i see it unfortunately haven’t got the skills to implement.

Advertisement

Answer

SQL works by filtering data, so the concept of if statements doesn’t really apply here. Rather you’re using a condition to select what should be returned, which is what SQL’s WHERE clause does; any rows matching the WHERE condition are returned, any which don’t aren’t.

You want those with zero tax OR those with a specific reference (including if both are true; but OR covers that too).

So you can replace:

with

Thus only if one or both of the conditions within the brackets returns true will that row be included in the results.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement