OJB ReportQuery join question - sql

Given three tables:
People: {Person_Id, Age}
Accounts: {Account_Id, Person_Id, Account_Type}
Age_Ranges: {Age_Range_Id, Age_Range_Label, Lower_Bound, Upper_Bound}
I'm trying to represent the following SQL query using OJB (without resorting to a QueryBySQL):
select ar.Age_Range_Label, count(a.Account_Id)
from People p
inner join Accounts a on p.Person_Id = a.Person_Id
inner join Age_Ranges ar on p.Age between ar.Lower_bound and ar.Upper_Bound
where a.Account_Type = 'Chequing'
The java code I have so far looks like this:
Criteria criteria = new Criteria();
criteria.addEqualTo("Account_Type", "Chequing");
// join on Age_Ranges based on the person's Age
criteria.???
ReportQueryByCriteria q = QueryFactory.newReportQuery(Accounts.class, criteria);
q.setAttributes(new String[] {"Age_Range_Label", "count(Account_Id)"});
q.addGroupBy("Age_Range_Label");
...
But I'm unsure how to join up with the Age_Ranges table such that I can group on the Age_Range_Label column.
Any insight?

Related

How can I GROUP a LINQ query that includes multiple joined tables with ambiguous column names?

Here is my SQL-Query that I want to convert to LINQ:
SELECT artikel.id, charge.id
from Lots charge
JOIN Product artikel on charge.artikel = artikel.id
JOIN StorageQuantity_TAB lmengeTAB on charge.id = lmengeTAB.charge
JOIN StorageQuantity lmenge on lmengeTAB.id = lmenge.id
GROUP BY artikel.id, charge.id
That´s what I got so far:
var query = (from charge in Lots
join artikel in PartProducts on charge.Artikel equals artikel.Id
join lmengeTAB in StorageQuantity_TABs on charge.Id equals lmengeTAB.Charge
join lmenge in StorageQuantities on lmengeTAB.Id equals lmenge.Id
select new
{
artikel.Id,
chargeID = charge.Id,
}).ToList();
So how do I include that GROUP BY statement?

How to Distinct a sql query but still return all columns

