PostgreSQL, add prefix to record field in SQL - sql

the problem is:
is there a query in postgreSQL that will add some prefix to every record specific field? For example let's assume we have table users:
**id** **name**
1 adam
2 ewa
and i want to get records and add to every id some specific prefix - let's say 'foo_', desirable result:
id: 'foo_1', name: adam
id: 'foo_2', name: ewa
I know it can be done with Python after retrieving records, but i wonder if possible with query.
Thanks in advance!!

select 'foo_'||id::text,
name
from the_table;

You can just use a concatenation operator String Functions and Operators
SELECT 'foo_' || id::text FROM table

Related

How to extract the data between the last two brackets in Postgresql?

So for example I have the following column:
Hello this is (BR20134)
Paris, France (BR10293)
(BR62543) Spice girls (BR6729)
I need to have following column
BR20134
BR10293
BR6729
I am using this query to extract what's between columns:
select substring(Column FROM '\((BR.+)\)')
FROM Table
but obviously it only works if there is one pair of brackets in a field and not two.
I am using Postgresql btw.
Thanks
This always extracts the last parenthesized expression:
substring(textcolumn FROM '\((BR[^)]+)\)[^(]*$')

How to aggregate logs by field and then by bin in AWS CloudWatch Insights?

I'm trying to do a query that will first aggregate by field count and after by bin(1h) for example I would like to get the result like:
# Date Field Count
1 2019-01-01T10:00:00.000Z A 123
2 2019-01-01T11:00:00.000Z A 456
3 2019-01-01T10:00:00.000Z B 567
4 2019-01-01T11:00:00.000Z B 789
Not sure if it's possible though, the query should be something like:
fields Field
| stats count() by Field by bin(1h)
Any ideas how to achieve this?
Is this what you need?
fields Field | stats count() by Field, bin(1h)
If you want to create a line chart, you can do it by separately counting each value that your field could take.
fields
Field = 'A' as is_A,
Field = 'B' as is_B
| stats sum(is_A) as A, sum(is_B) as B by bin(1hour)
This solution requires your query to include a string literal of each value ('A' and 'B' in OP's example). It works as long as you know what those possible values are.
This might be what Hugo Mallet was looking for, except the avg() function won't work here so he'd have to calculate the average by dividing by a total
Not able to group by a certain field and create visualizations.
fields Field
| stats count() by Field, bin(1h)
Keep getting this message
No visualization available. Try this to get started:
stats count() by bin(30s)

Are there some kind of "Reverse Wildcards" in SAP OpenSQL?

So we have a table with a field that contains strings.
These strings can contain wildcards.
For example:
id | name
---+----------------
1 | thomas
2 | san*
3 | *max*
Now I want to select from that table with respect to these wildcards.
For example something like this:
SELECT * FROM table WHERE name = 'sandra'.
That SELECT should fetch the record with ID = 2 from my table.
Note that it would be ok to use % instead of * as the wildcard character in the table.
Any way to achieve this in OpenSQL?
You can use wildcards, just the sign (like Matecki said), is %.
Take a look here:
https://scn.sap.com/thread/1418148
Additionally You could create and use a ranges table in the where clause. If You do not know, what it is, and how it can be done, just tell me. Populate the ranges table like this: OPTION = CP, SIGN = I, LOW = san.
Ok for You?
UPDATE:
I was wrong and changed the answer

query, where field contains 2 t's

is there a way to perform a where clause that will match only 2 t's independent off where they are located.
such as
Matthew --
would work
Thanatos --
would work
Thanatos T --
would not work
Tom --
would not work
I've been Googling but cant find anything specific about this
any help is apreciated
You could try
SELECT *
FROM Table
WHERE Field LIKE '%t%t%' AND Field NOT LIKE '%t%t%t%'
I'm curious which would be faster, this or Goat CO's answer.
You could use LEN() and REPLACE():
SELECT *
FROM Table
WHERE LEN(REPLACE(field,'t','tt')) - LEN(Field) = 2
Demo: SQL Fiddle

how to improve performance from a selection of the longest prefix within table

There is a table holding some values like:
id | prefix | name
----+----------------+--------------------------
1 | record1 | name for record 1
2 | record2 | name for record 2
3 | record | name for record 3
4 | another rec | name for record 4
In order to select the longest prefix of a given text and return the name I use the following SQL:
select top 1 name from prefixes where :text like prefix + '%' order by prefix desc
And this is exactly what I need, when I give text record1 it returns me name for record 2 when record1 it returns me name for record 1, if I give a it returns me name for record 4.
But the problem is that this is executed a few times and the table is updated a lot, so the performance in my case (table with just 210000 rows) is around 300ms, I would like to reduce this, is there something could be improved on the query or even on the database?
I don't know Sybase internals really well. However, look at the plan to see if it is using the index. If so, is it doing a full scan of the index or is the engine smart enough to understand the "like".
My guess is that the engine is doing a full scan. You might be able to trick it to seeking to the right starting location by changing the query:
where prefix >= :text and :text like prefix + '%'
However, it will probably do a full scan from that point forward. You can fix this by having a maximum place to search:
where prefix >= :text and prefix <= :text + 'zzz'
(Assuming that you are using alpha-numeric values in the prefix, this should be ok. You can also use something like :text + '}', because '}' has a very high ASCII value, assuming you are using an ASCII collating sequence.)
Are your prefixes known in advance? That is, for "record1" is the prefix always "record"? Or are you considering "r", "re", and so on.
If the former, then add a new column which contains the "base" part of the prefix. Build an index on this column and change the join to equality. The engine will fetch only the records from the index.
The issue of having the column "name" in the index is to prevent the additional step of looking up the name on the data pages for the table. Once again, this depends on how Sybase optimizes the query. It should find the appropriate records only using the index and then look up the fields after applying the top 1. However, if it fetches all the values, then applies the top 1, having "name" in the index will be a benefit.