Why is PostgreSQL picking up the wrong column data? [closed] - postgresql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
I'm doing this query and it's picking 'ar' 16, when I've specified to only get 'ar' equal to or less than 3?
SELECT ticket_id, status,targets, ar, create_date FROM tufin_sc_tickets WHERE create_date >= '2022-01-06' AND create_date <= '2022-14-06' AND targets <= '3' AND ar <= '3' AND (status = '0' OR status = '1' OR status = '2' OR status = '3' OR status = '4');
Any ideas why?

I think it's because ar type is text. you have to cast it first.
change
AND ar <= '3'
to
AND CAST (ar AS INTEGER) <= 3;

Related

Why is there a duplicate primary key error in my postgresql insert statement when there is no duplicate? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Note: After reading some comments, I updates my query to the following, which works. But perhaps there is a more concise solution.
This seems to do the trick...
insert into duplicates(contact_id_a, contact_id_b, ignore_duplicate, has_been_fetched, list_id, contact_a_name, contact_b_name)
SELECT 32753, 42260, false, false, 567, (select data->>'firstname' as ca_firstname from contacts where contact_id = 32753),
(select data->>'firstname' as cb_firstname from contacts where contact_id = 42260)
WHERE NOT EXISTS ( SELECT * FROM duplicates WHERE list_id = 567
AND ((contact_id_a = 32753 and contact_id_b = 42260) OR (contact_id_a = 42260 and contact_id_b = 32753) ));
Why is my postgresql query raising this duplicate primary key error when there is no duplicate?
ERROR: duplicate key value violates unique constraint "duplicates_pkey"
DETAIL: Key (contact_id_a, contact_id_b, list_id)=(32753, 42260, 567) already exists.
SQL state: 23505
The duplicates table is empty, so I would imagine the new row is unique.
Here is the query. It's pretty clear what I am doing, but in a nutshell, inserting a new row into the duplicates table containing name information from a second table called contacts only if there is not already a row in the duplicates table containing the specified contact_ids and list_id.
insert into duplicates(contact_id_a, contact_id_b, ignore_duplicate, has_been_fetched, list_id, contact_a_name, contact_b_name)
SELECT 32753, 42260, false, false, 567, ca.data->>'firstname', cb.data->>'firstname'
from contacts c
left join contacts ca on ca.list_id=567 and ca.contact_id = 32753
left join contacts cb on cb.list_id=567 and cb.contact_id = 42260
WHERE NOT EXISTS ( SELECT * FROM duplicates WHERE list_id = 567
AND ((contact_id_a = 32753 and contact_id_b = 42260) OR (contact_id_a = 42260 and contact_id_b = 32753) ));
Here is my primary key constraint...
Before doing anything try to dump you database so you can back to current state if any problem occurs, then try to REINDEX your database:
REINDEX DATABASE <YOUR-DBNAME>;
a vaccum may also be a good idea:
vacuum(full, analyse, verbose);
The select is returning more than one row with the same key combo.
You need to fix your select query so it returns exactly one row per key combo.
You may be able to get away with using distinct:
insert into duplicates (...)
select distinct ...

Case statement based on the result Postgresql

Hi so I have a question regarding case statements and why certain statements dont work.
(Case WHEN LENGTH(input)<=5 THEN text LIKE '%'
ELSE text = input)
When I did this, it didnt work and im still confused as to why.
However the solution to this problem was
(Case WHEN LENGTH(input)<=5 THEN text LIKE Concat('%',input,'%')
ELSE text = input END)
However My main question is below though I would like an answer to the above question and since they are identical I will leave it as one question.
I have a student who is still being reviewed for entering the school, however this school is german so i need to translate a few words for them since the system is in English
select s.fname, s.lastname,
(Case when s.status like 'APPROVED' then s.status = 'Genehmigt'
when s.status like 'PENDING' then s.status = 'Anstehend.'
end) as Status
from Student
My results are giving me False in the status field so obviously im doing something wrong...
And I know there are two types of case statement so am I using the recommended one?
The expression you have placed after then:
s.status = 'Genehmigt'
is not an assignment, it is a boolean expression, so it yields true or false.
s.status = is just redundant.
select
fname,
lastname,
case when status = 'APPROVED' then 'Genehmigt'
when status = 'PENDING' then 'Anstehend.'
end as status
from student
Note that like with a string without wildcards on the right side should be a simple comparison.

How do I check if a column is NULL using rust-postgres? [duplicate]

