Orientdb - where clause with multiple edges - graph-databases

i'm still quite new at this so I will just go to the case.
In our application we have notes that can be shared between users and stored in folders. Single note can obviously be in multiple folders (which cannot be shared). Till now for displaying notes in folder we were using this query:
(select expand(both(\'InFolder\')) from #folder_rid)
But now, we are developing some "advanced filters" where we want to show notes that are inside specific folder or those notes which are not in in. Notes can also be tagged (by single tag) so it was quite easy using "where clause"
SELECT FROM (select expand(both(\'CanView\')) from #user_rid) where out(\'HasTag\').#rid = #tag_rid
or
SELECT FROM (select expand(both(\'CanView\')) from #user_rid) where out(\'HasTag\').#rid <> #tag_rid
And now the problem. Since note can be stored in multiple folders above approach does not work. It works when I specify on which position in this array of edges(?) target folder should be:
SELECT FROM (select expand(both(\'CanView\')) from #user_rid) where out(\'InFolder\')[0].#rid = #folder_rid
SELECT FROM (select expand(both(\'CanView\')) from #user_rid) where out(\'InFolder\')[1].#rid = #folder_rid
But obviously this is not the way to do this.
I found (http://orientdb.com/orientdb-improved-sql-filtering/) that i can use ranges to do this, like
SELECT FROM (select expand(both(\'CanView\')) from #user_rid) where out(\'InFolder\')[0-2].#rid = #folder_rid
But it just calculates the value inside square brackets(oO).
I also tried with [1,2,3] and using infos from https://code.google.com/p/orient/wiki/Document_Field_Part
but I couldn't get it to work.
What is the correct syntax to do this or is this whole approach just bad. If problem is in the basics what is the good way to do this? Thanks for help and sorry for all mistakes I did in grammar.

I believe the query you're looking for is:
SELECT
FROM (SELECT expand(out('CanView')) FROM <user_rid>)
WHERE <folder_rid> IN out('InFolder')
An example:
create class User extends V
create class Note extends V
create class Folder extends V
create class CanView extends E
create class InFolder extends E
create vertex User set name = 'user'
create vertex Note set name = 'note0'
create vertex Note set name = 'note1'
create vertex Note set name = 'note2'
create vertex Folder set name = 'folder0'
create vertex Folder set name = 'folder1'
create edge CanView from (select from User where name = 'user') to (select from Note where name = 'note0')
create edge CanView from (select from User where name = 'user') to (select from Note where name = 'note1')
create edge InFolder from (select from Note where name = 'note0') to (select from Folder where name = 'folder0')
create edge InFolder from (select from Note where name = 'note1') to (select from Folder where name = 'folder1')
create edge InFolder from (select from Note where name = 'note2') to (select from Folder where name = 'folder1')
There are three notes. The user can see note0 and note1. note0 is in folder0, note1 is in folder1.
Let's say we want all the notes in folder0 the user can see. The query above will return note0.

Related

LEFT join on secure view fails in snowflake

I have a normal view and one secure view and a table.
create or replace NORMALVIEW as select click_id FROM SECURE_VIEW a
LEFT JOIN
TABLE e
ON (e.click_id = a.google_click_id);
select * from NORMALVIEW;
I am getting the error:
SQL execution internal error: Processing aborted due to error 300002:2523989150; incident 8846932.
other operations such as union is working
If you got an incident then you will need to open a case with Snowflake support to understand what is happening.
So I assume the SELECT part is valid:
select e.click_id
FROM SECURE_VIEW a
LEFT JOIN TABLE e
ON (e.click_id = a.google_click_id);
I also assume the view is actually defined more like:
CREATE table test.test.table_name (click_id int);
insert into test.test.table_name values (0),(1);
CREATE SECURE VIEW test.test.SECURE_VIEW AS
SELECT click_id as google_click_id
FROM test.test.table_name
WHERE click_id%2 = 1;
CREATE VIEW test.test.NORMALVIEW AS
SELECT click_id
FROM test.test.SECURE_VIEW AS a
LEFT JOIN test.test.table_name AS e
ON e.click_id = a.google_click_id;
Or some other all valid stuff, thus those views would not be created. for the select to fail on.
I wonder if you grab the DDL of the view and try recreate then (with different names) if they are still valid, or if the shape of some underlying table changed, or if something as be drop/recreate/renamed..
SELECT * from NORMALVIEW;
But yes, at the point open a support ticket.

SQL Server: Update where exists and Insert when not exists

I have 2 tables, one is called
INTER and the other one is called COM
The relationship between them is 1:many
Here's the structure of both tables
INTER
NUMBER
ADDRESS
COM
NUMBER
CONTACT
REFERENCE
To give some context about the tables:
The ADDRESS field from the INTER table can be empty (empty string '')
The CONTACT field of the COM table can be of two types: "E-mail" or "Website"
What I'm trying to accomplish is the following:
I want to look at all the records from COM where CONTACT='Website' and insert into COM the values of (INTER.NUMBER,'Website',INTER.ADDRESS)
where INTER.ADDRESS<>'' and INTER.NUMBER does not exist in COM.NUMBER
Otherwise, I want to update the value of COM.REFERENCE, set it to INTER.ADDRESS where COM.CONTACT='Website' and INTER.NUMBER exists in COM.NUMBER and INTER.ADDRESS<>''
How can I accomplish this? I know what the merge statement is but according to the documentation merge does not work well on filtered queries. Also, I know about the exists statement but I don't know how to make this query work.
In case you're wondering, unfortunately, the structure of these tables cannot be modified.
Some sample data:
INTER
ABCD123,google.com
XUEH342,facebook.com
IISI521,twitter.com
IEIEK885,''
COM
ABCD123, Website, test.com
ABCD123,E-mail,bob#gmail.com
XUEH342,Website,facebook.com
XASE456,Website, stackoverflow.com
XASE456,E-mail,tom#gmail.com
After running the query, the results on COM table would look as the following:
ABCD123,Website, google.com
ABCD123,E-mail,bob#gmail.com
XUEH342,Website, facebook.com
IISI521,Website, twitter.com
XASE456,Website, stackoverflow.com
XASE456,E-mail,tom#gmail.com
As you can see, IISI521,twitter.com got inserted,ABCD123,test.com got updated and IEIEK885,'' didn't get inserted because is an empty string.
You can try below query:
MERGE com with (HOLDLOCK) AS cm
USING (SELECT a.number, a.address, b.contact FROM inter a left join com b on a.number = b.number and b.contact = 'website' WHERE a.address != '' ) AS intr
ON cm.number = intr.number and cm.contact = intr.contact
WHEN MATCHED THEN
UPDATE
SET cm.reference = intr.address
WHEN NOT MATCHED THEN
INSERT
(
number,
contact,
reference
)
VALUES
(
intr.number,
'website',
intr.address
);

PostgreSQL adding multiple results to an array

Hello Stack Overflow Users,
I'm trying to pull together a list of users and all of their permissions, but I want to aggregate the permissions so they they list in the single record of the user.
The users table is pretty simple, but the permissions are broken out individually.
To give you an example, there are 3 tables: users, user_access, page
Also, I'm stuck with PostgreSQL v8.4.
The data pretty much looks like the following:
users table
user_id,username
1,bob
2,cindy
3,jen
user_access table
id,user_id,page_id,allowed
1,1,5,true
2,1,7,true
3,1,8,false
4,2,4,true
5,2,5,true
6,2,7,false
7,3,1,true
8,3,5,false
page table
page_id,page_name
1,report_1
2,report_2
3,report_3
4,admin
5,users
6,options
7,addl_options
8,advanced
So far I have been able to create an array for the permissions, but I'm only grabbing a single page permission and displaying the result of whether it's allowed or not. I'm having a hard time figuring out how to collect the rest of the pages and what they're set to within the one cell for each user.
What I have so far:
select users.user_id, users.username,
(select array(select page.page_name || ':' || user_access.allowed)
where users.user_id = user_access.user_id and page.page_id = user_access.page_id) as permissions
from users
full join user_access on users.user_id = user_access.user_id
full join page on page.page_id = user_access.page_id;
But this only returns results like the following:
user_id,username,permissions
1,bob,{report_1:<null>}
1,bob,{report_2:<null>}
1,bob,{report_3:<null>}
1,bob,{admin:<null>}
1,bob,{users:true}
1,bob,{options:<null>}
1,bob,{addl_options:true}
1,bob,{advanced:false}
What I want to get back instead is all permissions per user record like this:
user_id,username,permissions (where permissions is an array of <page_name>:<allowed>)
1,bob,{report_1:<null>,report_2:<null>,report_3:<null>,admin:<null>,users:true,options:<null>,addl_options:true,advanced:false}
2,cindy,{report_1:<null>,report_2:<null>,report_3:<null>,admin:true,users:true,options:<null>,addl_options:false,advanced:<null>}
3,jen,{report_1:true,report_2:<null>,report_3:<null>,admin:<null>,users:false,options:<null>,addl_options:<null>,advanced:<null>}
I appreciate any advice you could assist with.
Thank you!
You need a cross join between users and page, then an outer join to the permissions. The result of that then needs to be grouped:
select u.user_id, array_agg(concat(p.page_name, ':', coalesce(ua.allowed::text, '<null>')) order by p.page_name)
from users u
cross join page p
left join user_access ua on ua.page_id = p.page_id and ua.user_id = u.user_id
group by u.user_id;
Online example: https://rextester.com/XYC99067
(In the online example I used string_agg() instead of array_agg() as rextester doesn't display arrays nicely)

How to get Option Set Description field in MSCRM

I've tried to find where Option Set descriptions are stored in CRM's database. After research on the internet I've found that Option Set data is stored in the StringMap SQL table but this table doesn't contain description Field I want.
Does anyone know where Option Set descriptions are stored stored in CRM's SQL database? Below is a screenshot highlighting the field value I'm looking for:
Try this:
SELECT Label FROM [LocalizedLabelView] llv
join [AttributePicklistValueView] apvv on llv.ObjectId = apvv.AttributePicklistValueId
join [OptionSetView] osw on apvv.OptionSetId = osw.OptionSetId
join [AttributeView] aw on osw.OptionSetId = aw.OptionSetId
where aw.Name = 'fieldname' and llv.ObjectColumnName = 'Description'
This works for both global and non-global option sets, you just have to put as fieldname the name of the attribute on the entity (not a name of global option set). Of course to handle only global option sets you will not need the last join, simply do osw.Name = 'globaloptionsetname'
This seems to work:
SELECT DISTINCT l.Label
FROM MetadataSchema.LocalizedLabel l
LEFT JOIN MetadataSchema.AttributePicklistValue ap ON l.ObjectId = ap.AttributePicklistValueId
LEFT JOIN MetadataSchema.OptionSet os ON os.OptionSetId = ap.OptionSetId
WHERE l.ObjectColumnName = 'Description' AND os.Name = '<OPTIONSET_NAME>' AND ap.Value = <OPTIONSET_VALUE>
There are two parameters in the above script that you need to modify:
<OPTIONSET_NAME> must be replaced with the schema name of your optionset and prefixed with the entity's schema name. For example, if your optionset is called new_businessTypes and it's on the account entity, then <OPTIONSET_NAME> would be replaced with 'account_new_businesstypes'.
<OPTIONSET_VALUE> must be replaced with the integer value of the option you're looking for. In your example screenshot, that value is 2.

Stored Procedure with two input params and multiple matches and create view with in

I am not much strong in SQL, so looking for some help.
First I am looking for suggestion for the best way to implement this logic in SQL and then some sample code to implement.
My portal is going to connect Students and Training Providers.
Students: Select what courses (multiple) they want, type of delivery (online, class room), Industry(domain) to which the course to be targeted more, Location Preference.
Training Providers: Select what courses offering (so one record for each course), offering locations, type of delivery for each course, industries (multiple) it is targeting.
When student login:
I would like to create SP which in turn create view to store the matched records of the Training Providers data which matches that student needs of that StudentID, CourseID passed to SP
I have created the following sp ( but not included create view part as I am not sure how to do this)
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[sp_TPsMatched2StuCourse]
-- Add the parameters for the stored procedure here
#StuID int,
#CourseID int
AS
BEGIN
Select TP.MemID,TP.PastExp,SN.DeliveryType,SN.LocPref,SN.Industry,SC.CourseID from
tbl_TrainingProvider as TP , tbl_StuCourses as SC, tbl_StuNeeds SN
where SN.CourseID = #CourseID and SN.StuID = #StuID and
SN.DeliveryType in (TP.DeliveryMode) and
SN.LocPref IN (TP.LocOffering) and
SN.Industry IN (TP.Industries)
END
--- exec sp_ELsMatched2EntProp 1, 1
Why I need to put the data is as follows:
Assume the data is stored in that dynamic view and that would be bind to datagrid. Student then select interested TPs. Then only contact details would be shared to each other and this cannot be reveresed. So I would put this interested data in another table later. Every time data changes, hence the matches. Student can change some of his/her needs or new TPs join etc so view to be temparory.
when I executed this using above command, I am not getting data though it matches few records. What is wrong I am doing.
Any help would be greatly appreciated.
You are not getting expected results because you filter out too many records in WHERE( I'm talking about this part : SN.DeliveryType in (TP.DeliveryMode) and
SN.LocPref IN (TP.LocOffering) and SN.Industry IN (TP.Industries)). I'd recommend to use JOIN ... ON instead of specifying all tables in FROM and join condition in WHERE. I'm not sure what you want exactly, but I believe you are looking for
FROM tbl_StuNeeds SN
LEFT JOIN tbl_TrainingProvider as TP ON (TP.DeliveryMode = SN.DeliveryType AND
SN.LocPref = TP.LocOffering AND TP.Industries = SN.Industry)
WHERE SN.CourseID = #CourseID and SN.StuID = #StuID
Also, there is no join conditions in your code for tbl_StuCourses as SC which results in cross-join.
Finally, why do you need a stored procedure at all? From what I see in your example, a table-valued function will work better:
CREATE FUNCTION [dbo].getTPsMatched2StuCourse(#StuID INT,#CourseID INT)
RETURNS TABLE AS
RETURN
Select .... ;

Resources