Distinct is not working in sql - mysql

Select Distinct _Ad.ad_id,_Ad.Ad_Name,ID.Image_Path,VM.year,VD.Vehicle_Transformation,VD.Vehicle_Fuel_Type,VD.Vehicle_Millege
from _Ad
inner join _Image_Details ID
on ID.ad_id = _Ad.ad_id
inner join _Vehicle_Model VM
on VM.vehicle_model_id = _AD.vehicle_model_id
inner join _Vehicle_Details VD
on _ad.ad_id=VD.ad_id
inner join _Vehicle_Make VMA
on VM.vehicle_make_id=VMA.vehicle_make_id
where
VMA.Vehicle_Make='' OR
VM.Vehicle_Model='' OR
VD.Vehicle_Fuel_Type='' OR
VD.Vehicle_Seats='' OR
_Ad.price>'0' OR _Ad.price<'8888888'
i am using this query but its showing 4 records not 1. I need only single record across 1 ad_id. Kindly help.

Instead of SELECT DISTINCT, try using SELECT TOP 1. So your code would look like
SELECT TOP 1 a.ad_id,
a.Ad_Name,
b.Image_Path,
c.year,
d.Vehicle_Transformation,
d.Vehicle_Fuel_Type,
d.Vehicle_Millege
FROM _Ad a, _Image_Details b,
_Vehicle_Model c,
_Vehicle_Details d,
_Vehicle_Make e
WHERE b.ad_id = a.ad_id
AND c.vehicle_model_id = a.vehicle_model_id
AND a.ad_id = d.ad_id
AND c.vehicle_make_id = e.vehicle_make_id
e.Vehicle_Make='' OR
c.Vehicle_Model='' OR
d.Vehicle_Fuel_Type='' OR
d.Vehicle_Seats='' OR
a.price>'0' OR a.price<'8888888'
If you want one of each a.ad_id as you say, you will need to get rid of the b.Image_Path. So it would look like this:
SELECT DISTINCT a.ad_id,
a.Ad_Name,
c.year,
d.Vehicle_Transformation,
d.Vehicle_Fuel_Type,
d.Vehicle_Millege
FROM _Ad a, _Image_Details b,
_Vehicle_Model c,
_Vehicle_Details d,
_Vehicle_Make e
WHERE b.ad_id = a.ad_id
AND c.vehicle_model_id = a.vehicle_model_id
AND a.ad_id = d.ad_id
AND c.vehicle_make_id = e.vehicle_make_id
e.Vehicle_Make='' OR
c.Vehicle_Model='' OR
d.Vehicle_Fuel_Type='' OR
d.Vehicle_Seats='' OR
a.price>'0' OR a.price<'8888888'

Related

How not to include the ID in the result of the query when using Left Join

Im trying to populate my listview with data from three tables stu_tblbasicinfo,stu_tblfam and stu_tbleducbackground
they all have the same id which is TagID
Select A.TagID , A.Surname, A.Firstname, A.Middlename, A.Course, A.Year, B.ZipCode,
B.Province, B.Municipality, B.Barangay, A.ContactNo, A.EmailAddress, A.Birthdate,
A.Age, A.Birthplace, A.Religion, A.Gender, A.CivilStatus, A.Spouse, C.Mothersname,
C.M_occupation, C.M_Number, C.Fathersname, C.F_Occupation, C.F_Number,
C.GuardianName, C.G_Occupation, C.G_Number, D.Elementary, D.E_Years, D.JuniorHigh,
D.J_Years, Seniorhigh, D.S_Years
from stu_tblbasicinfo As A
left join stu_tblzipcode As B
on A.Barangay = B.Barangay
inner join stu_tblfam As C
ON A.TagID = C.TagID
inner join stu_tbleducbackground As D
ON A.TagID = D.TagID
My code works but the problem is it displays 3 TagID.
The result is like this
TagID(studentinfo table),Surname,Firstname,Middlename,TagID(familyBackground table),MName,FName,Gname,TagID(EducBackground table),Elementary,E_years,JuniorHigh,J_Years,SeniorHigh,S_Years
How can I make the result in this format?
TagID,Surname,Firstname,Middlename,Mname,Fname,Gname,Elementary,E.Years,JuniorHigh,J_Years,SeniorHigh,S_Years
Just this:
Select
A.TagID ,
A.Surname,
A.Firstname,
A.Middlename,
C.Mothersname,
C.Fathersname,
C.GuardianName,
D.Elementary,
D.E_Years,
D.JuniorHigh,
D.J_Years,
D.Seniorhigh,
D.S_Years
from stu_tblbasicinfo As A
left join stu_tblzipcode As B
on A.Barangay = B.Barangay
inner join stu_tblfam As C
ON A.TagID = C.TagID
inner join stu_tbleducbackground As D
ON A.TagID = D.TagID