This question already has an answer here:
How to handle an optional value returned by a query using the postgres crate?
(1 answer)
Closed 5 years ago.
I am using the rust-postgres library and I want to do a SELECT and check if the first column of the first row is NULL or not.
This is how I get my data:
let result = connection.query(
r#"
SELECT structure::TEXT
FROM sentence
WHERE id = $1
"#,
&[&uuid]
);
let rows = result.expect("problem while getting sentence");
let row = rows
.iter()
.next() // there's only 1 result
.expect("0 results, expected one...");
The only simple way I found to figure it out is the following code:
match row.get_opt(0) {
Some(Ok(data)) => some data found,
Some(Err(_)) => the column is null,
None => out of bound column index
}
Unfortunately, it seems that Some(Err(_)) is the executed path for any kind of SQL/database error, and not only if the retrieved column is NULL.
Which condition should I use to check that the column is NULL ?
If all you need to know is whether the column is NULL, you could try changing your query to:
SELECT COUNT(1) FROM sentence WHERE id = $1 AND structure IS NOT NULL
with or without the NOT.
If you want to make the logic simpler so any error is an actual error, I'd consider changing the select value to something like:
COALESCE( structure::TEXT, ''::TEXT ) AS "structure"
so it should never be NULL. That should work as long as an empty string isn't a valid non-NULL value for that column.
Otherwise, I may have misunderstood your problem.

How to do a one-to-many join with conditions in posgreql

Forgive me, I don't know how to ask this question and google for an answer. May have already been answered elsewhere on Stack, let me know if it is.
I want to use postgresql to join one table, Table A, with Table B such that the values in one set of columns in Table A are joined and multiplied (one-to-many join) by the corresponding values in a set of columns in Table B, based on whether the values in the set of columns in Table B are within the range of the values in the set of columns in Table A.
Basically:
Where Start_A >= Start_B AND End_A <= End_B
Like so:
i think this can help you. But in your quest and result where you question "Basically: Where Start_A >= Start_B AND End_A <= End_B", I think this your mistake because in result i saw Start_A <= Start_B AND End_A >= End_B. And id write the query for you:
SELECT *
FROM a LEFT JOIN b ON startA <= startB
WHERE endA >= endB

Compare two CSV files with different column numbers on basis of a common column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Using powershell for the first time.
I have 2 csv files. Each have different number of columns, and have different number of entries and not in same sequence.
Here are some examples of the first couple lines
File1.csv
Name,Alias,Category
Comdty,CONTRACT FOR DIFFERENCE,Future
Comdty,Calendar Spread Option,Future
Corp,Bond,FixedIncome
Corp,EURO-DOLLAR,FixedIncome
Corp,FLOORSPREAD,FixedIncome
Corp,FRA,FixedIncome
File2.CSV
Alias,Category
EURO-DOLLAR,FixedIncome
Bond,FixedIncome
Preferred,Equity
I need to compare the files using PowerShell script on the basis of Alias column,and show the matching values together and showing blank where the value not exist for either file.
Something like :
Alias,Alias, Category ------------------------- MacthCase
___,CONTRACT FOR DIFFERENCE,Future ------------ false
___,Calendar Spread Option,Future ------------- false
Bond,Bond,FixedIncome ------------------------- true
EURO-DOLLAR,EURO-DOLLAR,FixedIncome ----------- true
___,FLOORSPREAD,FixedIncome ------------------- false
___,FRA,FixedIncome --------------------------- false
Preferred,___,Equity -------------------------- false
try this
#import files with select columns
$file1=import-csv C:\temp\file1.csv | select Alias,Category, #{N="file";E={"file1"}}
$file2=import-csv C:\temp\file2.csv | select Alias,Category, #{N="file";E={"file2"}}
#merge into one list object
$allfile=$file1
$allfile+=$file2
#group by Alias and build objects result
$allfile | group Alias | foreach{
if ($_.Count -ge 2)
{
[pscustomobject]#{Alias1=$_.Name;Alias2=$_.Name;Categorie=$_.Group.Category[0];MatchCase=$true}
}
else
{
if ($_.Group.file -eq "file1")
{
[pscustomobject]#{Alias1="____";Alias2=$_.Name;Categorie=$_.Group.Category;MatchCase=$false}
}
else
{
[pscustomobject]#{Alias1=$_.Name;Alias2="____";Categorie=$_.Group.Category;MatchCase=$false}
}
}
}