Query SQL on Orientdb with append and substring - orientdb

I need to retrieve the country from a number having a table with international_prefix and local_prefix like image.
This is a test that I've made but unfortunately won't work.
SELECT *
FROM core_phone_prefix
WHERE (("+393925559000").asString()).left(international_prefix.append(local_prefix).length()) = international_prefix.append(local_prefix)

Try these:
With this one I compare only the prefix
select distinct(country) as country from(select international_prefix, "+393925559000" as number, country, local_prefix from core_phone_prefix) where international_prefix=number.subString(0,international_prefix.length())
Instead, with this one I compare the international_prefix + the local_prefix with the international_prefix and the local_prefix of the number
select country as country from(select international_prefix, "+393925559000" as number, country, local_prefix from core_phone_prefix) where international_prefix.append(local_prefix)=number.subString(0,international_prefix.append(local_prefix).length())
Hope it helps,
Let me know

Can you try with this query
select country from(
select number.left(international_prefix.append(local_prefix).length()) as a, international_prefix.append(local_prefix) as b, country from (
select international_prefix, "+393925559000" as number, country, local_prefix from core_phone_prefix
)
) where a = b
UPDATED
You can insert two index, one on the field international_prefix (NOTUNIQUE_HASH_INDEX) and one on the field local_prefix (NOTUNIQUE_HASH_INDEX )
You can use this query
select expand($a) from (select "+393925559000" as number from core_phone_prefix limit 1)
let $a=(select from core_phone_prefix WHERE international_prefix = $parent.current.number.left(international_prefix.length())
and local_prefix = $parent.current.number.subString(international_prefix.length(),international_prefix.append(local_prefix).length())) limit -1
Let me know

Related

How to make a Select sentence with acumulate in final column

I'm trying to make a query for acum the sum of one of fields in a new column, but i dont get it in SqlServer 2008 r2
I have the next table:
Fields: id,Codigo,tipo,cantidad
I want to make a query for get the next result
When the field tipo is 2, the acum begins
Is it possible?
Thanks.
finally i found a good sentence for me
with AcumulaCant as (
select
d.idrecno,
d.tipmov,
d.codart,
d.codalm,
d.cant
from movsto d
)
select *,
CantAcum = (
select SUM(cant)
from AcumulaCant c
where c.idrecno <= AcumulaCant.idrecno
and c.codart = AcumulaCant.codart
and c.codalm = AcumulaCant.codalm),
ROW_NUMBER() over(partition by codart,codalm order by idrecno desc) as rn
from AcumulaCant
order by AcumulaCant.codart,AcumulaCant.codalm,AcumulaCant.idrecno desc
With this sentence i get the acum of the sum quantities by ref.
Thanks to all for your comments.

Returning rows with distinct column value with data jpa named query

Assuming I have a table with 3 columns, ID, Name, City and I want to use named query to return rows with unique city..can it be done?
Are you asking whether it is possible to write a query that will return the cities that appear in exactly one row, in a table that has ID/Name/City triplets where there could be multiple rows for the same city but with different names?
If so, it would depend on the database engine behind the scenes - but you could try things like:
with candidates (city, num) as (
select city, count(*) from table
group by city
)
select city from candidates where num = 1
Or
select t1.city from table t1
where not exists (
select * from table t2
where t2.city = t1.city and t2.id <> t1.id
)
where table is your table with these triplets.

psql, display column that is not in the group by clause

i'm having problems with a query. I have two tables: country and city and i want to display the city with the highest population per country.
Here's the query:
select country.name as coname, city.name as ciname, max(city.population) as pop
from city
join country on city.countrycode=country.code
group by country.name
order by pop;`
Error
column "city.name" must appear in the GROUP BY clause or be used in an aggregate function.
I don't know how to solve this, i tried to make a subquery but it didn't work out.
How can i make it work?
You can easly get it using rank function:
select * from
(
select country.name as coname,
city.name as ciname,
city.population,
rank() over (partition by country.name order by city.population desc) as ranking
from
city
join
country
on city.countrycode=country.code
) A
where ranking = 1

Getting value from table with max key

I have a table with two columns:
UserId (auto int)
Email(Nvarchar)
I want to retrieve the email that was last inserted on table.
I've tried some options, but nothing seems to be working.
Thanks in advance.
Perhaps simply:
SELECT TOP 1 email FROM dbo.Table ORDER BY UserId DESC
or
SELECT UserId, Email
FROM dbo.Table
WHERE UserId = (SELECT MAX(UserId) FROM dbo.Table)
However, it's not good practise to abuse a primary-key column for information like "last inserted". Add a datetime column for this.
You could also use the ROW_NUMBER function:
WITH x AS (
SELECT UserId, Email,
rn = Row_number() OVER(ORDER BY UserId DESC)
FROM dbo.table)
SELECT UserId, Email
FROM x
WHERE rn = 1

Selecting distinct substring values

I have a field that is similar to a MAC address in that the first part is a group ID and the second part is a serial number. My field is alphanumeric and 5 digits in length, and the first 3 are the group ID.
I need a query that gives me all distinct group IDs and the first serial number lexicographically. Here is sample data:
ID
-----
X4MCC
X4MEE
X4MFF
V21DD
8Z6BB
8Z6FF
Desired Output:
ID
-----
X4MCC
V21DD
8Z6BB
I know I can do SELECT DISTINCT SUBSTRING(ID, 1, 3) but I don't know how to get the first one lexicographically.
Another way which seems to have the same cost as the query by gbn:
SELECT MIN(id)
FROM your_table
GROUP BY SUBSTRING(id, 1, 3);
SELECT
ID
FROM
(
SELECT
ID,
ROW_NUMBER() OVER (PARTITION BY SUBSTRING(ID, 1, 3) ORDER BY ID) AS rn
FROM MyTable
) oops
WHERE
rn = 1