Below is my where condition in query
WHERE ResetID= CASE WHEN NOT EXISTS (select 1 FROM ops.table where LabelItemId=#Item) THEN NULL
WHEN #LastIssued < #LastDispatched THEN 1
WHEN #LastIssued >= #LastDispatched THEN 2
END
If case statement returns null the where condition will not work.. Any alternate solution for this condition without case
If you want to have NULL = NULL, you need to handle it with IS NULL. At a guess, what you want is this:
WHERE (ResetID = CASE WHEN NOT EXISTS (select 1 FROM ops.table where LabelItemId=#Item) THEN NULL
WHEN #LastIssued < #LastDispatched THEN 1
WHEN #LastIssued >= #LastDispatched THEN 2
END
OR (ResetID IS NULL AND NOT EXISTS (select 1 FROM ops.table where LabelItemId=#Item)))
Edit note that this does require 2 scans of the table ops.table; there may be a better way to do this, but difficult to suggest when we only have a snippet.
I am trying to run this sum query to produce two columns of Turnover one for TY and another for LY. However, I want any transactions flagged as Cancelled to appear as negatives:
select [Location],BR.BranchName,
sum(case when (FX.TransactionDateKey between '20171101' and '20181031')
and ([Action] = 'Cancelled')
then FX.CustomerValue*-1 else [CustomerValue] end) as [CustmVal TY],
sum(case when (FX.TransactionDateKey between '20161101' and '20171031')
and ([Action] = 'Cancelled')
then FX.CustomerValue*-1 else FX.CustomerValue*1 end) AS [CustmVal LY]
from [dbo].[FRX_Transactions] FX
inner join DWX_Branch BR on BR.BranchID=FX.[Location]
where FX.TransactionDateKey between '20161101' and '20181031' and BR.BusinessDivision = 'Retail'
and FX.[Action] in ('Trade','cancelled') and FX.Reason in ('Public','BBG','Overridesupplyrate') and FX.Operation in ('Add','Del')
group by FX.[Location],BR.BranchName, BR.BranchOpenDate,BR.BranchCloseDate,BR.ActiveStatus
order by BR.BranchName
However, when I run it I get similar data in both columns - it seems to ignore the date conditions.
Please, what am I doing wrong? Is this case-when-statement with TWO conditions written wrong?
Any help would be HUGELY appreciated. Massive thanks!
I think what you actually want for your CASE expression is:
CASE WHEN FX.TransactionDateKey BETWEEN '20171101' AND '20181031' THEN [CustomerValue] *
CASE WHEN [Action] = 'Cancelled' THEN -1 ELSE 1 END
END
The following query
SELECT jsonb_build_array(jsonb_build_object('use', 'Home'),
CASE WHEN 1 = 2 THEN jsonb_build_object('use', 'Work')
END)
produces
[{"use":"Home"},null]
When I actually want
[{"use":"Home"}]
How do I go about doing this? json_strip_nulls() does not work for me.
By using a PostgreSQL array like that:
SELECT array_to_json(array_remove(ARRAY[jsonb_build_object('use', 'Home'),
CASE WHEN 1 = 2 THEN jsonb_build_object('use', 'Work') END], null))
which does produce:
[{"use": "Home"}]
while, to be sure:
SELECT array_to_json(array_remove(ARRAY[jsonb_build_object('use', 'Home'),
CASE WHEN 1 = 2 THEN jsonb_build_object('use', 'Work') END,
jsonb_build_object('real_use', 'NotHome')], null))
does produce:
[{"use": "Home"},{"real_use": "NotHome"}]
Creating a custom function seems to be the simplest way.
create or replace function jsonb_build_array_without_nulls(variadic anyarray)
returns jsonb language sql immutable as $$
select jsonb_agg(elem)
from unnest($1) as elem
where elem is not null
$$;
select
jsonb_build_array_without_nulls(
jsonb_build_object('use', 'home'),
case when 1 = 2 then jsonb_build_object('use', 'work') end
)
jsonb_build_array_without_nulls
---------------------------------
[{"use": "home"}]
(1 row)
I'm assuming this query is generaetd dynamically, somehow. If you're in control of the SQL generation, you can also use ARRAY_AGG(...) FILTER(...) instead, which, depending on your real world query, might be more convenient than using all the different array conversion functions suggested by Patrick.
SELECT (
SELECT json_agg(v) FILTER (WHERE v IS NOT NULL)
FROM (
VALUES
(jsonb_build_object('use', 'Home')),
(CASE WHEN 1 = 2 THEN jsonb_build_object('use', 'Work') END)
) t (v)
)
Or also:
SELECT (
SELECT json_agg(v)
FROM (
VALUES
(jsonb_build_object('use', 'Home')),
(CASE WHEN 1 = 2 THEN jsonb_build_object('use', 'Work') END)
) t (v)
WHERE v IS NOT NULL
)
One other way that this can be handled is the following:
SELECT jsonb_build_array(
jsonb_build_object('use', 'Home'),
CASE
WHEN 1 = 2 THEN jsonb_build_object('use', 'Work')
ELSE '"null"'
END
) - 'null'
(Unfortunately it's not really possible to do much with null by itself in postgres -- or most other DBs)
In the case above, '"null"' could be replaced with just about any unique string that would not be mistaken for live data in the array. I would not use numbers since - 0 would actually try to remove the first item from the array, rather than a number within the array. But you could probably use '"0"' and remove using something like - '0' if you wanted.
For those not using CASE, a COALESCE could be used to convert nulls into the desired string (alas, no NVL, IFNULL or ISNULL in postgres, but at least COALESCE is portable)
It's me again. I would just like to ask your opinion on how shall I modify my SELECT statament query. So basically I created a user defined field called "Force Schedule2", if this checkbox is tick (equals 1), then even if the item is on schedule 1, it will display on schedule 2. How shall I add it on my existing case statement?
my current SELECT statement is:
SELECT
CASE
WHEN
WorkOrder.DateCreated <
(
CASE
WHEN
(DATEPART(dw, dbo.ToBeScheduled_InProgress.Start) = 2)
THEN
(ToBeScheduled_InProgress.Start + 0.625) - 3
ELSE
(ToBeScheduled_InProgress.Start + 0.625) - 1
END
)
THEN
1
ELSE
2
END
AS ScheduleTime
and my user defined field for "Force Schedule2 is:
dbo.AdditionalInfo.UserDefined3 AS ForceSched
It's not obvious what you're trying to do here with your date operation. You could do something simple like this:
CASE
WHEN dbo.AdditionalInfo.UserDefined3 = 1
THEN 2
WHEN WorkOrder.DateCreated < (CASE WHEN (DATEPART(dw, dbo.ToBeScheduled_InProgress.Start) = 2) THEN (ToBeScheduled_InProgress.Start + 0.625) - 3 ELSE (ToBeScheduled_InProgress.Start + 0.625) - 1 END)
THEN 1
ELSE 2
END AS ScheduleTime
Basically it hits the condition and exits the statement. If you flipped it so your date operation was first it would return 1, so you still need to think about the order you're entering things into case statement.
Crystal Reports has the Select Records Expert which allows you to use variables to build WHERE conditions. Is there something similar to the select records experts in SSRS?
If the variable #Restriction below is equal to one, the column name CFE_EDI.ETAT = 'ENV' or CFE_EDI.ETAT = 'OUV'. I tried to mimic the functionality with a SQL query but due to AND and OR precedence, the WHERE condition is not functioning properly.
You can handle this within the SQL of your SSRS Dataset where clause buy using case statements that return a 1 or a 0 depending on whether or not the criteria is met:
where case when #Restriction = '1'
then case when CFE_EDI.ETAT in('ENV','OUV') then 1 else 0 end
when #Restriction = '0'
then case when CFE_EDI.ETAT not in('ENV','OUV') then 1 else 0 end
else case when CFE_EDI.ETAT <> '' then 1 else 0 end
end = 1