fetch values into a comma separated string in postgresql [duplicate] - sql

This question already has answers here:
Postgresql GROUP_CONCAT equivalent?
(9 answers)
Closed 7 years ago.
This should be simple one. A query like
SELECT code_id FROM code WHERE status = 1
gives normally a result like
code_id
10
11
12
But my goal is to fetch into a string like
10,11,12
In order to use it in an other query
SELECT x FROM table WHERE status in (10,12,13)
Preferable in the same query. Is this possible using "standard" Postgresql WITHOUT adding extra extension?
Everything so far is using extension that not are available as standard.
Thanks in advance.

Whatever tool you are using just shows you the data like that for convenience. But you can also use the resultset in a subquery, like this
SELECT x FROM table WHERE status in (
SELECT code_id FROM code WHERE status = 1
)

You can try this way to get result as comma-separated
But my goal is to fetch into a string like
SELECT string_agg(code_id, ',') FROM code WHERE status = 1

Related

Concatenate multiple rows to one row [duplicate]

This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
SQL Server: Combine multiple rows into one row from a join table?
(1 answer)
ListAGG in SQLSERVER
(4 answers)
how to stuff date and time in SQL
(2 answers)
Query to get multiple row into single row
(3 answers)
Closed 3 years ago.
I have the below data and I have to concatenate the long text column and make it as a single row. The challenge part is only one row has the notification number and other rows are null. You cannot group by the notification number.
I need the output as 2 rows
row number Notification Number Plant Creation Date Language Lineno Tag Long Text
1 10014354914 A057 43466 EN 1 >X aaabbbcccdddeeefffggghhhjjjkkklll
2 10014354915 A057 43466 EN 1 >X aaabbbcccdddeeefffgggpppqqqrrrsss
I have used cursor for this. But it is taking much time.
If you are using oracle:
with data("row number", "Notification Number","Plant","Creation Date","Language","Lineno","Tag","Long Text") as (
select 1,10014354914,'A057',43466,'EN',1,'>X','aaabbbcccdddeeefffggghhhjjjkkklll' from dual
union all
select 2,10014354915,'A057',43466,'EN',1,'>X','aaabbbcccdddeeefffgggpppqqqrrrsss' from dual)
select LISTAGG("Long Text",'') within group (order by "row number") from data;
if you are using ms-sql maybe try this:
SELECT u.[Long Text] AS [text()]
FROM yourtable u
ORDER BY u.[row number]
FOR XML PATH ('')

one column with not equal condition in Ms access

I have 5 words in a column where I need to eliminate two words and show the remaining result
Table name temp
Id approval_trade
1 closed
2 closed by
3 open
4 target
5 running
6 now
I need result as given below using not equal to condition
Id approval_trade
3 open
4 target
5 running
6 now
My query
select *
from temp
where approval_trade <> 'closed' and approval_trade <> 'closed by'
Presumably, you have hidden characters that you cannot see but interfere with the comparison. To start, such a query would normally be written using NOT IN:
select *
from temp
where approval_trade not in ('closed', 'closed by');
This is shorter and less prone to accidental error.
In your case, I would ask if this returns the rows you don't want:
select *
from temp
where approval_trade in ('closed', 'closed by');
If it does, then your query (and the not in) version should work. My guess is that they do not. Next, I would go for like. Does this return only the rows you do not want?
select *
from temp
where approval_trade like 'closed*';
If this returns exactly what you want out, then use not like to get what you want:
where approval_trade not like 'closed*'
If this doesn't work, then the problem gets harder. More understanding of what your data exactly looks like and what the queries return would help.

BigQuery split column and get count of count each substring [duplicate]

This question already has answers here:
How can I compute TF/IDF with SQL (BigQuery)
(3 answers)
Closed 4 years ago.
In BigQuery I would like to create a query to count the occurrence of words in a comments field and group by a count of each occurrence. This would me get a sense of what words were used more than others and get a sense of user behavior and moods. Pretty new to bigquery so any ideas will be helpful.
What I ended up doing was using the split function...
SELECT
COUNT(JJ) AS STUFF, JJ
FROM
(SELECT SPLIT(text, ' ') AS JJ FROM [bigquery-public-
data:hacker_news.comments] LIMIT 1000 )
GROUP BY JJ
ORDER BY STUFF DESC
LIMIT 5
Obviously it can be manipulated more by using replace to remove other characters before splitting.

How can i use a thousand separator in SQL Server? [duplicate]

This question already has an answer here:
How can I get a number with a thounds separator on top?
(1 answer)
Closed 5 years ago.
SELECT count(id) FROM table a
it gives me a number lets say 36136, how can i make thousand separator on top?
Try (MSSQL)
SELECT REPLACE(REPLACE(
CONVERT(VARCHAR,CONVERT(MONEY, COUNT(ID)),1)
,'.00','') ,',','''')
FROM TABLE A
Example
SELECT REPLACE(REPLACE(CONVERT(VARCHAR,CONVERT(MONEY, 1000),1),'.00','') ,',','''') X
Output
X
----
1'000
Use TOP statement in SQL server :
SELECT TOP 1000 * FROM table a

SQL Order By in oracle custom order by [duplicate]

This question already has answers here:
Keep order from 'IN' clause
(6 answers)
Closed 8 years ago.
I had a requirement like
select * from USER where firstname IN
('nexus',
'samsung',
'apple');
On executing this query , i get results based on insertion order of rows in database.But i need the results exactly same way, i given for IN parameter.
for ex : the above query should give results like
nexus
samsung
apple
and not any other order like.
samsung
nexus
apple
What can i use with the above query to get selection results in the order i given?
One method is to use case:
order by (case when firstname = 'nexus' then 1
when firstname = 'samsumg' then 2
when firstname = 'apple' then 3
end)
Another method that has less typing:
order by instr(',nexus,samsung,apple,', ',' || name || ',')