Why The Query Against HashKey returns no records - sql

I am working on a new sql table. The table has a column [varbinary(8000)], where we are storing hash of a certain text. Now, I am trying to retrieve the same record back by using a where clause against the hashkey, but that yields zero records.
I have added a similar query here: http://sqlfiddle.com/#!18/be996/11

Try without the single quotes, like this
SELECT id, description
FROM ForgeRock
where id = 0x94EE059335E587E501CC4BF90613E0814F00A7B08BC7C648FD865A2AF6A22CC2
and you will get the expected result.

Related

Handling Json data in snowflake

enter image description here
I have a table which contains Json file data in each row which gets updated into my snowflake table every weak. I am extracting values from the Json files into another table. When the data is loaded in Json format there are multiple entries of the same ID. So, when I extract values from Json to a table there are duplicate rows. How do I tackle them in order to get the distinct rows only. My select query look something like this:
select
json_data:data[0].attributes."Additional Invoice?":: string as "Additional Invoice?",
json_data:data[0].attributes."Additional PO?":: string as "Additional PO?",
json_data:data[0].attributes."Aggregate Contract Value":: number as "Aggreagate Contract Value" ,
json_data:data[0].attributes."Annualized Baseline Spend" :: number as "Annualized Baseline Spend",
json_data:data[0].id ::number as ID,
json_data:data[0].type::string as TYPE
from scout_projects order by ID
the scout project file screenshot is attached.
The attached Screenshot is the output form the given query and as you could see the ID column is the same but there are only 2 unique rows. I want my query to return only those 2 unique rows.
select distinct json_data:data[0].id :: number as ID from scout_projects
what is the approach should I take?
I tried using subquery, but it gave me error stating "single-row subquery returns more than one row. snowflake error" which is obvious. so, need a way out .

How to use json array in WHERE IN clause in Postgres

I have a Postgres query like this
SELECT * FROM my_table WHERE status IN (2,1);
This is part of a big query, but I am facing an issue with the WHERE IN part here. I am using this query inside a function and the input parameters are in JSON format. Now the status values I am getting in in the form of a JSON array and it will be like status=[2,1]. I need to use this array in the WHERE clause in the query and not sure how to do that. Currently, I am using like
SELECT * FROM my_table WHERE status IN (array([2,1]));
But this is giving me an error. The status column is of smallint data type. I know this is simple, but I am very much new to Postgres and could not figure out any method to use the JSON array in WHERE IN clause. Any help will be appreciated.

Postgres query cannot find rows based on column value

I want to select rows based on a column value. I know for a fact the column value exists. The first query returns 100 rows from the listing table. The second query, which looks for listings.OriginatingSystemName = 'mfrmls` returns nothing. Why?
(Removing the quotes or using double quotes does not work).
I am using pgAdmin4 to run these queries.
first query:
select * from listing limit 100;
second query:
select * from listing where 'listing.OriginatingSystemName' = 'mfrmls'
This produces a 'column does not exist' error:
select * from listing where OriginatingSystemName = 'mfrmls'
The correct syntax is to just write the column name in your WHERE statement:
SELECT * FROM listings WHERE "OriginatingSystemName" = 'mfrmls';
To elaborate further:
What your original query is doing is selecting every row in the listings table where the text string 'listings.OriginatingSystemName' is equal to this other text string 'mfrmls'. It is not actually grabbing the value from the column you want. No row in the table satisfies your where statement because your where statement is always false. Therefore, no rows are returned but the query was a success.
We need to implement the double quotes when dealing with case-sensitive identifiers. Here is some helpful documentation.

How do you query a table filtering on a substring of one of the columns?

I have a table I wish to query. It has a string variable called comment which contains an ID along with other things. (i.e. "123456;varA;varB")
rowNo
comment
1
"123456;varA;varB"
2
"987654;varA;varB"
I want to filter based on the first substring in the comment variable.
That is, I want to filter the table on rows where the first substring of comment is "123456" (which in the example would return the first row)
How do I do this?
I was thinking something along the lines of the code below, using the "string_split" function, but it doesn't work.
SELECT *,
FROM table
WHERE (SELECT value FROM STRING_SPLIT(comment,';',1)="123456")
Does anyone have any ideas?
Note, I am querying in SQL in SAS, and this is on a large dataset, so I don't want to create a new table with a new column to then query on instead. Ideally I'd want to query on the existing table directly.
You can use the SCAN() function to parse a string.
WHERE '123456'=scan(comment,1,';')

Inserting a new column into SQL

I have these queries:
SELECT *
FROM dbo.GRAUD_ProjectsByCostCategory
select right(CostCategoryId,14) as CostBreak
from dbo.GRAUD_ProjectsByCostCategory
They work well in that they give me the correct data, but I would like to know how to combine the new column CostBreak into the table of results rather than as a separate query result.
An example of the results I get are as below:
Where I want them in the same table
The data is coming from the same table so you should be able to just add that value to your initial query. You do not even have to perform a join to get it:
SELECT name,
description,
project,
CostCategoryId,
right(CostCategoryId,14) as CostBreak
FROM dbo.GRAUD_ProjectsByCostCategory