I have following table and would like to change to result a as follow.
The country code should be dynamically based on value of table A
How can this achieve with postgresql query?
Table A
ProductID CountryCode Active
A0001 MY true
A0001 SG true
A0002 MY true
A0002 SG false
A0003 MY false
A0003 SG false
Result A
ProductID MY SG
A0001 true true
A0002 true false
A0003 false false
Related
I am running a query where I am converting the output to a boolean, as below
jsonb_path_query(AnchorNode, '$.IsIndexStartDeferred')#>> '{}' )::boolean AS ISINDEXSTARTDEFERRED
The output is correctly shown as True or False, if I copy the result in a Notepad or equivalent editor, but on the dBeaver it shows as [] for False and [v] for True
Is there a way to "show" in dBeaver, the values as True or False?
Thank you
I have a data table with column Status and Extract Name and some other columns. I want to create a calculated field whose value will be true for each row if one row is true. To be clear, if there is a row where the Status is "In progress" and Extract Name is "A" I want the other rows for which this is not valid to be true (I mean that column Calculation should be true for each row), not just for that row. The picture shows that table where I was able to make only that row true.
This is the code:
IF [Status]="In Progress" AND [Extract Name]="A"
THEN TRUE
ELSE FALSE
END
How do I modify that code to get what I want, to make all rows true if there is one row that matches this condition?
use this
IF
{MAX(IF [Status]="In Progress" AND [Extract Name]="A"
THEN 1
ELSE 0
END)} >0 THEN TRUE ELSE FALSE END
If you want a demonstration, please add some sample data
You could use an Or, something like this.
IF [Status]="In Progress" OR [Status]="Completed" AND [Extract Name]="A"
THEN TRUE
ELSE FALSE
END
The OR operator would make all the rows True. The AND operator may be omitted.
I have a table called 'apple' and I wrote the following query:
select name,
count(name),
case when istasty is null then false else istasty end
from apple
group by name, istasty;
This the output:
I am trying to group the name and istasty flag using following condition:
When the corresponding name column has both true or false, then I return false. In the above image, tala has both true and false istasty column. However, I want to return false because it has atleast one false istasty column.
If after grouping all istasty column for a particular name column is true then return true; Similarly, if all istasty column is false, then return false for that particular name column.
I can achieve using the bool_and operator in postgresql by writing the query:
select name, count(name), bool_and(coalesce(istasty = true, false)) from apple group by name;
Is there any way I can modify my first query so that I add filter in having clause to achieve the same result as I got using bool_and operator in second query? Or is there any other possible way?
Please note I am not using bool_and operator. I appreciate your time. Thank you.
An alternative to using the bool_and operator would be regular conditional aggregation:
SELECT
name,
COUNT(*) AS count,
CASE WHEN SUM(CASE WHEN !istasty THEN 1 ELSE 0 END) > 0
THEN FALSE ELSE TRUE END AS result
FROM apple
GROUP BY name;
I am not sure what you have in mind when you suggest using the HAVING clause. By moving the conditional check to the HAVING clause you may exclude/include specific groups in the query matching the condition.
Your logic (return true only if all values are true) equates to getting the min() boolean value:
select
name,
min(istasty::varchar(5))::boolean
from (select name, case when istasty is null then false else istasty end istasty
from apple
group by name, istasty) x
group by name
Note that postgres doesn't support aggregating boolean values, so you have to cast to character then back to boolean to use min().
I have a Postgres table that looks like this:
Category Unit Default Unit
---------------------------------------
Currency USD True
Currency EURO False
Currency AUS False
Length Kilometer True
Length Mile False
Length Foot False
Length Inch False
Mass Kilogram True
I want to set a table constraint so that there can be only one 'Default Unit' per Category.
Can this be done using Group By in a constraint, perhaps?
You can create a unique partial index:
create unique index idx_table(category, default_unit) on table(category, default_unit)
where default_unit;
select c.status from Table c where c.id = 13513 and status != false;
true
true
But not get null value. I want to get this like value and haw to get?
true
true
null
I don't have much to go on with the context you gave, but try this:
select c.status from Table c where c.id = 13513 and (status != false or status is null);
If that doesn't work, please post your table schema and, preferably, a data representation that contains the "null" you're looking for.