I would like to fetch linked entities in BATCH script, but it returns me only the RecordIds.
begin;
let count = select count(*) from Company;
let companies = select from Company ORDER BY name ASC LIMIT 25 FETCHPLAN type:1 portfolios:1;
commit;
return [$count, $companies];
Using OrientDB Server v2.2.17
Try this:
begin;
let count = select count(*) from Company;
let companies = select name from Company ORDER BY name ASC LIMIT 25 FETCHPLAN type:1 portfolios:1;
commit;
return [$count, $companies];
Hope it helps.
Regards,
Michela
Related
I have a function that should return two integers: study asset store and origin asset store.
I have the study asset store saved as a variable from a query and then use CTE to find out what the origin asset store was. However, I'm getting errors trying to return both integers.
What would be the correct syntax for the query below to get the i_study_asset_store and asset_store_id back?
BEGIN
i_study_asset_store = (SELECT stud.asset_store_id FROM study.studies as stud WHERE studyid = _study_id::INT);
WITH study_store AS(
SELECT origin_asset_id
FROM asset.assets
WHERE asset_store_id = 23 --study_asset_store
LIMIT 1
),
origin_asset_store AS(
SELECT asset_store_id
FROM asset.assets
WHERE asset_id IN (SELECT origin_asset_id FROM study_store)
)
RETURN QUERY
SELECT
i_study_asset_store AS study_asset_store
,(SELECT asset_store_id FROM origin_asset_store) AS origin_asset_store;
END;
You got the syntax wrong. It should be
RETURN QUERY
WITH cte1 AS (/* ... */),
cte2 AS (/* ... */)
SELECT ...;
select *
from amazon_shipment, customer
where amazon_shipment.customer_id = customer.customer_id
and amazon_shipment.customer_id in
(select top(1) amazon_shipment.customer_id
from amazon_shipment
group by amazon_shipment.customer_id
order by count(*) desc);
I am trying to select all the customers with the most order, however, I get an error:
FROM keyword not found were expected
TOP(1) isn't available in Oracle.
In Oracle 11gR2 and lower, you can use WHERE ROWNUM < 2
select *
from EMPLOYEES
where rownum < 2
order by SALARY desc;
In Oracle 12c and higher, you can use FETCH FIRST 1 ROWS ONLY
select *
from EMPLOYEES
order by SALARY desc
fetch first 1 rows only;
If there is one more UID in sessions than there is in users (obviously not supposed to be that way), then I expect to have a non-empty result set when I run the last select, but I get no rows returned - this result just doesn't make logical sense to me...
select count(distinct(uid)) from users;
> 108736
select count(distinct(uid)) from sessions;
> 108737
select count(*) from sessions where uid not in (select uid from users);
> 0
and just for completeness:
select count(*) from users where uid not in (select uid from sessions);
> 0
I have checked for nulls:
select count( * ) from sessions where uid is null;
> 0
select count( * ) from users where uid is null;
> 14
The schema is defined in sqlalchemy and includes a foreign key in the session table:
uid = Column(Integer, ForeignKey('users.uid', use_alter=True, name='fk_uid'))
This schema is a static dump for analytics purposes so there is no chance of concurrency issues...
Your third query does not do what you think it does.
The following query illustrates the problem:
SELECT 1 NOT IN (SELECT unnest(ARRAY[NULL]::int[]));
This returns NULL, because it can't say if 1 <> NULL.
So, in your query the where condition is always NULL, because users contains a NULL uid.
I recommend using EXCEPT do find the culprit in your sessions table.
SELECT uid from sessions EXCEPT SELECT uid from users;
I have a table ProductNumberDuplicates_backups, which has two columns named ProductID and ProductNumber. There are some duplicate ProductNumbers. How can I count the distinct number of products, then print out the outcome like "() products was backup." ? Because this is inside a stored procedure, I have to use a variable #numrecord as the distinct number of rows. I put my codes like this:
set #numrecord= select distinct ProductNumber
from ProductNumberDuplicates_backups where COUNT(*) > 1
group by ProductID
having Count(ProductNumber)>1
Print cast(#numrecord as varchar)+' product(s) were backed up.'
obviously the error was after the = sign as the select can not follow it. I've search for similar cases but they are just select statements. Please help. Many thanks!
Try
select #numrecord= count(distinct ProductNumber)
from ProductNumberDuplicates_backups
Print cast(#numrecord as varchar)+' product(s) were backed up.'
begin tran
create table ProductNumberDuplicates_backups (
ProductNumber int
)
insert ProductNumberDuplicates_backups(ProductNumber)
select 1
union all
select 2
union all
select 1
union all
select 3
union all
select 2
select * from ProductNumberDuplicates_backups
declare #numRecord int
select #numRecord = count(ProductNumber) from
(select ProductNumber, ROW_NUMBER()
over (partition by ProductNumber order by ProductNumber) RowNumber
from ProductNumberDuplicates_backups) p
where p.RowNumber > 1
print cast(#numRecord as varchar) + ' product(s) were backed up.'
rollback
what I need to test for on my table is if there are rows for a given user id and order id on two separate days (DATETIME field for a timestamp).
I'm pretty sure I'd need a having clause and that's why I'm here...that frightens me terribly.
Having shouldn't scare you, it is just a "Where" on an aggregated field:
Select UserID, Count(*) From OrderTbl Group By UserID Having Count(*) > 1
That'll give you all the Users that have multiple orders.
Select UserID, Count(*) From OrderTbl Where (UserID=#UserID) Group By UserID Having Count(*) > 1
will give you the count if there are multiple records for the user id in #UserID and null if not.
if exists (Select UserID, Count(*) From OrderTbl Where (UserID=#UserID) Group By UserID
Having Count(*) > 1) Select 1 else Select 0
will return a 1 if there are multiple records for the User, 0 if not.
Update: Didn't realize that you could have multiple orders per day. This query will do what you want:
With DistinctDates as (Select Distinct UserID, [DATE] From OrderTbl Where (UserID=#UserID))
Select UserID, Count(*) From DistinctDates
Group By UserID Having Count(*) > 1
I am not sure if I understood your question, but this may work for you. The HAVING is your friend and you can still use the WHERE clause. This should let you know what order and user id combo is occuring more than once in the table.
SELECT [UserId], [OrderId]
FROM OrderTable
WHERE UserId = #UserId
AND OrderId = #OrderId
GROUP BY UserId, OrderId
HAVING Count(*) > 1