OrientDB: Join in OrientDB - How to - orientdb

Hello everybody (again),
I am trying to join two class in orient db.
I want all the records and properties from two class as a result.
Since here join not works so please suggest me in orient db how join works
and please suggest me also how to use edges for join in orientdb

Its rather simple: Write the rid of the target-record into a field in your master-table.
I will describe using the activeorient, the ruby-orientDB ORM:
DB.create_class :basiswert
=> Basiswert
DB.create_class :stock
=> Stock
apple = Basiswert.create name: 'Apple', kind: 'silicon valley company'
=> #<Basiswert:0x0000000241ca38 #metadata={"type"=>"d", "class"=>"basiswert", "version"=>1, "fieldTypes"=>nil, "cluster"=>53, "record"=>0}, #d=nil, #attributes={"name"=>"Apple", "kind"=>"silicon valley company", "created_at"=>Fri, 24 Feb 2017 16:55:37 +0100}>
apple_stock = Stock.create symbol: 'AAPL', :price => 200, basiswert: apple
=> #<Stock:0x00000003ecb370 #metadata={"type"=>"d", "class"=>"stock", "version"=>1, "fieldTypes"=>"basiswert=x", "cluster"=>57, "record"=>0}, #d=nil, #attributes={"symbol"=>"AAPL", "price"=>200, "basiswert"=>"#53:0", "created_at"=>Fri, 24 Feb 2017 16:55:43 +0100}>
apple_stock.basiswert
=> #<Basiswert:0x0000000241ca38 #metadata={"type"=>"d", "class"=>"basiswert", "version"=>1, "fieldTypes"=>nil, "cluster"=>53, "record"=>0}, #d=nil, #attributes={"name"=>"Apple", "kind"=>"silicon valley company", "created_at"=>Fri, 24 Feb 2017 16:55:37 +0100}>
Alternatively you just put "#53:0" into »apple-stock.basiswert«
This is a unidirectional join (or a simple link).
Obviously you can query the stock-class
Stock.where basiswert: apple-stock.rid
or in plain OrientDB-SQL
select from stock where basiswert= "#53:0"

Related

Trying to isolate the hours given a date range (YYYY:MM:DD HH:MM:SS) in SQL and group them. by specific hour intervals regardless of the date

