Is there any way to avoid the top rows with NULL values that has one column value (One of the table Business Key) in Cognos report prompt values - sql

Is there any way to avoid the top rows with NULL values that has one column value (One of the table Business Key) in Cognos report prompt values
When the user does not select any value in the value prompt (Cognos Report Portal) and clicks FINISH button, the results include the column (One of the Business Keys with Value) that has other columns with NULL values.
Its no different as selecting the whole table.
`Select * From InsuranceTable`
Is there a way in Cognos to avoid the first top 10 rows when the user opts not to select anything in the value prompt
Business_Sub_Division Business_Division_Name Claim_Handling_Unit
1 NULL Construction NULL
2 NULL Binding Operations NULL
3 NULL E&S Casualty NULL
4 NULL Executive Assurance NULL
5 NULL Facultative Reinsurance NULL
6 NULL Healthcare NULL
7 NULL Professional Liability NULL
8 NULL Open Brokerage NULL
9 NULL Property NULL
10 NULL Special Risks NULL
11 Asset Protection Executive Assurance Canada - Claims
12 Captive Agents Property Executive Assurance
13 Excess Casualty Healthcare Europe - Claims
14 Financial Institutions E&S Casualty Executive Assurance

I'm a little bit confused as to what you want to achieve. However, if you simply want to filter out the rows that have NULL for column 1 and column 2 even if the user doesn't pick a value from the prompt then your filter could look something like this (assuming that you are selecting Claim_Handling_Unit in the value prompt):
(?param? IS MISSING AND [Businss_Sub_Division] IS NOT NULL AND [Claim_Handling_Unit] IS NOT NULL)
OR
(?param? IS NOT MISSING AND [Claim_Handling_Unit] = ?param?)
You have two bits of logic, one where nothing is selected and the other where a value is selected. If nothing is chosen then we only select rows where both of the other two values are not null. The key is that the two conditions don't overlap. Either ?param? is missing or it is not missing.
You can also accomplish the same logic using IF...THEN like this:
IF (?param? IS MISSING)
THEN ([Businss_Sub_Division] IS NOT NULL AND [Claim_Handling_Unit] IS NOT NULL)
ELSE ([Claim_Handling_Unit] = ?param?)
..or with a CASE statement
CASE
WHEN (?param? IS MISSING) THEN ([Businss_Sub_Division] IS NOT NULL AND [Claim_Handling_Unit] IS NOT NULL)
ELSE ([Claim_Handling_Unit] = ?param?)
END

Related

SQL - How to keep the last row if it meets a condition, but remove other rows beforehand which meet the same condition

I'm coming across an SQL issue which I could do with some advice on.
I have an example below showing actions taken between different energy suppliers for dispute cases. The action_time_start is when supplier 1 sends an action, and action_time_end is when supplier 2 sends a response.
The row number is not included in the main table but I have added it in here for visibility.
dispute_id
supplier_1_action_sent
supplier_2_action_response
action_time_start
action_time_end
row_num
847294
Proposal received (P)
Accept Proposal
2023-01-23
2023-01-23
4
847294
Agreement made (Y)
NULL
2023-01-24
NULL
3
847294
Agreement made (Y)
Close Dispute
2023-01-25
2023-02-03
1
847294
Proposal received (P)
NULL
2023-02-3
NULL
1
I need to:
Include columns 2 and 4 in the results.
Include column 1 in the results (the last row), where the result for action_time_end is null.
Remove column 3 from results, where action_time_end is null.
For the table overall, I need to remove any columns where action_time_end is null except for when it is the last row, for each dispute_id. I also need to keep all columns in the results where action_time_end is not null.
If the last row has a value in action_time_end which is not null then this needs to be kept in, and all rows before where it's null removed.
Any suggestions here?
I have tried a number of different solutions, including:
Using MAX(COALESCE(TO_DATE(action_time_end), DATE '9999-01-01')) and filtered out instances where the action_time_start < action_time_end and action_time_end != '9999-01-01'.
Including row_num and filtering where row_num = 1 and action_time_end is not null
Doing a complex CASE WHEN in the last where clause of the query
The issue is that I'm not sure how to keep in the last row but remove all the others when a certain condition is met.
We can use CASE expressions here:
SELECT
CASE WHEN action_time_end IS NULL THEN dispute_id END AS dispute_id,
supplier_1_action_sent,
CASE WHEN action_time_end IS NOT NULL THEN supplier_2_action_response END AS supplier_2_action_response,
action_time_start
FROM yourTable;

How to summarize table based on other column values?

There is a decision table as follows:
Id decision
1 NULL
1 NULL
1 yes
1 NULL
2 no
2 no
2 no
3 yes
3 no
3 yes
The result should return :
Id decision
1 Decision Pending
2 no
3 yes
So for each Id in the decision table:
If any of the decision value is NULL then it is to be set to
"decision pending". eg. id 1
If there is no NULL and any of the decision is yes then final
decision is set to be "yes". eg. id 3
If there is no NULL and all the decisions are no then final decision
is set to be as "no". eg. id 2
What should the azure sql query to get the above mentioned result?
P.S. I am new to SQL, so having trouble figuring this out.
SELECT
id,
CASE
WHEN COUNT(*) > COUNT(decision)
THEN 'pending'
ELSE MAX(decision)
END
AS decision
FROM
decision
GROUP BY
id
GROUP BY id ensures that you get one row per id.
COUNT(*) tells you how many rows there are for that id.
COUNT(decision) tell you how many of those rows have NOT NULL decisions.
COUNT(*) > COUNT(decision) is therfore TRUE if any of the decisions in the group are NULL.
MAX(decision) returns yes if there are any yes values in the group, and no only if there aren't any yes values in the group.
One way is to compare the count(*) and count(decision) which reveals if a row is null and use an inline if to succinctly know to use the aggregate
select id, Iif(Count(*)=Count(decision),Max(decision),'Decision Pending')
from Decision
group by id