How can I get the Unique ID and MAX(date)?

Could someone help me please. I don't know what wrong with the query I think it's is right..
Item Table
Loan Table
Pawner Table
Loan_assignment Table
This is my query for getting the unique ID and MAX(date)
SELECT distinct p.pawner_id, c.item_name, c.description, l.net_proceeds,
max(DATE_FORMAT(a.date_loan_granted, '%d-%b-%Y')) as date
from pawner p, loan l, collateral c, loan_assignment a, pawnshop b
WHERE b.pawnshop_id = a.pawnshop_id AND p.pawner_id = a.pawner_id
AND l.loan_id = a.loan_id AND a.item_id = c.item_id
AND b.pawnshop_id = 1 group by p.pawner_id;
The result of the above^ query is this:
The result that I like to happen is this below
Based on comments, if you want the highest date_loan_granted value for each pawner_id, and not each (pawner_id, item_id) you might instead want:
select m.pawner_id,
i.item_name,
i.description,
l.net_amount,
date_format(m.date_loan_granted, '%d-%b-%Y') as date_loan_granted
from (select pawner_id,
max(loan_id) as loan_id,
max(date_loan_granted) as date_loan_granted
from loan_assignment
group by pawner_id) m
join loan_assignment la
on m.pawner_id = la.pawner_id
and m.loan_id = la.loan_id
and m.date_loan_granted = l.date_loan_granted
join loan l
on m.loan_id = l.loan_id
join item i
on la.item_id = i.item_id
Try this:
SELECT distinct p.pawner_id, c.item_name, c.description, l.net_proceeds,
DATE_FORMAT(MAX(a.date_loan_granted), '%d-%b-%Y') as date
from pawner p, loan l, collateral c, loan_assignment a, pawnshop b
WHERE b.pawnshop_id = a.pawnshop_id AND p.pawner_id = a.pawner_id
AND l.loan_id = a.loan_id AND a.item_id = c.item_id
AND b.pawnshop_id = 1 group by p.pawner_id;

Outer Join with Multiple Inner Join In a Single Query

I want to use Outer Join with inner Join in a single query
Query:
select d.unit_name, a.tour_code, a.hub_code, b.name, c.pp_no, c.dte_of_expiry
from bkng_mst a , bkng_pax b, bkng_cust c, unit_mst d
where a.bkng_id = b.bkng_id
and b.unit_cde = d.unit_cde
and a.unit_cde = d.unit_cde
and b.cust_id = c.cust_id
and a.bkng_stat = 'CNF'
and b.bkng_pax_cancel_flg = 'N'
and a.bkng_id = 'XXXX'
Use Outer Join from Table pax_dtl pd on a.bkng_id=pd.bkng_id along with above query
UPDATED :
I think that, taking into account the information provided in your comments, the following query should be helpful:
SELECT DISTINCT
d.unit_name, a.tour_code, a.hub_code, b.name, c.pp_no, c.dte_of_expiry,
pd.bkng_id, pd.unit_name, pd.tour_code, pd.pax_name, pd.pnr_no, pd.fare_base, pd.is_block, pd.is_system
FROM
bkng_mst a
INNER JOIN bkng_pax b
ON a.bkng_id = b.bkng_id
INNER JOIN bkng_cust c
ON b.cust_id = c.cust_id
INNER JOIN unit_mst d
ON b.unit_cde = d.unit_cde
AND a.unit_cde = d.unit_cde
LEFT OUTER JOIN pax_dtl pd
ON a.bkng_id=pd.bkng_id
WHERE
a.bkng_stat = 'CNF'
AND b.bkng_pax_cancel_flg = 'N'
AND a.bkng_id = 'XXXX'
Because of 1 to many relation between bkng_mst and pax_dtl tables, the columns d.unit_name, a.tour_code, a.hub_code, b.name, c.pp_no, c.dte_of_expiry from above query will repeat only if for 1 particular bkng_id value there will be at least one different value among the columns pd.bkng_id, pd.unit_name, pd.tour_code, pd.pax_name, pd.pnr_no, pd.fare_base, pd.is_block.
I hope it might help you, but in case of any doubts please write.

Left Join to another select statement

