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

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.

Related

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

NULL behavior with Comoperator like ALL in Oracle SQL

SELECT * FROM hr.NullValueCheck
ID Name
1 abc
2 abc
3 bcd
4 cde
https://oracle-base.com/articles/misc/all-any-some-comparison-conditions-in-sql
Query 1 :
SELECT *
FROM hr.NullValueCheck
where id > All (SELECT NULL FROM DUAL )
Nothing is coming.
But for below quesry. All records are coming while subquesry is returning is NULL same as like above query (SELECT NULL FROM DUAL )
Query 2:
SELECT *
FROM hr.NullValueCheck
where id > All (SELECT id from hr.NullValueCheck where id = 5)
Please explain me why Query 1 is returning No Records but Query 2 is returning all records.
As per my knowledge Query 1 should also return all records.
NULL is different from an empty set.
The first example is saying: "select all rows where the id is greater than all values of NULL". Or more simply, "where id is greater than 'NULL'`.
In SQL, 'NULL' generally has the semantics of "not known". If you don't know the value, then you don't know if a given id is larger. Hence, no rows are returned.
In the second example, instead has an empty set for comparison. An empty set is not NULL. Obviously, any number is greater than all numbers in an empty set. Hence, all rows are returned.

google bigquery selecting rows with multiple values in repeating field

Lets say I have a schema:
key STRING NULLABLE
values INTEGER REPEATED
Now, please note that second column is a repeated field of integers,
Lets say the data is something like:
key:'abc'
values: 1 2 3 (3 separate values, same for below values)
key:'def'
values: 1 2 5
key:'ghi'
values: 1 6 9
And here I wish to find out keys which has values 1 and 2 ? Expecting 'abc' and 'def' as result set.
Looking for a query for this. I want an 'and' ('in' does not work here). I need those both values to be present for any key to return as result.
SELECT
key,
SUM(values = 1 or values = 2) WITHIN RECORD AS check
FROM yourtable
HAVING check = 2

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

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

SQLite - sql question 101

I would do something like this:
select * from cars_table where body not equal to null.
select * from cars_table where values not equal to null And id = "3"
I know the syntax for 'not equal' is <>, but I get an empty results.
For the second part, I want to get a result set where it only returns the columns that have a value. So, if the value is null, then don't include that column.
Thanks
You cannot use equality operators for nulls, you must use is null and is not null.
So your first query would be:
select * from cars_table where body is not null;
There's no easy way to do your second operation (excluding columns that are null). I'm assuming you mean don't display the column if all rows have NULL for that column, since it would be even harder to do this on a row-by-row basis. A select statement expects a list of columns to show, and will faithfully show them whether they are null or not.
The only possibility that springs to mind is a series (one per column) of selects with grouping to determine if the column only has nulls, then dynamically construct a new query with only columns that didn't meet that criteria.
But that's incredibly messy and not at all suited to SQL - perhaps if you could tell us the rationale behind the request, there may be another solution.
Comparing to NULL is not done with <>, but with is null and is not null :
select *
from cars_table
where body is not null
And :
select *
from cars_table
where values is not null
and id = '3'
NULL is not a normal value, and has to be dealt with differently than standard values.
In SQL, NULL is an unknown value. It is neither equal-to nor not-equal-to any other value. All comparison operators (=, <>, <, >, etc.) return false if either of the values being compared is NULL. (I don't know how old you are, so I can't say that you're 24, but I also can't say that you're not 24.)
As already mentioned, you have to use IS NULL or IS NOT NULL instead when testing for NULL values.
Thank you all for your answers.
Well, I have a table with a bunch of columns.
And I am going to search a particular car, and get the values for that car...
car | manual | automatic | sedan | power brakes | jet engine
honda | true | false | false | true | false
mazda | false | true | false | true | true
So, I want to do ->
select * from car_table where car='mazda' and values not equal to false
So then I just iterate over the cursor result and fill up the table with the appropriate columns and values. Where values are columns. I guess I replace values with * for columns
I know I could do it programmatically, but was thinking I could do this just by sql