This is my query:
SELECT dbo.Webs.Id, dbo.Webs.Title, dbo.Webs.FullUrl, dbo.Roles.RoleId,
dbo.Roles.Title AS RoleTitle, dbo.UserInfo.tp_Title, dbo.UserInfo.tp_Login
FROM dbo.RoleAssignment
INNER JOIN dbo.Roles ON dbo.RoleAssignment.SiteId = dbo.Roles.SiteId
AND dbo.RoleAssignment.RoleId = dbo.Roles.RoleId
INNER JOIN dbo.Webs ON dbo.Roles.SiteId = dbo.Webs.SiteId
AND dbo.Roles.WebId = dbo.Webs.Id
INNER JOIN dbo.UserInfo ON dbo.RoleAssignment.PrincipalId = dbo.UserInfo.tp_ID
WHERE tp_Title = 'HOBSON, Will';
This database contains all the permissions for the users of all sharepoint sites. I'm trying to create a query that displays all sites the user has access to. Currently it outputs a lot of duplicate information. I only want it to display results that have either a distinct Role Title or a distinct Web id.
So for example, in this query I would only want to see 4 results; 1, 5, 11 and 13.
(all this information is on a local test SharePoint installation that cannot be accessed externally, so the only information I'm giving away here is my name :))
Your query would be much easier to read with table aliases. The direct answer to your question is to use SELECT DISTINCT:
SELECT DISTINCT w.Id, w.Title, w.FullUrl, r.RoleId, r.Title AS RoleTitle,
ui.tp_Title, ui.tp_Login
FROM dbo.RoleAssignment ra INNER JOIN
dbo.Roles r
ON ra.SiteId = r.SiteId AND
ra.RoleId = r.RoleId INNER JOIN
dbo.Webs w
ON r.SiteId = w.SiteId AND
r.WebId = w.Id INNER JOIN
dbo.UserInfo ui
ON ra.PrincipalId = ui.tp_ID
WHERE tp_Title = 'HOBSON, Will';
However, it would be better to find the cause of the duplicates. Often duplicates like this are caused by incomplete join conditions. Fixing the join is the better approach, but sometimes SELECT DISTINCT is necessary.
You can just add a DISTINCT to your query:
SELECT DISTINCT dbo.Webs.Id, dbo.Webs.Title, dbo.Webs.FullUrl, dbo.Roles.RoleId,
dbo.Roles.Title AS RoleTitle, dbo.UserInfo.tp_Title, dbo.UserInfo.tp_Login
FROM dbo.RoleAssignment
INNER JOIN dbo.Roles ON dbo.RoleAssignment.SiteId = dbo.Roles.SiteId
AND dbo.RoleAssignment.RoleId = dbo.Roles.RoleId
INNER JOIN dbo.Webs ON dbo.Roles.SiteId = dbo.Webs.SiteId
AND dbo.Roles.WebId = dbo.Webs.Id
INNER JOIN dbo.UserInfo ON dbo.RoleAssignment.PrincipalId = dbo.UserInfo.tp_ID
WHERE tp_Title = 'HOBSON, Will';

SQL update statement inserting PeopleID into child tables to link data

I need to place the PeopleID in several tables for my new database to link all of the peole information. I have tried several times to write a simple update statement please help. Every time I get close I get AMBIGUOUS COLUMN ERROR I don't know what else to do.
Update CONTRACT
Set PeopleID = B.PeopleID
from People A
Inner join
(
Select PeopleId, F.ContractID
From People A
Inner Join Person PRSN on PRSN.PersonID = A.PersonID
Inner Join DARPA_IMPORT_REAL..persnl oldP on oldP.pl_pid = PRSN.PersonID
Left outer join Contract F on F.ContractID = oldP.kn_254id
) B on A.PeopleID = B.PeopleID
Go
try this
Update CONTRACT Set CONTRACT.PeopleID = B.PeopleID
from People A
Inner join (
Select A.PeopleId, F.ContractID
From People AA
Inner Join Person PRSN on PRSN.PersonID = AA.PersonID
Inner Join DARPA_IMPORT_REAL..persnl oldP on oldP.pl_pid = PRSN.PersonID
Left outer join Contract F on F.ContractID = oldP.kn_254id ) B
on A.PeopleID = B.PeopleID
you were asigning the ´A´ alias twice so I recomend using different aliases always

SQL same postal code

select PERSON.fornavn, PERSON.efternavn, PERSON.postnr, POST.distrikt
from PERSON inner join POST on POST.postnr = PERSON.postnr
inner join MEDLEM on PERSON.personnr = MEDLEM.personnr
inner join FORMAND on MEDLEM.personnr = FORMAND.personnr
group by PERSON.fornavn, PERSON.efternavn, PERSON.postnr, POST.distrikt, FORMAND.afdnr
having FORMAND.afdnr = 3
order by PERSON.fornavn
I need to show the persons that live on the same postal code as the boss for department 3, but the results make no sense.
Fornavn = First name, Formand = boss, Adresse = adress, Afdeling = Department (afdnr = department nr.), Medlem = member
Tables:
PERSON: personnr, fornavn, adresse, postnr
POST: postnr, district
FORMAND: personnr, afdnr
MEDLEM: personnr, afdnr
AFDELING: afdnr, afdname
select PERSON.fornavn, PERSON.efternavn, PERSON.postnr, POST.distrikt
from PERSON inner join POST on POST.postnr = PERSON.postnr
inner join MEDLEM on PERSON.personnr = MEDLEM.personnr
inner join FORMAND on MEDLEM.personnr = FORMAND.personnr
where FORMAND.afdnr = 3
group by PERSON.fornavn, PERSON.efternavn, PERSON.postnr, POST.distrikt, FORMAND.afdnr
order by PERSON.fornavn
try with :
where FORMAND.afdnr = 3
insteed of
having FORMAND.afdnr = 3
If I understand you right ("the persons that live on the same postal code as (the boss for department 3)", and assuming there is only one boss:
select PERSON.fornavn, PERSON.efternavn, PERSON.postnr, POST.distrikt
from PERSON
inner join POST on POST.postnr = PERSON.postnr
where PERSON.postnr = (SELECT pf.postnr
FROM FORMAND f
inner join PERSON pf ON f.personnr = pf.personnr
WHERE f.afdnr = 3)
order by PERSON.fornavn
Note: Your query makes no sense given the description, unless I am missing something. You are joining PERSON through MEDLEM to FORMAND on personnr, which means you will only get the boss's record back (PERSON.personnr = FORMAND.personnr).
Note: If there is more than one boss, you can change the = in the WHERE clause to IN to get all people that live in the same post code as any of the bosses.
In case I read you wrong, and you actually mean "the (persons that live on the same postal code as the boss) for department 3", try:
select p.fornavn, p.efternavn, p.postnr, POST.distrikt
from PERSON p
inner join POST on POST.postnr = p.postnr
inner join MEDLEM on p.personnr = MEDLEM.personnr
inner join FORMAND on MEDLEM.afdnr = FORMAND.afdnr
inner join PERSON pf on FORMAND.personnr = pf.personnr AND pf.postnr = p.postnr
where MEDLEM.afdnr = 3
order by PERSON.fornavn
Note: Again, I believe your join from the question is strange. See above.

Oracle SQL Query (LEFT OUTER JOIN)

I have five tables and i want to get result out of them. Here is what i am doing:
select
person.SERVICE_NO as Service_No, person.CNIC_NO as CNIC, person.NAME as NAME , card.CPLC_SERIAL_NO as Card_Number,
child_dc.NAME as Child_DC, root_dc.NAME as Root_DC, person.OU as OU, person.EMAIL as Email
from
person,card,person_card,child_dc,root_dc
where
person_card.PERSON_ID = person.ID
and
person_card.CARD_ID = card.ID
and
person.CHILD_DC_ID = child_dc.ID
and
root_dc.ID = child_dc.ID;
This query give redundant values, (not if i place a distinct with it). I was thinking of doing it with left out join; which means that i would be LEFT OUTER JOINING with 5 tables. How would i do this. If anyone has more optimized query or any other idea, that would be great.
Query Updated:
select distinct
person.SERVICE_NO as Service_No,
person.CNIC_NO as CNIC, person.NAME as NAME ,
card.CPLC_SERIAL_NO as Card_Number,
child_dc.NAME as Child_DC,
root_dc.NAME as Root_DC, person.OU as OU,
person.EMAIL as Email
from
person_card inner join person
on person_card.PERSON_ID = person.ID
inner join card
on person_card.CARD_ID = card.ID
left outer join child_dc
on person.CHILD_DC_ID = child_dc.ID
left outer join root_dc
on child_dc.ID = root_dc.ID;