how to select some fields from tables in CakePHP - sql

I tried to make query in cakephp with joins, but i want to get more fileds,
Here the query sql :
SELECT
sd.debut,
sd.fin,
fr.id,
f.id as formtaion_id,
s.id as seance_id,
fr.nom as formateur,
f.nom as formation,
r.title,
f.module,
sd.module as partie ,
f.couleur
FROM seances s
INNER JOIN formations f on s.formation_id = f.id
INNER JOIN seances_dates sd on s.id = sd.seance_id
INNER JOIN salles sa on sa.id = s.salle_id
INNER JOIN regions r on r.id = sa.region_id
INNER JOIN presence_formateurs pf ON pf.seance_id = s.id
INNER JOIN formateurs fr ON fr.id = pf.formateur_id
WHERE fr.archived = 0
AND fr.deleted is null
AND (
(sd.debut between '".$from."' and '".$to."')
OR
(sd.fin between '".$from."' and '".$to."')
)
GROUP BY sd.id
ORDER BY sd.debut
please help me to make that query in cakephp :
ClassRegistry::init('seance')->find('all'....

You should read very carefully
1. How to Retrieve data in CakePHP and
2. How to make Relation with different table

Related

Trouble with aggregate in Where clause, Selecting Max(x) When Max(x) != 3

I am trying to reconfigure the below sql to only pull records when the Max(Field) != 3 but keep getting an error (detailed) below.
This is the code before adding the Where Max(field) != 3
SELECT P.Code,
MAX(PW.v1) AS V1
FROM SW
INNER JOIN S ON SW.S_Id = S.Id
INNER JOIN PW ON SW.PW_Id = PW.Id
INNER JOIN PON S.P_Id = P.id
WHERE S.P_Id = P.id
GROUP BY P.Code
My Attempt
SELECT P.Code,
MAX(PW.v1) AS V1
FROM SW
INNER JOIN S ON SW.S_Id = S.Id
INNER JOIN PW ON SW.PW_Id = PW.Id
INNER JOIN PON S.P_Id = P.id
WHERE S.P_Id = P.id
AND (SELECT MAX(PW.v1)
FROM SW AS SW2
WHERE SW.PWId = SW2.PW_Id) != 3
GROUP BY P.Code
This is the error I get and not sure what to do:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Traditional method of filtering on results of an aggregate can be achieved by using HAVING clause. I also removed the unnecessary WHERE clause as you already joined those 2 tables on that column. Here is the query:
SELECT P.Code
,MAX(PW.v1) AS V1
FROM SW
INNER JOIN S
ON SW.S_Id = S.Id
INNER JOIN PW
ON SW.PW_Id = PW.Id
INNER JOIN P
ON S.P_Id = P.id
GROUP BY P.Code
HAVING MAX(PW.v1)!=3;

How to retrieve count of records in SELECT statement

I am trying to retrieve the right count of records to mitigate an issue I am having. The below query returns 327 records from my database:
SELECT DISTINCT COUNT(at.someid) AS CountOfStudentsInTable FROM tblJobSkillAssessment AS at
INNER JOIN tblJobSkills j ON j.jobskillid = at.skillid
LEFT JOIN tblStudentPersonal sp ON sp.someid2 = at.someid
INNER JOIN tblStudentSchool ss ON ss.monsterid = at.someid
INNER JOIN tblSchools s ON s.schoolid = ss.schoolid
INNER JOIN tblSchoolDistricts sd ON sd.schoolid = s.schoolid
INNER JOIN tblDistricts d ON d.districtid = sd.districtid
INNER JOIN tblCountySchools cs ON cs.schoolid = s.schoolid
INNER JOIN tblCounties cty ON cty.countyid = cs.countyid
INNER JOIN tblRegionUserRegionGroups rurg ON rurg.districtid = d.districtid
INNER JOIN tblGroups g ON g.groupid = rurg.groupid
WHERE ss.graduationyear IN (SELECT Items FROM FN_Split(#gradyears, ',')) AND sp.optin = 'Yes' AND g.groupname = #groupname
Where I run into trouble is trying to reconcile that with the below query. One is for showing just a count of all the particular students the other is showing pertinent information for a set of students as needed but the total needs to be the same and it is not. The below query return 333 students - the reason is because the school the student goes to is in two separate counties and it counts that student twice. I can't figure out how to fix this.
SELECT DISTINCT #TableName AS TableName, d.district AS LocationName, cty.county AS County, COUNT(DISTINCT cc.monsterid) AS CountOfStudents, d.IRN AS IRN FROM tblJobSkillAssessment AS cc
INNER JOIN tblJobSkills AS c ON c.jobskillid = cc.skillid
INNER JOIN tblStudentPersonal sp ON sp.monsterid = cc.monsterid
INNER JOIN tblStudentSchool ss ON ss.monsterid = cc.monsterid
INNER JOIN tblSchools s ON s.schoolid = ss.schoolid
INNER JOIN tblSchoolDistricts sd ON sd.schoolid = s.schoolid
INNER JOIN tblDistricts d ON d.districtid = sd.districtid
INNER JOIN tblCountySchools cs ON cs.schoolid = s.schoolid
INNER JOIN tblCounties cty ON cty.countyid = cs.countyid
INNER JOIN tblRegionUserRegionGroups rurg ON rurg.districtid = d.districtid
INNER JOIN tblGroups g ON g.groupid = rurg.groupid
WHERE ss.graduationyear IN (SELECT Items FROM FN_Split(#gradyears, ',')) AND sp.optin = 'Yes' AND g.groupname = #groupname
GROUP BY cty.county, d.IRN, d.district
ORDER BY LocationName ASC
If you just want the count, then perhaps count(distinct) will solve the problem:
select count(distinct at.someid)
I don't see what at.someid refers to, so perhaps:
select count(distinct cc.monsterid)

Joining two SQL statements in postgres

How would I join both of these statements together so it comes up as one? The two counts are being done separately as it is coming from two different tables.
SELECT ril.invoice_label_id, ril.invoice_label, ril.invoice_label_code,
fp.price as fee, count(*) as ct, l.link_id
FROM consultation_chl c
INNER JOIN link_service_pct_location l on l.link_id=c.link_id
INNER JOIN medication m ON c.consult_id=m.consult_id
INNER JOIN ref_invoice_label ril ON m.formulary_id = ril.formulary_id
INNER JOIN pharmacy ph ON ph.pharmacy_id = l.id AND l.location_type_id = 3
INNER JOIN formulary f ON f.formulary_id=m.formulary_id
INNER JOIN formulary_price fp ON fp.formulary_id=f.formulary_id
WHERE l.pct_id = 1425
AND l.service_id = 4
AND c.invoice_period = '2015-04-30'
AND ril.section_id=2
AND ril.invoice_label_code in ('MEDTABS','MEDCAPS','DOXYCAPS','DOXYTABS')
AND fp.valid_from <= c.consult_date
AND (fp.valid_to >= c.consult_date OR fp.valid_to IS NULL) GROUP BY ril.invoice_label_id, ril.invoice_label, ril.invoice_label_code,
fp.price, l.link_id
SELECT ril.invoice_label_id, ril.invoice_label, ril.invoice_label_code,
ricf.fee, count(*) as ct, l.link_id
FROM consultation_chl c
INNER JOIN link_service_pct_location l on l.link_id=c.link_id
INNER JOIN medication m ON c.consult_id=m.consult_id
INNER JOIN ref_invoice_label ril ON m.formulary_id = ril.formulary_id
INNER JOIN pharmacy ph ON ph.pharmacy_id = l.id AND l.location_type_id = 3
INNER JOIN formulary f ON f.formulary_id=m.formulary_id
INNER JOIN formulary_price fp ON fp.formulary_id=f.formulary_id
INNER JOIN ref_invoice_consult_fee ricf ON ricf.invoice_label_id = ril.invoice_label_id
WHERE l.pct_id = 1425
AND l.service_id = 4
AND c.invoice_period = '2015-04-30'
AND ril.section_id=2
AND ril.invoice_label_code in ('MEDSUSP15-25','MEDSUSP16-35','MEDSUSP26-35','MEDSUSP36-45','MEDSUSP45+')
AND fp.valid_from <= c.consult_date
AND (fp.valid_to >= c.consult_date OR fp.valid_to IS NULL) GROUP BY ril.invoice_label_id, ril.invoice_label, ril.invoice_label_code,
ricf.fee, l.link_id
You should take a look at the UNION operator.
SQL UNION Operator at W3schools.com
As your selected columns each have same names
you should be able to concatenate the selects with a "UNION" keyword in between.

sql query sum bringing back different results

I have the following two queries below, the Total is coming back different, but I am adding the sums in each of the query the same way. Why is the total coming back different?
select [Total Children] = (SUM(demo.NumberOfPreschoolers) + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants)),
County = co.Description
from ClassroomDemographics as demo
inner join Classrooms as c on demo.Classroom_Id = c.Id
inner join Sites as s on c.Site_Id = s.Id
inner join Profiles as p on s.Profile_Id = p.Id
inner join Dictionary.Counties as co on p.County_Id = co.Id
where co.Description = 'MyCounty'
Group By co.Description
select [Number Of DLL Children] = SUM(cd.NumberOfLanguageSpeakers),
[Total Children] = (SUM(demo.NumberOfPreschoolers) + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants)),
County = co.Description
from ClassroomDLL as cd
inner join Classrooms as c on cd.Classroom_Id = c.Id
inner join Sites as s on c.Site_Id = s.Id
inner join Profiles as p on s.Profile_Id = p.Id
inner join Dictionary.Counties as co on p.County_Id = co.Id
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
where co.Description = 'MyCounty'
Group by co.Description
Just a quick glance over the two querties, I would presume that:
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
in the second query is excluding results that are in the first query, therefor the aggregated values will be different.
Your join to the Classrooms table is joining with an extra table in the 2nd query.
Query 1:
from ClassroomDemographics as demo
inner join Classrooms as c on demo.Classroom_Id = c.Id
Query 2:
from ClassroomDLL as cd
inner join Classrooms as c on cd.Classroom_Id = c.Id
...
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
My bet is that the ClassroomDLL table has less data in it, or has rows with a null for one of the join criteria columns, either of which could exclude rows from the results and throw your aggregate totals off.

Joining 3 tables into 1 - SQL

I'm trying to LEFT OUTER JOIN two tables and then INNER JOIN another table together in Access 2007.
SELECT RestaurantName,
StreetAddress,
City,
State,
Zip,
RestaurantWebsite,
MenuLink,
RestaurantTimes,
PhoneNumber,
PictureTitle,
PictureTitle3,
PictureTitle3,
PictureTitle4,
PictureTitle,
TagType
FROM Restaurants r
LEFT OUTER JOIN RestaurantPictures rp ON r.ID = rp.ID
INNER JOIN RestaurantTag rt ON r.TagID = t.TagID
I keep getting a Syntax Error in my query expression. "INNER JOIN RestaurantTag rt ON rt.TagID = r.TagID"
I have a Corresponding TagID in both the Restaurant and RestaurantTag tables. I can't seem figure out why I'm getting this error.I can successfully JOIN the first two tables but the third table is the one giving me trouble. Any suggestions would be greatly appreciated!
Without seeing a table definition this is a guess, but you have an error:
INNER JOIN RestaurantTag rt ON r.TagID = t.TagID should be INNER JOIN RestaurantTag rt ON r.TagID = rt.TagID
Try to put the join expression in ()
(Restaurants r LEFT OUTER JOIN RestaurantPictures rp ON r.ID = rp.ID)
INNER JOIN RestaurantTag rt ON r.TagID = t.TagID
SELECT
RestaurantName, StreetAddress, City, State, Zip, RestaurantWebsite,
MenuLink, RestaurantTimes, PhoneNumber, PictureTitle, PictureTitle3,
PictureTitle3, PictureTitle4, PictureTitle, TagType
FROM
Restaurants r LEFT OUTER JOIN RestaurantPictures rp ON r.ID = rp.ID
INNER JOIN RestaurantTag rt ON r.TagID = rt.TagID