This works...
$sql30 = "SELECT * FROM statuses LEFT JOIN friends ON statuses.userID=friends.lowerFriendID WHERE friends.lowerFriendID != :user_ida
UNION SELECT * FROM statuses RIGHT JOIN friends ON statuses.userID=friends.higherFriendID WHERE friends.higherFriendID != :user_idb ORDER BY userID DESC";
But I don't need to sort by userID, this does not work...
$sql30 = "SELECT * FROM statuses LEFT JOIN friends ON statuses.userID=friends.lowerFriendID WHERE friends.lowerFriendID != :user_ida
UNION SELECT * FROM statuses RIGHT JOIN friends ON statuses.userID=friends.higherFriendID WHERE friends.higherFriendID != :user_idb ORDER BY id DESC";
nor does this...
$sql30 = "SELECT * FROM statuses LEFT JOIN friends ON statuses.userID=friends.lowerFriendID WHERE friends.lowerFriendID != :user_ida
UNION SELECT * FROM statuses RIGHT JOIN friends ON statuses.userID=friends.higherFriendID WHERE friends.higherFriendID != :user_idb ORDER BY `id` DESC";
nor does this...
$sql30 = "SELECT * FROM statuses LEFT JOIN friends ON statuses.userID=friends.lowerFriendID WHERE friends.lowerFriendID != :user_ida
UNION SELECT * FROM statuses RIGHT JOIN friends ON statuses.userID=friends.higherFriendID WHERE friends.higherFriendID != :user_idb ORDER BY timestamp DESC";
I need to order by either "id" or "timestamp" DESC so that the most recent post shows first. Your help is much appreciated. Thank You!
Related
I'd like to isplay the name, address and phone number for the clients who has not made any reservations for the past two months.
The tables are :
CLIENT (
ClientNo,
Name,
Sex,
DOB,
Address,
Phone,
Email,
Occupation,
MaritalStatus,
Spouse,
Anniversary
)
RESERVATION
ResNo,
ResDate,
NoOfGuests,
StartDate,
EndDate,
ClientNo,
Status
)
I tried:
SELECT Name, Address, Phone, ResNo
FROM Client C, Reservation R
WHERE Date_Column >= ResDate R(MONTH, -3, GETDATE())
ORDER BY Name DESC
You need an outer join, with all conditions in the join, and then filter out all successful joins:
SELECT C.*
FROM Client C
LEFT JOIN Reservation R ON C.ID = R.ID -- outer join
AND R.ResDate >= add_months(TRUNC(SYSDATE) + 1, 2) -- condition in join
WHERE R.ID IS NULL -- only return missed joins
ORDER BY Name ASC
You want the missed joins - ones where there are no reservations at all given the conditions. Such missed joins have all-nulls in the joined table's columns, so filtering on (one of) those columns being null will return clients who don't have any such reservations.
Try to make your where condition like
ResDate >= add_months(TRUNC(SYSDATE) + 1, 2)
Also you are missing the JOIN for your table. So you need to put the JOIN like
SELECT C.Name, C.Address, C.Phone, R.ResNo, R.ResDate
FROM Client C INNER JOIN Reservation R ON C.ID = R.ID --Change the column for Joing as per your table structure
WHERE R.ResDate >= add_months(TRUNC(SYSDATE) + 1, 2))
ORDER BY Name ASC;
Tables needed -
Habits(conditionId, name)
Patient(patientId, name, gender, DoB, address, state,postcode, homePhone, businessPhone, maritalStatus, occupation, duration,unit, race, registrationDate , GPNo, NaturopathNo)
PatientMetabolic (functionNo, patientId, score)
The Question Is -
Question - Display the details of the patient (i.e. Name, Gender, Address, Postcode, DOB) who smokes and has the highest (most severe) total of metabolic functions.
(conditionid for smoke is H1 in Habit table)
(metabolic function are in patientbetabolic table functionNo)
(To find the highest most severe total of metabolic function we need to create a sum of score which tells who has the most metabolic functions)
My query -
SELECT *
FROM patient
where patientid IN (SELECT patientid,SUM(score) as totalscore
from PATIENTMETABOLIC
where patientid IN (SELECT patientid
from patienthabit
where conditionid = 'H1')
group by patientid
order by totalscore desc);
Error:
ORA-00907: missing right parenthesis
An alternative way to do this is by using joins.
select * from (select p.patientid,p.name,sum(pm.score) as total from patient p join patienthabit ph on p.patientid = ph.patientid
and ph.conditionid = 'H1' Left join patientmetabolic pm
on p.patientid = pm.patientid group by p.patientid,p.name order by 3 desc) where ROWNUM = 1;
Try this:
SELECT *
FROM PATIENT
WHERE PATIENTID = (SELECT PATIENTID
FROM (SELECT patientid, SUM(score)
from PATIENTMETABOLIC
where patientid IN (SELECT patientid
from patienthabit
where conditionid = 'H1')
group by patientid
order by SUM(score) desc)
WHERE ROWNUM = 1);
SQLFiddle here
Share and enjoy.
Since the first inner query returns patientid and totalscore, you can't use it as a list against IN operator.
The result of this query might be similar to that of your query:
SELECT p.*
FROM patient p JOIN
patienthabit ph ON p.patientid=ph.patientid
WHERE ph.conditionid='H1'
I know we can get distinct on one column using following query:
I know we can get distinct on one column using following query:
SELECT *
FROM (SELECT A, B, C,
ROW_NUMBER() OVER (PARTITION BY B ORDER BY A) AS RowNumber
FROM MyTable
WHERE B LIKE 'FOO%') AS a
WHERE a.RowNumber = 1
I have used similar sql query in my case where i am joining multiple tables but my project is in mvc4 and i need linq to entity equivalent of the same. Here is my code:
select * from
(
select fp.URN_No,
ROW_NUMBER() OVER
(PARTITION BY pdh.ChangedOn ORDER BY fp.CreatedOn)
as num,
fp.CreatedOn, pdh.FarmersName, pdh.ChangedOn, cdh.Address1, cdh.State, ich.TypeOfCertificate, ich.IdentityNumber, bdh.bankType, bdh.bankName,
pidh.DistrictId, pidh.PacsRegistrationNumber, idh.IncomeLevel, idh.GrossAnnualIncome
from MST_FarmerProfile as fp inner join PersonalDetailsHistories as pdh on fp.personDetails_Id = pdh.PersonalDetails_Id
inner join ContactDetailsHistories as cdh on fp.contactDetails_Id = cdh.ContactDetails_Id
inner join IdentityCertificateHistories as ich on fp.IdentityCertificate_Id = ich.IdentityCertificate_Id
inner join BankDetailsHistories as bdh on fp.BankDetails_Id = bdh.BankDetails_Id
left join PacInsuranceDataHistories as pidh on fp.PacsInsuranceData_Id = pidh.PacsInsuranceData_Id
left join IncomeDetailsHistories as idh on fp.IncomeDetails_Id = idh.IncomeDetails_Id
where URN_No in(
select distinct MST_FarmerProfile_URN_No from PersonalDetailsHistories where MST_FarmerProfile_URN_No in(
select URN_No from MST_FarmerProfile where (CreatedOn>=#fromDate and CreatedOn<= #toDate and Status='Active')))
)a where a.num=1
Use this linq query after getting result from sql. p.ID is be your desire distinct column name
List<Person> distinctRecords = YourResultList
.GroupBy(p => new { p.ID})
.Select(g => g.First())
.ToList();
I am trying to perform a right outer join on two liferay tables — users_ and expandovalue — to get a result set.
When I did the following query on all the users, I got desired result.
SELECT USER_.FIRSTNAME
, USER_.LASTNAME
, USER_.EMAILADDRESS
, USER_.JOBTITLE
, expandovalue.data_
from expandovalue
right outer join
user_ on expandovalue.classpk = user_.userid
and expandovalue.columnid =35695;
When tried to do the same for a set of users (part of a user group) it errored out. Query below:
SELECT USER_.FIRSTNAME
, USER_.LASTNAME
, USER_.EMAILADDRESS
, USER_.JOBTITLE
, expandovalue.data_
from expandovalue
right outer join
user_ on expandovalue.classpk
in (select userid
from user_
where userid
in ( select userid
from users_usergroups
where usergroupid = 40073
) and status =0) = user_.userid in
(select userid
from user_
where userid
in ( select userid
from users_usergroups
where usergroupid = 40073
)
and status =0
) and expandovalue.columnid =35695
Here's the subquery which gives the userids of people in a particular usergroup.
(select userid
from user_
where userid in
( select userid
from users_usergroups
where usergroupid = 40073)
and status =0
)
Am I going in a completely incorrect direction? Please advise.
Try this query:
SELECT USER_.FIRSTNAME,
USER_.LASTNAME,
USER_.EMAILADDRESS,
USER_.JOBTITLE,
expandovalue.data_
from expandovalue
right outer join user_
on expandovalue.classpk IN
(select userid
from user_
where userid in (select userid
from users_usergroups
where usergroupid = 40073) and
status = 0) AND
user_.userid in (select userid
from user_
where userid in (select userid
from users_usergroups
where usergroupid = 40073) AND
status =0) AND
expandovalue.columnid = 35695
There was an "AND" missing after the end of the first subquery.
How to write the query using linq?
SELECT
(SELECT SUM([quantity] * [price_usd])
FROM [vi].[dbo].[SALES]
WHERE [type_id] =1 and [date_id] in(200701,200702,200703,200704,200705,200706,200707,200708,200709,200710,200711,200712)) as y2007,
(SELECT SUM([quantity] * [price_usd])
FROM [vi].[dbo].[SALES]
WHERE [type_id] =1 and [date_id] in(200801,200802,200803,200804,200805,200806,200807,200808,200809,200810,200811,200812)) as y2008,
(SELECT SUM([quantity] * [price_usd])
FROM [vi].[dbo].[SALES]
WHERE [type_id] =1 and [date_id] in(200901,200902,200903,200904,200905,200906,200907,200908,200909,200910,200911,200912)) as y2009
What about that kind of query:
From s in Sales
where s.type_id == 1
group s by s.date_id.ToString().Substring(0,4) into g
select New { Year = g.Key, Sum = g.Sum(i => i.quantity * i.price_usd) }
I have no opportunity to try it on iPad, but the idea is clear and it should work.