I used the following WHERE clause
WHERE (TYPE1 = :P3_ITEM1 OR :P3_ITEM1 IS NULL)
to filter my query on TYPE1 and it worked fine to display the records of same TYPE1 when page item P3_ITEM1 is not null.
Now I need to add two more filters for TYPE2 and TYPE3. Normally only one page item P3_ITEM1, P3_ITEM2, or P3_ITEM3 is not null, so I need to filter on whichever on is not null.
I tried
WHERE (TYPE1=:P3_ITEM1 OR :P3_ITEM1 IS NULL) OR
(TYPE2=:P3_ITEM2 OR :P3_ITEM2 IS NULL) OR
(TYPE3=:P3_ITEM3 OR :P3_ITEM3 IS NULL)
but it did not work.
I tried using CASE statement in my WHERE clause but no success so far. Can anyone help?
WHERE CASE
WHEN (:P3_ITEM1 IS NOT NULL) THEN (TYPE1=:P3_ITEM1)
WHEN (:P3_ITEM2 IS NOT NULL) THEN (TYPE2=:P3_ITEM2)
WHEN (:P3_ITEM3 IS NOT NULL) THEN (TYPE3=:P3_ITEM3)
You can simply use AND to combine the various filters. Each filter will evaluate to true if the parameter for it is NULL, so if all three are NULL you will get all records, but you can fill in a any, or even all three parameters to filter out unwanted records.
WHERE
(TYPE1 = :P3_ITEM1 OR :P3_ITEM1 IS NULL) AND
(TYPE2 = :P3_ITEM2 OR :P3_ITEM2 IS NULL) AND
(TYPE3 = :P3_ITEM3 OR :P3_ITEM3 IS NULL)
Related
Here is the code:
WHERE 1=1
AND TU.Auction_I IN (41, 42)
AND #StatePickupID = CASE
WHEN #StatePickupID IS NOT NULL
THEN (UP.TransportStateID = #StatePickupID)
END
AND #StateDropoffID = CASE
WHEN #StateDropoffID IS NOT NULL
THEN (UD.TransportStateID = #StateDropoffID)
END
So I only want to return records where UP.TransportStateID is equal to StatePickupID if it is not null, same thing for DropoffID. But I get a syntax error message where the equals sign is and I cannot find any other way to check for equality online. Any help or advice would be much appreciated.
I only want to return records where UP.TransportStateID is equal to StatePickupID if it is not null
This would translate as the following predicate:
#StatePickupID IS NULL OR #StatePickupID = UP.TransportStateID
When the parameter is not null, the predicate filters on TransportStateID; when the parameter is null, the filter is a no-op.
In your where clause, for both parameters :
WHERE 1 = 1
AND TU.Auction_I IN (41, 42)
AND ( #StatePickupID IS NULL OR #StatePickupID = UP.TransportStateID )
AND ( #StateDropoffID IS NULL OR #StateDropoffID = UD.TransportStateID )
I am trying to create a multi-criteria query based on several combo boxes where if they are left empty, the result should include all records. I have tried several variations of below but to no use.
I did a lot of searching and found to inculde OR 'Control Name' IS NULL but still can't seem to get it working.
I am trying to sum up Revenue grouped by Revenue Identifier (eg. Verified/Unverified/Deleted).
SELECT Sum(sTblLineItemMaster.lineItemTotal) AS SumOflineItemTotal, sTblLineItemMaster.lineItemStatus
FROM sTblActivityMaster INNER JOIN sTblLineItemMaster ON sTblActivityMaster.actID = sTblLineItemMaster.lineItemActID
WHERE
((sTblActivityMaster.actTargetYear=[Forms]![frmActivityList]![cmbSearchYear] Or [Forms]![frmActivityList]![cmbSearchYear] Is Null) AND
(sTblActivityMaster.actTargetMonth=[Forms]![frmActivityList]![cmbSearchMonth] Or [Forms]![frmActivityList]![cmbSearchMonth] Is Null) AND
(sTblActivityMaster.actTargetWeek=[Forms]![frmActivityList]![cmbSearchWeek] Or [Forms]![frmActivityList]![cmbSearchWeek] Is Null) AND
(sTblActivityMaster.actCategory=[Forms]![frmActivityList]![cmbSearchCategory] Or [Forms]![frmActivityList]![cmbSearchCategory] Is Null) AND
(sTblActivityMaster.actCustOwner=[Forms]![frmActivityList]![cmbSearchOwner] Or [Forms]![frmActivityList]![cmbSearchOwner] Is Null) AND
(sTblActivityMaster.actRegion=[Forms]![frmActivityList]![cmbSearchCoordinatorRegion] Or [Forms]![frmActivityList]![cmbSearchCoordinatorRegion] Is Null) AND
(sTblActivityMaster.actMode=[Forms]![frmActivityList]![cmbSearchMode] Or [Forms]![frmActivityList]![cmbSearchMode] Is Null) AND
(sTblActivityMaster.actCreator=[Forms]![frmActivityList]![cmbSearchCoordinator] Or [Forms]![frmActivityList]![cmbSearchCoordinator] Is Null) AND
(sTblActivityMaster.actSection=[Forms]![frmActivityList]![cmbSearchSection] Or [Forms]![frmActivityList]![cmbSearchSection] Is Null) AND
(sTblActivityMaster.actProject=[Forms]![frmActivityList]![cmbSearchProject] Or [Forms]![frmActivityList]![cmbSearchProject] Is Null) AND
(sTblActivityMaster.actRevenueBookStatus=[Forms]![frmActivityList]![cmbSearchRevenue] Or [Forms]![frmActivityList]![cmbSearchRevenue] Is Null) AND
(sTblActivityMaster.actVerificationStatus=[Forms]![frmActivityList]![cmbSearchVerification] Or [Forms]![frmActivityList]![cmbSearchVerification] Is Null) AND
(sTblActivityMaster.actAcceptanceStatus=[Forms]![frmActivityList]![cmbSearchAcceptance] Or [Forms]![frmActivityList]![cmbSearchAcceptance] Is Null) AND
(sTblActivityMaster.actKPIStatus=[Forms]![frmActivityList]![cmbSearchKPI] Or [Forms]![frmActivityList]![cmbSearchKPI] Is Null) AND
(sTblActivityMaster.actInvoicingStatus=[Forms]![frmActivityList]![cmbSearchInvoicing] Or [Forms]![frmActivityList]![cmbSearchInvoicing] Is Null))
GROUP BY sTblLineItemMaster.lineItemStatus;
Can anyone please check if syntax is OK or if any other modification is needed.
Thanks
I like to do it this way:
sTblActivityMaster.actTargetYear=Nz([Forms]![frmActivityList]![cmbSearchYear],sTblActivityMaster.actTargetYear)
That makes it simpler by just having a series of AND without mixing OR and having to deal with the parenthesis mess.
As I am using ComboBoxes to get the search criteria and the empty combo boxes return a "" and not NULl, so the query wasn't returning the results.
So what I did was that when search button is pressed to run the query, I set all empty or "" comboboxes to NULL in VBA. That did the trick and results appeared.
In my Ruby on Rails app I'm using blazer(https://github.com/ankane/blazer) and I have the following sql query:
SELECT *
FROM survey_results sr
LEFT JOIN clients c ON c.id = sr.client_id
WHERE sr.client_id = {client_id}
This query works really well. But I need to add conditional logic to check if client_id variable is present. If yes then I filter by this variable, if not then I not launching this where clause. How can I do it in PostgreSQL?
Check if its null OR your condition like this:
WHERE {client_id} IS NULL OR sr.client_id = {client_id}
The WHERE clause evaluate as follow: If the variable is empty, then the WHERE clause evaluate to true, and therefore - no filter. If not, it continue to the next condition of the OR
If anyone faced with the psql operator does not exist: bigint = bytea issue, here is my workaround:
WHERE ({client_id} < 0 AND sr.client_id > {client_id}) OR sr.client_id = {client_id}
Please consider that, client_id generally cannot be negative so you can use that information for eleminating the operation cast issue.
My solution:
I use spring data jpa, native query.
Here is my repository interface signature.
#Query(... where (case when 0 in :itemIds then true else i.id in :itemIds end) ...)
List<Item> getItems(#Param("itemIds) List<Long> itemIds)
Prior calling this method, I check if itemIds is null. If yes, I set value to 0L:
if(itemIds == null) {
itemIds = new ArrayList<Long>();
itemIds.add(0L);
}
itemRepo.getItems(itemIds);
My IDs starts from 1 so there is no case when ID = 0.
I am trying to update sum of particular amount from one table as a value in another table, but getting problem when the particular id does not exist in another table :
the hql i have looks like beow:
var session = SessionManager.GetCurrentSession();
var queryString = string.Empty;
queryString += #"update CLiner cl set cl.UnbilledDue =
(select sum(cu.UnbilledAmount) as UnbilledAmount from CUnbilled cu
where cu.AccountNo = cl.AccountNo and cu.FileDate = :fileDate)
where cl.FileDate = :fileDate ";
var query = session.CreateQuery(queryString);
query.SetDateTime("fileDate", new DateTime(2014, 10, 7));
query.ExecuteUpdate();
but its giving exception when the particular account no does not exist in child table and the sum is returned as zero.
i tried changing the subquery select to
select isnull(sum(cu.UnbilledAmount),0) as UnbilledAmount
which as expected is not supported by nhibernate hql. so there a way i can convert null to zero in hql select statement...
Try this:
coalesce(sum(cu.UnbilledAccount), 0)
Reason:
Normally when you wirte a SQL query to prevent NULL you must apply an ISNULL function over the SELECT, in this way:
ISNULL(SELECT sum(...., 0) or COALESCE(SELECT sum(...., 0) (return the same result but COALESCE can applied with more parameter, ISNULL has only two parameters)
but in HQL isn't possible, so you can use COALESCE function to prevent that behaviour.
I have a difficulty because when comparing two fields in a subquery, although the fields are identical i.e. they both have NULL values, the comparison returns a FALSE result
Therfore NULL = NULL is returning FALSE
Now I know that NULLs are supposed to be compared with the IS operator, however when I compare two fields how am I supposed to know they contain a null? I need to compare two fields for identical data both if the values are NULL or not.
Consider this SQL:
SELECT
*
FROM
fts.fts_customers_data_50360001
WHERE
fts.fts_customers_data_50360001.record_type = 15
AND
fts.fts_customers_data_50360001.mid = 103650360001
AND NOT EXISTS
(
SELECT
fts.temp_fees_50360001.record_type
FROM
fts.temp_fees_50360001
WHERE
fts.temp_fees_50360001.record_type = fts.fts_customers_data_50360001.record_type
AND
fts.temp_fees_50360001.merch_id = fts.fts_customers_data_50360001.mid
AND
fts.temp_fees_50360001.fee_curr = fts.fts_customers_data_50360001.currency
AND
fts.temp_fees_50360001.card_scheme = fts.fts_customers_data_50360001.card_scheme
AND
fts.temp_fees_50360001.tran_type = fts.fts_customers_data_50360001.fee_type
AND
fts.temp_fees_50360001.area = fts.fts_customers_data_50360001.region
AND
fts.temp_fees_50360001.srvc_type = fts.fts_customers_data_50360001.card_type
);
In the query above,
fts.temp_fees_50360001.card_scheme = fts.fts_customers_data_50360001.card_scheme
both have NULL values inside but the comparison returns false .. too bad
ANY IDEAS WOULD BE MUCH APPRECIATED
As the others have pointed out, NULL cannot be compared with NULL.
In Postgres you can shorten your expressions by using the operator IS DISTINCT FROM which is a null-safe replacement for <>. In your case you'd need to use IS NOT DISTINCT FROM to compare for equality (looks a bit the wrong way round but unfortunately there is no corresponding IS EQUAL TO defined in the SQL standard).
From the manual:
Ordinary comparison operators yield null (signifying "unknown"), not true or false, when either input is null. For example, 7 = NULL yields null, as does 7 <> NULL. When this behavior is not suitable, use the IS [ NOT ] DISTINCT FROM constructs:
So, instead of
(fts.temp_fees_50360001.record_type = fts.fts_customers_data_50360001.record_type
OR (fts.temp_fees_50360001.record_type IS NULL
AND fts.fts_customers_data_50360001.record_type IS NULL)
)
you can use:
(fts.temp_fees_50360001.record_type IS NOT DISTINCT FROM fts.fts_customers_data_50360001.record_type)
to handle NULL values automatically. The condition looks a bit strange if you want to compare for equality but it still is quite short.
First of all, use aliases for your tables, your query will be MUCH more readable:
select *
from fts.fts_customers_data_50360001 as d
where
d.record_type = 15 and
d.mid = 103650360001 and
not exists
(
select *
from fts.temp_fees_50360001 as f
where
f.record_type = d.record_type and
f.merch_id = d.mid and
f.fee_curr = d.currency and
f.card_scheme = d.card_scheme and
f.tran_type = d.fee_type and
f.area = d.region and
f.srvc_type = d.card_type
)
As for your question, there's several ways to do this, for example, you can use syntax like this:
...
(
f.card_scheme is null and d.card_scheme is null or
f.card_scheme = d.card_scheme
)
...
Or use coalesce with some value that couldn't be stored in your column:
...
coalesce(f.card_scheme, -1) = coalesce(d.card_scheme, -1)
...
Recently I also like using exists with intersect for this type of comparisons:
...
exists (select f.card_scheme, f.tran_type intersect select d.card_scheme, d.tran_type)
...
Just a side note - you have to be careful when writing queries like this and check query plans to be sure your indexes are used.
In SQL, null is never equal to null. The only way to get a true result for a comparison with null is via the special tests:
IS NULL
IS NOT NULL
In your case, you must cater specifically for the "two nulls" case being considered equal:
AND (fts.temp_fees_50360001.card_scheme = fts.fts_customers_data_50360001.card_scheme
OR (fts.temp_fees_50360001.card_scheme IS NULL
AND fts.fts_customers_data_50360001.card_scheme IS NULL)
)
There's no getting around dealing with it (although there are a few variations).
The following inner SELECT works (but I give no guarantee regarding performance):
SELECT
fts.temp_fees_50360001.record_type
FROM
fts.temp_fees_50360001
WHERE
(fts.temp_fees_50360001.record_type = fts.fts_customers_data_50360001.record_type
OR (fts.temp_fees_50360001.record_type IS NULL AND fts.fts_customers_data_50360001.record_type IS NULL))
AND
(fts.temp_fees_50360001.merch_id = fts.fts_customers_data_50360001.mid
OR (fts.temp_fees_50360001.merch_id IS NULL AND fts.fts_customers_data_50360001.mid IS NULL))
AND
(fts.temp_fees_50360001.fee_curr = fts.fts_customers_data_50360001.currency
OR (fts.temp_fees_50360001.fee_curr IS NULL AND fts.fts_customers_data_50360001.currency IS NULL))
AND
(fts.temp_fees_50360001.card_scheme = fts.fts_customers_data_50360001.card_scheme
OR (fts.temp_fees_50360001.card_scheme IS NULL AND fts.fts_customers_data_50360001.card_scheme IS NULL))
AND
(fts.temp_fees_50360001.tran_type = fts.fts_customers_data_50360001.fee_type
OR (fts.temp_fees_50360001.tran_type IS NULL AND fts.fts_customers_data_50360001.fee_type IS NULL))
AND
(fts.temp_fees_50360001.area = fts.fts_customers_data_50360001.region
OR (fts.temp_fees_50360001.area IS NULL AND fts.fts_customers_data_50360001.region IS NULL))
AND
(fts.temp_fees_50360001.srvc_type = fts.fts_customers_data_50360001.card_type
OR (fts.temp_fees_50360001.srvc_type IS NULL AND fts.fts_customers_data_50360001.card_type))