I'm quite new to NHibernate and starting to find my way around.
I have a domain model that is somewhat like a tree.
Funds have Periods have Selections have Audits
Now I would like to get all Audits for a specific Fund
Would look like this if I made it in SQL
SELECT A.*
FROM Audit A
JOIN Selection S ON A.fkSelectionID = S.pkID
JOIN Period P ON S.fkPeriodID = P.pkID
JOIN Fund F ON P.fkFundID = F.pkID
WHERE F.pkID = 1
All input appreciated!
Try this
select elements(s.Audits)
from Fund as f inner join Period as p inner join Selection as s
where f = myFundInstance
session.CreateCriteria ( typeof(Audit) )
.CreateCriteria("Selection")
.CreateCriteria("Period")
.CreateCriteria("Fund")
.Add(Restrinction.IdEq(fundId))
using LINQ ....
(from var p in Fund.Periods
let fundPeriodSelections = p.Selections
from var selection in fundPeriodSelections
select selection.Audit).ToList()
... but it does depend on those many-to-many / one-to-many relations being 2-way. Also, I was thinking you may need a mapping table / class in bewteen the Period / Fund table.. but I guess you've already considered it.
Hope the LINQ statemanet above works ... it depends on those mentioend properties, but it's an apraoch we've used on our project that's really cleaned up the code.
Related
I need to get the hierarchy of a project (like shown in transaction CJ20N) in ABAP.
I've found a function module 'GET_PROJECT_HIERARCHY', which delivers me a table like this:
However, I rather need all WBS elements, order numbers, purchase requests and network elements (AUFNR) in this project. Is there a better function module or a next step to the GET_PROJECT_HIERARCHY?
At least what concerns WBS elements you can get them from table PRPS
DATA: lt_prhi TYPE TABLE OF prhi.
CALL FUNCTION 'GET_PROJECT_HIERARCHY'
EXPORTING
i_pronr = '00000113'
TABLES
t_prhi = lt_prhi.
SELECT * FROM prps
INTO TABLE #DATA(lt_prps)
FOR ALL ENTRIES IN lt_prhi
WHERE pspnr = lt_prhi-posnr.
cl_demo_output=>display( lt_prps ).
Thank you Suncatcher, your directed me the right way with the PRPS table and I'll accept your answer.
I finally decided to build a custom query, looking in the tables PRPS, AFVC, AUFK, AFKO and PROJ to get all the data I want using the PSPHI column from GET_PROJECT_HIERARCHY.
I just wanted to post my solution, maybe it helps others too.
SELECT
PROJ~PSPID,
PROJ~POST1 AS PROJ_NAME,
PRPS~POST1 AS WSB_NAME,
AFVC~LTXA1,
AUFK~KTEXT
FROM
PRPS
LEFT JOIN PROJ ON PROJ~PSPNR = PRPS~PSPHI
LEFT JOIN AUFK ON AUFK~PSPEL = PRPS~PSPNR
LEFT JOIN AFKO ON AUFK~AUFNR = AFKO~AUFNR
LEFT JOIN AFVC ON AFKO~AUFPL = AFVC~AUFPL
WHERE
PRPS~PSPHI = '00000136'
INTO TABLE #DATA(LT_RESULT)
Suppose I have a relation map called M with from and to.
I want to find all rooms that can be accessed with 1 intermediate room. Would the following lines be correct?
P = πTo (σFrom='Vestibule'(M))
X = P U πTo (σFrom=P(M))
X = πTo (σFrom= (σFrom='Vestibule'(M)) (M))
In other words, can I use the outcome of a select in another select, or do I have to use a product?
Thanks in advance.
if i understand right, you need a inner query or nested queries into inside
following link explains logic of inner and outer queries it may help you.
http://www.codecandle.com/articles/sql/subqueries/sql-subquery.html
I have this code:
select Users.phoneMac, Users.apMac, Locations.Lon, Locations.Lat
from Locations, Users
inner join (
select u.phoneMac, max(u.strenght) as most
from Users u, Locations l
where u.apMac = l.apMac
group by u.phoneMac
) as ij on ij.phoneMac=Users.phoneMac and Users.strenght = ij.most
where Locations.apMac = Users.apMac;
It worked for me fine but when I added more data to users table this query calculated results from all the data and I wanted to get results just from latest data. So I added timestamp to Users table.
So can you help me fix this code so it first take only data from latest timestamp for every user(users.phoneMac)(there can be more then 1 row of data for same phoneMac) and then do the rest of calculations.
You're already picking the max value of the "strenght" field and joining on that, so why not use the same approach again for your timestamp field? Something like:
SELECT Users.phoneMac, Users.apMac, Locations.Lon, Locations.Lat
FROM Locations
INNER JOIN Users
ON Users.apMac = Locations.apMac
INNER JOIN (
SELECT u.phoneMac, max(u.strenght) AS most
FROM Locations l
INNER JOIN Users u ON u.apMac = l.apMac
GROUP BY u.phoneMac) AS ij
ON ij.phoneMac = Users.phoneMac
AND Users.strenght = ij.most
INNER JOIN (
SELECT u2.phoneMac, max(u2.timestampfield) AS latest
FROM Locations l2
INNER JOIN Users u2 ON u2.apMac = l2.apMac
GROUP BY u2.phoneMac) AS ijk
ON ijk.phoneMac = Users.phoneMac
AND Users.timestampfield = ij.latest;
(By the way, using the old join syntax with comma and the WHERE clause makes it harder to understand the logic, and occasionally makes the logic wrong. The new join syntax with ON is really a lot better.)
I have a database that contains schemas for skus, kits, kit_contents, and checklists. Here is a query for "Give me all the SKUs defined for kitcontent records defined for kit records defined in checklist 1":
SELECT DISTINCT s.* FROM skus s
JOIN kit_contents kc ON kc.sku_id = s.id
JOIN kits k ON k.id = kc.kit_id
JOIN checklists c ON k.checklist_id = 1;
I'm using Django, and I mostly really like the ORM because I can express that query by:
skus = SKU.objects.filter(kitcontent__kit__checklist_id=1).distinct()
which is such a slick way to navigate all those foreign keys. Django's ORM produces basically the same as the SQL written above. The trouble is that it's not clear to me how to get all the SKUs not defined for checklist 1. In the SQL query above, I'd do this by replacing the "=" with "!=". But Django's models don't have a not equals operator. You're supposed to use the exclude() method, which one might guess would look like this:
skus = SKU.objects.filter().exclude(kitcontent__kit__checklist_id=1).distinct()
but Django produces this query, which isn't the same thing:
SELECT distinct s.* FROM skus s
WHERE NOT ((skus.id IN
(SELECT kc.sku_id FROM kit_contents kc
INNER JOIN kits k ON (kc.kit_id = k.id)
WHERE (k.checklist_id = 1 AND kc.sku_id IS NOT NULL))
AND skus.id IS NOT NULL))
(I've cleaned up the query for easier reading and comparison.)
I'm a beginner to the Django ORM, and I'd like to use it when possible. Is there a way to get what I want here?
EDIT:
karthikr gave an answer that doesn't work for the same reason the original ORM .exclude() solution doesn't work: a SKU can be in kit_contents in kits that exist on both checklist_id=1 and checklist_id=2. Using the by-hand query I opened my post with, using "checklist_id = 1" produces 34 results, using "checklist_id = 2" produces 53 results, and the following query produces 26 results:
SELECT DISTINCT s.* FROM skus s
JOIN kit_contents kc ON kc.sku_id = s.id
JOIN kits k ON k.id = kc.kit_id
JOIN checklists c ON k.checklist_id = 1
JOIN kit_contents kc2 ON kc2.sku_id = s.id
JOIN kits k2 ON k2.id = kc2.kit_id
JOIN checklists c2 ON k2.checklist_id = 2;
I think this is one reason why people don't seem to find the .exclude() solution a reasonable replacement for some kind of not_equals filter -- the latter allows you to say, succinctly, exactly what you mean. Presumably the former could also allow the query to be expressed, but I increasingly despair of such a solution being simple.
You could do this - get all the objects for checklist 1, and exclude it from the complete list.
sku_ids = skus.values_list('pk', flat=True)
non_checklist_1 = SKU.objects.exclude(pk__in=sku_ids).distinct()
I know there are a lot of other SO entries that seem like this one, but I haven't found one that actually answers my question so hopefully one of you can either answer it or point me to another SO question that is related.
Basically, I have the following query that returns Venues that have any CheckIns that contain the searched Keyword ("foobar" in this example).
SELECT DISTINCT v.*
FROM "venues" v
INNER JOIN "check_ins" c ON c."venue_id" = v."id"
INNER JOIN "keywordings" ks ON ks."check_in_id" = c."id"
INNER JOIN "keywords" k ON ks."keyword_id" = k."id"
WHERE (k."name" = 'foobar')
I want to SELECT and ORDER BY the count of the matched Keyword for each given Venue. E.g. if there have been 5 CheckIns that have been created, associated with that Keyword, then there should be a returned column (called something like keyword_count) with the value 5 which is sorted.
Ideally this should be done without any queries in the SELECT clause, or preferably none at all.
I've been struggling with this for a while and my mind is just going blank (perhaps it's been too long a day) so some help would be greatly appreciated here.
Thanks in advance!
Sounds like you need something like:
SELECT v.x, v.y, count(*) AS keyword_count
FROM "venues" v
INNER JOIN "check_ins" c ON c."venue_id" = v."id"
INNER JOIN "keywordings" ks ON ks."check_in_id" = c."id"
INNER JOIN "keywords" k ON ks."keyword_id" = k."id"
WHERE (k."name" = 'foobar')
GROUP BY v.x, v.y
ORDER BY 3