I have a Query that gives me a list of numbers, for example:
61728_0be80d3c-029d-4d06-ae75-37f72fdeacaf
61784_4e1b2b79-1190-4e65-91cc-07552e28b522
61864_f0a58134-a1d5-40f6-ada1-d12b7e991675
61928_3a5a70b1-9350-4acf-99e4-e858f14a6d98
62048_a489f752-ae51-4919-b720-1b6e15235a3e
62112_3a8289e9-c5e6-4aae-8c8a-431cc5ca9415
62176_95fbfdc9-88e3-4918-ac19-6b54f3205af4
62296_2f6fbd6b-9af4-4d6c-85e8-07ba64326669
62688_71c3ee51-0f5c-4f8e-8026-8b90a335795e
62776_e93d9f1d-272f-4161-80eb-5de90a026829
How can I make this query give me all these numbers in agg_string in order to add the to a where clause, so I can filter answers in a different query to only these numbers.
example:
'61728_0be80d3c-029d-4d06-ae75-37f72fdeacaf','61784_4e1b2b79-1190-4e65-91cc-07552e28b522',
'61864_f0a58134-a1d5-40f6-ada1-d12b7e991675' etc
in order to put it inside of:
where XXX IN ('61728_0be80d3c-029d-4d06-ae75-37f72fdeacaf','61784_4e1b2b79-1190-4e65-91cc-07552e28b522','61864_f0a58134-a1d5-40f6-ada1-d12b7e991675')
Any way to do it auto in sql or excel if not?
tried to use string_agg("personId",',') and it will add the commas between the numbers but i cant add Apostrophe at the beginning of the personID and at the end
You can concatenate single quotes to the ID inside the string_agg()
string_agg(concat('''', "personId", ''''), ',')
or a bit simpler:
string_agg(quote_literal("personId"), ',')
If those IDs are the result of a query, then you can also use it directly:
where xxx in (select "personId" from ...)
Related
I want to somehow hash the result of a query in PostgreSQL. I have a query like
SELECT output FROM result;
And it returns a column composed only of integers. So I somehow want to hash the result of this query. Concatenate the values and hash, or somehow hash the query output directly. Simply I need a way to put it inside SELECT sha256(...). So please note that I do not want to get hash of every column entry, but one hash that somehow corresponds to the query output. Any ideas?
PostgreSQL doesn't come with a built-in streaming hash function exposed to the user, so the easiest way is to build the string in memory and then hash it. Of course this won't work with giant result sets. You can use digest from the pg_crypto extension. You also need to order your rows, or else you might get different results on the same data from one execution to the next if you get the rows in different orders.
select digest(string_agg(output::text,' ' order by output),'sha256')
from result;
Replace 1234 with your column name and add [from table_name] to this query:
select encode(digest(1234::text, 'sha256'), 'hex')
Or for multiple rows use this:
select encode(
digest(
(select array_agg(q1)::text[] from (select row(R.*)::text as q1 from (SELECT output FROM result)R)alias)::text
, 'sha256')
, 'hex')
I'm trying to write a query in sql to exclude a keyword:
It's a list of cities written out (e.g. AnnArbor-MI). In the list there are duplicates because some have the word 'badsetup' after the city and these need to be discarded. How would I write something to exclude any city with 'badsetup' after it?
Your question title and content appear to be asking for two different things ...
Query cities while excluding the trailing 'badsetup':
SELECT regexp_matches(citycolumn, '(.*)badsetup')
FROM mytable;
Query cities that don't have the trailing 'badsetup':
SELECT citycolumn
FROM mytable
WHERE citycolumn NOT LIKE '%badsetup';
In psql, to select rows excluding those with the word 'badsetup' you can use the following:
SELECT * FROM table_name WHERE column NOT LIKE '%badsetup%';
In this case the '%' indicates that there can be any characters of any length in this space. So this query will find any instance of the phrase 'badsetup' in your column, regardless of the characters before or after it.
You can find more information in section 9.7.1 here: https://www.postgresql.org/docs/8.3/static/functions-matching.html
I have a table with values like this:
book;65
book;1000
table;66
restaurant;1202
park;2
park;44444
Is there a way using postgres sql to remove everything, regardless of the length of the word, that includes the semi-colon and everything after it?
I plan on doing a query that goes something like this after I figure this out:
select col1, modified_col_1
from table_1
--modified is without the semi-colon and everything after
You can use substring and strpos() for this:
select col1, substring(col1, 1, strpos(col1, ';') - 1) as modified_col_1
The above will give an error if there are values without a ;
Another option would be to split the string into an array and then just pick the first element:
select (string_to_array(col1, ';'))[1]
from table_1
This will also work if no ; is present
I want to display a string on each row (Details section) in my Crystal Report. The contents of this string will be retrieved with the help of a SQL Expression.
The SQL I have is follows: However if multiple rows are returned, I am not sure how to convert that into a Comma Separated String. I have an Oracle 11g database.
(select distinct NAME from TEST
where SAMPLE_NUMBER = "TEST"."SAMPLE_NUMBER"
and X_BENCH <> '"TEST"."X_BENCH"')
The TEST Table looks like this:
My report will be filtered for all samples with a specific test (e.g. Calcium). For those samples on the report, My SQL Expression should retrieve all "Other" Tests on the sample. See output example.
You can accomplish this with a wm_concat. WM_CONCAT takes a bunch of rows in a group and outputs a comma separated varchar.
Using the substr function you can separate the first result with the last.
Please note that I am dirty coding this (without a compiler to check my syntax) so things may not be 100% correct.
select sample_number
, substr(wm_concat(name),1,instr(wm_concat(name),",")-1) as NAME
, substr(wm_concat(name),instr(wm_concat(name),","),length(wm_concat(name)-instr(wm_concat(name),",")+1) as OTHER_TEST_NAMES
from TEST
where SAMPLE_NUMBER = "TEST"."SAMPLE_NUMBER"
and X_BENCH <> '"TEST"."X_BENCH"'
and rownum < 2
group by sample_number
However, if it is not necessary to separate the name and the other test names, it actually is much simpler.
select sample_number
, wm_concat(name) as NAMES
from TEST
where SAMPLE_NUMBER = "TEST"."SAMPLE_NUMBER"
and X_BENCH <> '"TEST"."X_BENCH"'
and rownum < 2
group by sample_number
Also please try to organize your lines to make it easier to read.
You can use LISTAGG for Converting Rows to Comma-Separated String in Oracle.
Example:
SELECT user_id
, LISTAGG(expertise, ',')
WITHIN GROUP (ORDER BY expertise)
AS expertise
FROM TEMP_TABLE
GROUP BY user_id;
I would like to be able to sort (order by) in postgres ignoring leading words like "the, a, etc"
one way: script (using your favorite language) the creation of an extra column of the text with noise words removed, and sort on that.
Add a SORT_NAME column that has all that stuff stripped out. For bonus points, use an input trigger to populate it automatically, using your favorite SQL dialect's regex parser or similar.
Try splitting the column and sorting on the second item in the resulting array:
select some_col from some_table order by split_part(some_col, ' ', 2);
No need to add an extra column. Strip out the leading words in your ORDER BY:
SELECT col FROM table ORDER BY REPLACE(REPLACE(col, 'A ', ''), 'The ', '')