I am struggling trying to extract information out of the database I created in SQL. The views work great and all data is displayed but I am trying to isolate the following:
Isolate time frames from 07:00:00 to 09:00:00.
Still new to coding, so help is appreciated.
SELECT ch.name,
t.date,
t.amount,
t.card AS "Credit Card",
t.id_merchant,
m.name AS "Merchant",
mc.name AS "merchant category"
FROM transaction AS t
JOIN credit_card AS cc
ON (t.card = cc.card)
JOIN card_holder AS ch
ON (cc.cardholder_id = ch.id)
JOIN merchant AS m
ON (t.id_merchant = m.id
JOIN merchant_category AS mc
ON (m.id_merchant_category = mc.id);

TSQL PIVOT Function: Invalid column name error and creating calculated column

I'm pretty new to the PIVOT function and I have been trying to figure this out for the past day and a half so I thought I would create an account after lurking for so long and just ask.
I have a table with the layout as follows:
AsOfDt AcctNum MntYr Dt Category Count
4/15/2015 12345 Jan-15 1/18/2015 Registered User 1
4/15/2015 12346 Feb-15 2/7/2015 New Registration User 1
4/15/2015 12347 Jan-15 1/27/2015 Unique Account 1
4/15/2015 12348 Jan-15 1/24/2015 Registered User 1
This is the end result I am trying to achieve
MntYr Account Population New Registration User Registered User Unique Account
Jan 2015 330984 12 26212 26311
Feb 2015 331897 2953 58702 58894
Mar 2015 343561 950 29498 29638
Apr 2015 343181 675 8845 8916
Grand Total 1349623 4590 123257 123759
Here is the Query that I currently have built:
WITH BaseQuery AS (
SELECT
MntYr
,Category
,[Count]
FROM [dbo].[rpt_gen_WebPortal_TestingData]
)
SELECT [MntYr]
,'Account Population'
,'Unique Account'
,'Registered User'
,'New Registration User'
FROM BaseQuery
pivot (sum([count]) for MntYr
in ("Jan 2015", "Feb 2015", "Mar 2015", "Apr 2015" )
) AS Pivoting
My first question:
I am getting an error for my MntYr column in the second SELECT statement, "Invalid column name 'MntYr'." I really don't understand why this is throwing an error. What am I doing wrong with trying to pull that column when I explicitly name it in my BaseQuery pull?
My second question:
I would also like to create a calculated field based upon the percentage of (Unique Account / Account Population), but I'm not quite sure how to go about calculated fields in a PIVOT function. Any ideas on how to get started with this one?
Any and all help would be much appreciated!
Thanks.
Your pivot clause was wrong. You also don't need a CTE. Try this:
SELECT
MntYr
,[Account Population]
,[Unique Account]
,[Registered User]
,[New Registration User]
,case
when isnull([Account Population],0) = 0 then 0
else 100 * [Unique Account] / [Account Population]
end Pct
FROM (
SELECT
MntYr
,Category
,[Count]
FROM [dbo].[rpt_gen_WebPortal_TestingData]
) BaseQuery
pivot (sum([Count]) for Category
in ([Account Population]
,[Unique Account]
,[Registered User]
,[New Registration User] )
) AS Pivoting

Selecting rows only if meeting criteria

I am new to PostgreSQL and to database queries in general.
I have a list of user_id with university courses taken, date started and finished.
Some users have multiple entries and sometimes the start date or finish date (or both) are missing.
I need to retrieve the longest course taken by a user or, if start date is missing, the latest.
If multiple choices are still available, then pick random among the multiple options.
For example
on user 2 (below) I want to get only "Economics and Politics" because it has the latest date;
on user 6, only "Electrical and Electronics Engineering" because it is the longer course.
The query I did doesn't work (and I think I am off-track):
(SELECT Q.user_id, min(Q.started_at) as Started_on, max(Q.ended_at) as Completed_on,
q.field_of_study
FROM
(select distinct(user_id),started_at, Ended_at, field_of_study
from educations
) as Q
group by Q.user_id, q.field_of_study )
order by q.user_id
as the result is:
User_id Started_on Completed_on Field_of_studies
2 "2001-01-01" "" "International Economics"
2 "" "2002-01-01" "Economics and Politics"
3 "1992-01-01" "1999-01-01" "Economics, Management of ..."
5 "2012-01-01" "2016-01-01" ""
6 "2005-01-01" "2009-01-01" "Electrical and Electronics Engineering"
6 "2011-01-01" "2012-01-01" "Finance, General"
6 "" "" ""
6 "2010-01-01" "2012-01-01" "Financial Mathematics"
I think this query should do what you need, it relies on calculating the difference in days between ended_at and started_at, and uses 0001-01-01 if the started_at is null (making it a really long interval):
select
educations.user_id,
max(educations.started_at) started_at,
max(educations.ended_at) ended_at,
max(educations.field_of_study) field_of_study
from educations
join (
select
user_id,
max(
ended_at::date
-
coalesce(started_at, '0001-01-01')::date
) max_length
from educations
where (started_at is not null or ended_at is not null)
group by user_id
) x on educations.user_id = x.user_id
and ended_at::date
-
coalesce(started_at, '0001-01-01')::date
= x.max_length
group by educations.user_id
;
Sample SQL Fiddle

EntityFramework Query select (contains) (all villages that have farmers which plant apples)

village(id, list(farmers))
farmer(id, List(fruits));
fruit(id,name).
How would I write a query that selects all villages that have the fruit with ID 23 (e.g. apples)?
It would be easy to write this with 2 queries. How wold you do it with one?
Try
var villages = db.Villages
.Where(v => v.Farmaers.Any(f => f.Fruits.Any(t => t.Id == 23)));

Ordering MongoDB query results in less rows returned

I'm having an issue with ordering in Mongoid (2.0.0.beta.17 and 2.0.2)/MongoDB. Perhaps I'm just not experienced enough with MongoDB, and some of you can help me understand what I'm doing wrong?
The user-level symptoms are:
Queries not sorting correctly by date or ID
User posts are not appearing when they seem like they should be (this is a Twitter-like social networking site for gardeners, and the most recent posts are sometimes not showing up)
irb(main):024:0> Update.all.size
=> 551
Update.ordered.size # (see below for definition)
=> 490
irb(main):010:0> Update.all.select{|u| u.created_at.nil?}
=> []
When I go into the mongo shell, and do:
var cursor = db.updates.find({}, {'_id': 1}).limit(600);
while (cursor.hasNext()) printjson(cursor.next());
I get 454 lines returned.
var cursor = db.updates.find({}, {'_id': 1}).sort({created_at : 1}).limit(600);
while (cursor.hasNext()) printjson(cursor.next());
Also returns 454 lines.
db.updates.find({}).sort({created_at: -1}).limit(1);
returns an update from February 23rd. But I have updates from yesterday in MongoDB.
Any ideas?
My model is:
class Update
include Mongoid::Document
include Mongoid::Timestamps
include Paperclip
field :body
...
index [[ :created_at, Mongo::DESCENDING ]]
...
named_scope :ordered, :order_by => ([[:created_at, :desc]])
...
end
Can you print out the created_at field?
db.updates.find({}, {created_at:1}).sort({created_at: -1}).forEach(printjson)
You may have an issue with how you're saving your date fields. Is the output there a string? an ISODate()?
I didn't figure out WHY, but doing the following seems to fix the problem:
named_scope :ordered, :order_by => ([[:created_at, :desc], [:_id, :desc]])
It seems the secondary ordering helps.
irb(main):022:0> update = Update.order_by([[:created_at, :desc], [:_id, :desc]])
irb(main):025:0> update.count
=> 556
irb(main):026:0> update.first.created_at
=> Sat May 28 02:53:33 -0700 2011
irb(main):027:0> update.second.created_at
=> Fri May 27 17:55:38 -0700 2011
irb(main):028:0> update.first.created_at.class
=> Time
irb(main):029:0> update.second.created_at.class
=> Time
The first of these was being left out of the original query.