how to add filter on output of sql query to get answer as boolean?

I have one table in which ids are mentioned and values for that id.
id | value
101 | admin
102 | user
103 | test
104 | admin
105 | basic
I get input as 2 ids. now, I have to check whether these two ids have equal value or not? This id field is foreign key in some other table.
so far I tried this:
select id, value from roles where id in('101', '104')
but I want output to be as - not equal or equal or return some value only if equal, empty output if not equal.
Note: values will be never be null
I am using oracle 12 and node.js.
Here is one way.
select case when min(value) = max(value) then 'equal' else 'not equal' end
as compare_values
from roles
where id in ('101', '104')
;
This works even if you pass three or more id's - it just tells you if ALL the values (for ALL the id's) are equal.
Note that if value can be null (you said that's not the case in your real-life data), the null values will be ignored; the query will return 'equal' if all non-null values are equal.

How to return NULL records on a query with a where clause?

I'm using SQL Server 2016 and I have a view setup for novice end users.
To start, let's say there is a table like the following:
id number
=========
1 2
2 4
3 6
4 NULL
5 12
If a user makes a query on the view such as, select * from view1 where number <> 12, the view is setup to return NULL values as -99 using coalesce(number,-99):
Result of 'select * from view1 where number <> 12':
id number
=========
1 2
2 4
3 6
4 -99
Is there anyway to have the view return NULL instead of -99 (or whatever value), without the end user having to include ... or where is null in their query?
I understand NULLs and why it behaves like this, but for convenience I'd rather these end users not have to do this.
No.
The best you can do is fix the result so it decodes -99 as NULL:
SELECT id, CASE WHEN number = -99 THEN NULL ELSE number END AS number
FROM view1
WHERE number != 12
which I believe defeats the purpose of not exposing NULL values to the end user, or approach the data by accounting NULL as a valid data, using OR number IS NULL in that matter.
Try this:
select * from view1 where number <> 12 or number is null

Sybase SQL CASE with CAST

I have a Sybase table (which I can't alter) that I am trying to get into a specific table format. The table contains three columns all which are string values, with an id (which is not unique), a "position" which is a number that represents a field name, and a field column that is the value. The table looks like:
id position field
100 0 John
100 1 Jane
100 2 25
100 3 50
101 0 Dave
101 3 30
Position 0 means "SalesRep1", Position 1 means "SR1Commission", Position 2 means "SalesRep2", and Position 3 means "SR2Commission".
I am trying to get a table that looks like following, with the Commission columns being decimals instead of strings:
id SalesRep1 SR1Commission SalesRep2 SR2Commisson
100 John 25 Jane 50
101 Dave 30 NULL NULL
I've gotten close using CASE, but I end up with only one value per row and not sure there's a way to do what I want. I also have problems with trying to get CAST included to change the commission values from strings to decimals. Here's what I have so far:
SELECT id
CASE "position" WHEN '0' THEN field END AS SalesRep1,
CASE "position" WHEN '1' THEN field END AS SalesRep2,
CASE "position" WHEN '2' THEN field END AS SR1Commission,
CASE "position" WHEN '3' THEN field END AS SR2Commission
FROM v_custom_field WHERE id = ?
This gives me the following result when querying for id 100:
id SalesRep1 SR1Commission SalesRep2 SR2Commission
100 John NULL NULL NULL
100 NULL 25 NULL NULL
100 NULL NULL Jane NULL
100 NULL NULL NULL 50
This is close, but I want to 'collapse' the rows down into one row based off of the id as well as cast the commission values to numbers. I tried adding in a CAST(field AS DECIMAL) I'm not sure if this is even the right direction to go, and was looking into PIVOT, but Sybase doesn't seem to support that.
This is known as an entity-attribute-value table. They're a pain to work with because they're one step removed from being relational data, but they're very common for user-defined fields in applications.
If you can't use PIVOT, you'll need to do something like this:
SELECT DISTINCT s.id,
f0.field AS SalesRep1,
CAST(f1.field AS DECIMAL(20,5)) AS SR1Commission,
f2.field AS SalesRep2,
CAST(f3.field AS DECIMAL(20,5)) AS SR2Commission
FROM UnnamedSalesTable s
LEFT JOIN UnnamedSalesTable f0
ON f0.id = s.id AND f0.position = 0
LEFT JOIN UnnamedSalesTable f1
ON f1.id = s.id AND f1.position = 1
LEFT JOIN UnnamedSalesTable f2
ON f2.id = s.id AND f2.position = 2
LEFT JOIN UnnamedSalesTable f3
ON f3.id = s.id AND f3.position = 3
It's not very fast because it's a ton of self-joins followed by a DISTINCT, but it does work.