I have a SQL table of the format (INTEGER, json_array(INTEGER)).
I need to return results from the table that have two boolean columns. One is set to true iff a 1 appears in the json_array, and the other true iff a two appears in the array. Obviously there is not mutual exclusion.
For example, if the data were this:
-------------------------------
| ID | VALUES |
-------------------------------
| 12 | [1, 4, 6, 11] |
_______________________________
| 74 | [0, 1, 2, 5] |
-------------------------------
I would hope to get back:
-------------------------------
| ID | HAS1 | HAS2 |
-------------------------------
| 12 | true | false |
_______________________________
| 74 | true | true |
-------------------------------
I have managed to extract the json data out of the values column using json_each, but am unsure how to proceed.
If I recall correctly, SQLite max aggregate function supports boolean, therefore you can simply group by your data:
select
t1.id,
max(case json_each.value when 1 then true else false end) as has1,
max(case json_each.value when 2 then true else false end) as has2
from
yourtable t1,
json_each(t1.values)
group by
t1.id
Related
I got table with 2 IDs. I'm using parametrs as filters.
eg.
| ID | DIFF_ID |
| ----| ------ |
| 1 | null |
| 2 | 3 |
| 3 | null |
...
As you can see Diff_ID has the same int as ID and I need to use DIFF_ID to show other values in table by using parametrs eg.
case when $P{aba} is true then DIFF_ID (...) = ID(?) end
if the paramter 'aba' is true then use DIFF ID as base but show up as ID.
I'm expecting DIFF_ID appear in ID column if the parameter is true.
Think I have a table with two fields: ID and State. State value (that is boolean) can be 0 or 1. ID isn't unique so the table looks like this:
ID | State |
-----------------
1 | true |
-----------------
1 | false |
-----------------
2 | false |
-----------------
3 | true |
-----------------
1 | true |
Now, I want to count every rows group by ID field and have State as two different columns in resultset. So it should look like this:
ID | TrueState | FalseState |
------------------------------------
1 | 2 | 1 |
------------------------------------
2 | 0 | 1 |
------------------------------------
3 | 1 | 0 |
How to do that?
This is a pivot query, which mysql doesn't support. The workarounds get ugly fast, but since you're only going to be generating two new columns, it won't be horribly ugly, just mildly unpleasant:
SELECT SUM(State = True) AS TrueState, SUM(State = False) AS FalseState,
SUM(State is NULL) AS FileNotFoundState
...
Basically state = true will evaluate to boolean true/false, which MySQL will type-cast to an integer 0 or 1, which can them be SUM()med up.
I have a table called users and another table called relationship which has a column of two userId. I want to find an output if the user has a relationship with another user, (for this example, it is a user with userId = 2). If they do have a relationship, I want my output to be true, else false. I am having some trouble and wondering if anyone can help me.
I tried something like this:
SELECT DISTINCT
userid,
CASE WHEN user_one_id = '2' THEN true ELSE false END AS has_user_two
FROM users
LEFT JOIN relationship ON userid = user_two_id;
However, I am getting duplicates...
My Query output that is wrong
userid | has_user_two|
---------------------+
1 | true |
1 | false |
2 | false |
3 | false |
4 | true |
5 | true |
Users
userid
-----------
1
2
3
4
5
relationship
user_one_id| user_two_id
-----------+-------------
1 | 1
3 | 1
3 | 2
My output should look something like...
userid | has_user_two|
---------------------+
1 | true |
2 | false |
3 | false |
4 | false |
5 | false |
Any help would be appreciated.
Use EXISTS:
SELECT u.userid,
(EXISTS (SELECT 1
FROM relationship r
WHERE r.user_one_id = 2 AND r.user_two_id = u.userid
)
) as has_user_two
FROM users u;
Note that you don't need a CASE expression in Postgres. A boolean expression can be -- well -- a boolean column.
In Snowflake, what is the difference between x = NULL and x IS NULL in a condition expression? It seems empirically that x IS NULL is what I want when I want to find rows where some column is blank. I ask because x = NULL is treated as valid syntax and I am curious whether there's a different application for this expression.
what is the difference between x = NULL and x IS NULL
In Snowflake just like in other RDBMS, Nothing is equal to NULL (even NULL itself), so a condition x = NULL (which is valid SQL syntax) will always evaluate as false (well, actually, it evaluates to NULL in most RDBMS, which is not true). Note that this is also true for non-equality comparisons: that is NULL <> NULL is false too.
The typical way to check if a variable is NULL is to use the x IS NULL construct, which evaluate as true if x is NULL. You can use x IS NOT NULL too. This syntax is reserved for NULL, so something like x IS y is a syntax error.
Here is a small demo:
select
case when 1 = null then 1 else 0 end 1_equal_null,
case when 1 <> null then 1 else 0 end 1_not_equal_null,
case when null is null then 1 else 0 end null_is_null,
case when 1 is not null then 1 else 0 end 1_is_not_null
1_equal_null | 1_not_equal_null | null_is_null | 1_is_not_null
-----------: | ---------------: | -----------: | ------------:
0 | 0 | 1 | 1
This particular case is well-described in Snowflake's documentation:
EQUAL_NULL
IS [ NOT ] DISTINCT FROM
Compares whether two expressions are equal. The function is NULL-safe, meaning it treats NULLs as known values for comparing equality. Note that this is different from the EQUAL comparison operator (=), which treats NULLs as unknown values.
+------+------+--------------------------------+------------------------------------------+----------------------------+--------------------------------------+
| X1_I | X2_I | X1.I IS NOT DISTINCT FROM X2.I | SELECT IF X1.I IS NOT DISTINCT FROM X2.I | X1.I IS DISTINCT FROM X2.I | SELECT IF X1.I IS DISTINCT FROM X2.I |
|------+------+--------------------------------+------------------------------------------+----------------------------+--------------------------------------|
| 1 | 1 | True | Selected | False | Not |
| 1 | 2 | False | Not | True | Selected |
| 1 | NULL | False | Not | True | Selected |
| 2 | 1 | False | Not | True | Selected |
| 2 | 2 | True | Selected | False | Not |
| 2 | NULL | False | Not | True | Selected |
| NULL | 1 | False | Not | True | Selected |
| NULL | 2 | False | Not | True | Selected |
| NULL | NULL | True | Selected | False | Not |
+------+------+--------------------------------+------------------------------------------+----------------------------+--------------------------------------+
Like most SQL languages, comparing NULL = NULL does not return TRUE. In SnowFlake, it returns NULL, as does ANY comparison to a NULL value. The reason for this is tied to the convoluted history of SQL, and it has been well argued whether or not this is a good feature or not. Regardless, it's what we have.
As such, when you are comparing two values that may be NULL here are a few different solutions you can typically use.
-- NVL will return the second value if the first value is NULL
-- So if both of your values are NULL, then an NVL around each of them will
-- return a value so that they are both equal.
-- This only works if you know that your values will never be equal to -1 for example
SELECT ...
WHERE NVL(x, -1) = NVL(y, -1)
-- A little messier, especially among more complicated filters,
-- but guaranteed to work regardless of values
SELECT ...
WHERE x = y OR (x is null and y is null)
-- My new favorite which works in SnowFlake (thanks to #waldente)
SELECT x IS NOT DISTINCT FROM y;
-- For most SQL languages, this is a neat way to take advantage of how
-- INTERSECT compares values which does treat NULLs as equal
SELECT ...
WHERE exists (select x intersect select y)
Lets say I have this table :
--------------------------------------------------------------------------
| id | allowed_to_play_pokemon_go_after | last_played_pokemon_go | name |
--------------------------------------------------------------------------
| 1 | 20-JUL-16 | 19-JUL-16 | Jhon |
--------------------------------------------------------------------------
| 2 | 19-JUL-16 | 21-JUL-16 | Bill |
--------------------------------------------------------------------------
Now I want to make a select like that :
SELECT name, (last_played_pokemon_go > allowed_to_play_pokemon_go_after) as must_punish
FROM myTable;
Where 'must_punish' has to bo a boolean (1/0).
You can use case:
SELECT name,
(case when last_played_pokemon_go > allowed_to_play_pokemon_go_after then 1 else 0
end) as must_punish
FROM myTable;
Oracle (the database) doesn't have a boolean data type. A number should be fine.