I have an INNER JOIN query that runs perfectly fine by itself, which I want to LEFT JOIN to a another query/SELECT statement which contains WHERE clause.
I'm not able to join both the queries. It should link wt.tkinit = t.tkinit
Can you please suggest what I'm missing.
SELECT c.clnum, m.mmatter, ot.tkinit AS 'otkinit', wt.tkinit AS 'wtikint', t.tkrt01,
SUM(mt.mthrwkdb) AS 'whrs2010',
FROM client c, matter m, timekeep ot, timekeep wt, mattimhs mt, periodt p, timerate t
WHERE c.clnum = m.mclient
AND m.mmatter = mt.mtmatter
GROUP BY c.clnum, m.mmatter, ot.tkinit, wt.tkinit
SELECT t.tkinit, t.tkrt01
FROM timerate t
INNER JOIN (
SELECT tkinit, max(tkeffdate) as max_effdate
FROM timerate WHERE DATEPART(year, tkeffdate) = '2012'
GROUP BY tkinit) mt ON mt.tkinit = t.tkinit AND mt.max_effdate = t.tkeffdate
Did you try the following:
SELECT fields from (SELECT1) s1
LEFT JOIN
(SELECT2) s2
on s1.wt.tkinit = s2.t.tkinit.
Don't forget to get normal name for wt.tkinit and t.tkinit in their selects like wt_tkinit and t_tkinit because you can't do double aliases.
SELECT temp2.tkinit, temp2.tkrt01, temp.clnum, temp.mmatter, temp.otkinit, temp.tkrt01, temp.whrs2010
FROM
( SELECT c.clnum, m.mmatter, ot.tkinit AS 'otkinit', wt.tkinit AS 'wtikint', t.tkrt01,
SUM(mt.mthrwkdb) AS 'whrs2010',
FROM client c, matter m, timekeep ot, timekeep wt, mattimhs mt, periodt p, timerate t
WHERE c.clnum = m.mclient
AND m.mmatter = mt.mtmatter
GROUP BY c.clnum, m.mmatter, ot.tkinit, wt.tkinit ) temp
LEFT JOIN
SELECT t.tkinit, t.tkrt01
FROM timerate t
INNER JOIN (
SELECT tkinit, max(tkeffdate) as max_effdate
FROM timerate WHERE DATEPART(year, tkeffdate) = '2012'
GROUP BY tkinit) mt ON mt.tkinit = t.tkinit AND mt.max_effdate = t.tkeffdate ) temp2
ON temp.wtikint = temp2.tkinit
Be careful of your field names, but this may be a start

Trying to add one last SUM() column to my query in SQL Server 2008

I have the first query which is producing correct results. What I need is I need to add the sum of values as a last column grouped by surveyid. I can't insert Sum(c.value) into the first query because it is an aggregate function. I have the correct query as my second query below. I know there's pivot functionality but not sure if it can be used here. I do realize that there will be repetition but that's okay.
'first query
SELECT
A.PATIENTID, B.STUDENTNUMBER, c.surveyid,
convert(varchar, A.CreatedDate, 107),
C.QuestionID, C.Value, D.Question
FROM
dbo.Survey A, dbo.Patient B, [dbo].[SurveyQuestionAnswer] C, [dbo].[LookupQuestions] D
WHERE
A.PATIENTID = B.ID
and c.SurveyID = A.ID
and c.QuestionID = d.ID
and c.questionid <> 10
ORDER BY
A.PATIENTID
'second query
select
c.surveyid,SUM(c.value) as scores
from
dbo.SurveyQuestionAnswer c
group by
c.SurveyID
order by
SurveyID '---not important
You can use SUM if you add the OVER clause. In this case:
SELECT
A.PATIENTID, B.STUDENTNUMBER, c.surveyid,
convert(varchar, A.CreatedDate, 107),
C.QuestionID, C.Value, D.Question,
SUM(c.Value) OVER(PARTITION BY c.surveyid) scores
FROM
dbo.Survey A
INNER JOIN dbo.Patient B
ON A.PATIENTID = B.ID
INNER JOIN [dbo].[SurveyQuestionAnswer] C
ON c.SurveyID = A.ID
INNER JOIN [dbo].[LookupQuestions] D
ON c.QuestionID = d.ID
WHERE
c.questionid <> 10
ORDER BY
A.PATIENTID
You could use something like this:
SELECT
s.PATIENTID, p.STUDENTNUMBER, sqa.surveyid,
CONVERT(varchar, s.CreatedDate, 107),
sqa.QuestionID, sqa.Value, lq.Question,
Scores = (SELECT SUM(Value) FROM dbo.SurveyQuestionAnswer s2 WHERE s2.SurveyID = s.ID)
FROM
dbo.Survey s
INNER JOIN
dbo.Patient p ON s.PatientID = p.ID
INNER JOIN
[dbo].[SurveyQuestionAnswer] sqa ON sqa.SurveyID = s.ID
INNER JOIN
[dbo].[LookupQuestions] lq ON sqa.QuestionID = lq.ID
WHERE
sqa.questionid <> 10
ORDER BY
s.PATIENTID
By having a subquery with the SUM(...) you should be able to get that sum as a single value and you don't need to use any grouping function