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;
Related
I have 3 table
tmp (pid,title,price)
tmp_studyarea(areaid, tittle, tm_pid)
tmp_module(mid,title, duration, areaid)
I am trying to write a query where I can obtain (tmp.pid, tmp.title, tmp.price, SUM(tmp_module.duration Where tmp_module.areaid = tmpstudyarea.areaid and tmp_studyarea.tmp_pid = tmp.pid) Group by tmp.pid
Here is a query I wrote and i'm unable to get expected results. Help please
SELECT s.title, s.pid, SUM(duration) IN (SELECT a.tmpid, a.areaid, a.title, SUM(m.duration) as duration FROM tmp_studyarea AS a, tmp, tmp_module as m WHERE m.areaid = a.areaid AND a.tmpid = s.pid GROUP BY a.areaid) FROM tmp as s;
Here is my expected resultst
title
pid
duration
tmp Title 1
1
3000
tmp Title 3
4
1000
EDIT
Found a solution
SELECT DISTINCT s.title,s.pid, (SELECT SUM(m.duration) as duration FROM tmp_studyarea AS a, tmp_module as m WHERE m.areaid = a.areaid AND a.tmpid = s.pid GROUP BY a.tmpid) as duration FROM tmp as s, tmp_studyarea, tmp_module GROUP by pid
You must join the 3 tables properly and aggregate:
SELECT t.pid, t.title, t.price,
SUM(m.duration) total_duration
FROM tmp t
INNER JOIN tmp_studyarea s ON s.tmp_pid = t.pid
INNER JOIN tmp_module m ON m.areaid = s.areaid
GROUP BY t.pid, t.title, t.price
I am trying to get a SQL code but can't really figure out how to do it so I will explain what I want.
I have 4 tables called Person, Customer, Adres and Store. Now I have to show each customer NAMES which lives in the same city as where there is a Store. So First I figured out which persons are customers by:
SELECT person_name
FROM person
WHERE person_id IN
(SELECT Person_Person_Id
FROM customer);
Which stores are in which city:
SELECT Store_name, adres_city
FROM store s, adres a
WHERE s.Adres_Adres_Id = a.adres_id;
Note that person_person_id is the same as person_id just as a fk.
I am stuck at this code and don''t know how to go further from here. My column name of table adres = adres_city.
Okay, if I realised what do you want, try to do this:
select --distinct
b.Adres_City,
a.person_id
from
dbo.Person a
join dbo.Adres b on a.adres_adres_id = b.Adres_Id
join dbo.Customer c on a.person_id = c.Person_Person_Id
join dbo.Store d on b.Adres_Id = d.Adres_Adres_Id
If you are not sure, that all your keys in tables are uniq, uncomment --distinct in the first string.
Or, if you are want to know statistics among your cities, do this:
select
b.Adres_City,
count(distinct a.person_id) as cnt
from
dbo.Person a
join dbo.Adres b on a.adres_adres_id = b.Adres_Id
join dbo.Customer c on a.person_id = c.Person_Person_Id
join dbo.Store d on b.Adres_Id = d.Adres_Adres_Id
group by b.Adres_City
Please let me know, if it will help you.
Update1:
select --distinct
b.Adres_City,
a.person_id
from
dbo.Person a
join dbo.Adres b on a.adres_adres_id = b.Adres_Id
join dbo.Customer c on a.person_id = c.Person_Person_Id
where
b.Adres_City in (
select y.Adres_City
from dbo.Store x join dbo.Adres y on y.Adres_Id = x.Adres_Adres_Id
)
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'
I'm struggling with an problem to make a query of the following:
I have an table called Article_Statuses (and the main table Articles, with ArticleID as p.key) and has the following structure
ID, ArticleID, Status, Status_Date, Category
In this table I collect all the statuses of the articles (ArticleID) per category (which are predefined), the highest ID per category is the latest status of that category, herewith some data:
1, BB0001, LFS, 15-01-2015, LIC
2, BB0001, LFA, 19-01-2015, LIC
3, BB0001, SA, 10-01-2015, FIS
4, BB0001, CA, 19-01-2015, FIS
5, BB0002, LFS, 10-01-2015, LIC
6, BB0002, LFA, 11-01-2015, LIC
7, BB0003, CA, 19-01-2015, FIS
I want to make a query with the following result:
ArticleID, Status LIC, Status_Date LIC, Status FIS, Status_Date FIS
BB0001, LFA, 19-01-2015, CA, 19-01-2015
BB0002, LFA, 11-01-2015, ,
BB0003, , , CA, 19-01-2015
I found the following solution which works for only one category, I'm stuck with adding the other categories...
SELECT `a`.`ArticleID`, `b`.`Status_Date` AS `LIC_Date`, `b`.`Status` AS `LIC_Status`
FROM `Articles` `a`
INNER JOIN `Article_Statuses` `b` ON `a`.`ArticleID` = `b`.`ArticleID`
INNER JOIN ( SELECT `ArticleID`, MAX( `ID` ) `MAXID`
FROM `Article_Statuses`
WHERE `Category` = 'LIC' GROUP BY `ArticleID` ) `c`
ON `b`.`ArticleID` = `c`.`ArticleID` AND `b`.`ID` = `c`.`MAXID`
WHERE `a`.`Partner` = 10
GROUP BY `a`.`ArticleID`
ORDER BY `a`.`ArticleID` ASC
What is the meaning of "Partner" in your query? You didn't mentioned it anywhere earlier so I'm guessing it's not important.
How many different categories do you have? Just two or more? I'm asking because returning data in such a way is far for being fast and optimal.
It would be something like:
SELECT
a.ArticleID,
b.Status_Date AS LIC_Date, b.Status AS LIC_Status,
d.Status_Date AS FIS_Date, d.Status AS FIS_Status
FROM Articles AS a
INNER JOIN Article_Statuses AS b ON a.ArticleID = b.ArticleID
INNER JOIN (
SELECT ArticleID, MAX( ID ) AS ID
FROM Article_Statuses
WHERE Category = 'LIC' GROUP BY ArticleID
) AS c ON b.ArticleID = c.ArticleID AND b.ID = c.ID
INNER JOIN Article_Statuses AS d ON a.ArticleID = d.ArticleID
INNER JOIN (
SELECT ArticleID, MAX( ID ) AS ID
FROM Article_Statuses
WHERE Category = 'FIS' GROUP BY ArticleID
) AS e ON d.ArticleID = e.ArticleID AND d.ID = e.ID
WHERE a.Partner = 10
ORDER BY a.ArticleID ASC
Basically you repeat joins just with different aliased - I used "d" and "e" but it's better to use something meaningful like "LIC_category" instead of just "b".
You also should use left join instead of inner join if some categories can be empty as in your example.
This is the query which returns desired result:
select a.articleid,
b.status_date as LIC_Date, b.status as LIC_Status,
c.status_date as FIS_Date, c.status as FIS_Status
from Articles a
left join Article_Statuses b on b.ArticleID =a.articleid and
b.id = (select max(id) from Article_Statuses ast
where ast.articleid=a.articleid and ast.category='LIC')
left join Article_Statuses c on c.ArticleID =a.articleid and
c.id = (select max(id) from Article_Statuses ast
where ast.articleid=c.articleid and ast.category='FIS')
order by a.articleid
I used left joins because not for every article both categories are present.
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