im trying to use max() here in my sql but im confused because i am using join table and i dont have idea where i should insert the max()
i need to add the column name date_issue from the table crew_documents_table
here is my sql:
select *
from
info join
crew_documents_table on info.id = crew_documents_table.document_crew_id join
crew_rank on info.crew_rank = crew_rank.crew_rank_id
where
crew_rank in ('1','2','3','4','5') and
crew_status = '$crew_status' and
vessel = '$vessel_name'
group by full_name
You can customize your SQL query as below :
select *, MAX(crew_documents_table.date_issue) as max_date
from
info join
crew_documents_table on info.id = crew_documents_table.document_crew_id join
crew_rank on info.crew_rank = crew_rank.crew_rank_id
where
crew_rank in ('1','2','3','4','5') and
crew_status = '$crew_status' and
vessel = '$vessel_name'
group by full_name
MAX allows you to select the maximum date
Related
I am trying to update two columns within a table from a select statment in MySQL 5.7.
The error I get is "invalid use of group function"
Stmt:
UPDATE
catalog mpc
JOIN
reviews mpr ON mpr.merchant_id = mpc.MERCHANT_ID and mpr.sku = mpc.ARTICLE_ID
SET
mpc.RATING = avg(mpr.rating),
mpc.RATINGS = count(mpr.rating)
WHERE
mpr.MERCHANT_ID = 1
AND mpr.sku = '133';
It looks about right to me, what could be the problem here?
You must aggregate first in reviews and then join to catalog:
UPDATE catalog mpc
INNER JOIN (
SELECT merchant_id, sku, AVG(rating) avg_rating, COUNT(rating) count_rating
FROM reviews
WHERE merchant_id = 1 AND sku = '133'
GROUP BY merchant_id, sku
) mpr ON mpr.merchant_id = mpc.MERCHANT_ID and mpr.sku = mpc.ARTICLE_ID
SET mpc.RATING = mpr.avg_rating,
mpc.RATINGS = mpr.count_rating
I'm working on a query in Access, but I'm really struggling to return a count of unique rows, and similarly a sum of unique values.
My query is as follows:
SELECT SiteOrders.CustomerID, Count(SiteOrders.ID) AS CountOfID, Sum(SiteOrders.Total) AS SumOfTotal, Sum(SiteOrderItems.Total) AS BrandTotal, Sum(SiteOrderItems.Quantity) AS SumOfQuantity
FROM SiteProducts INNER JOIN (SiteOrderItems INNER JOIN SiteOrders ON SiteOrderItems.OrderID = SiteOrders.ID) ON SiteProducts.ID = SiteOrderItems.ProductID
WHERE (((SiteProducts.Brand)="1") AND ((SiteOrders.OrderDate)>#4/1/2017# And (SiteOrders.OrderDate)<#4/1/2020#) AND ((SiteOrders.Paid)=True))
GROUP BY SiteOrders.CustomerID
ORDER BY SiteOrders.CustomerID;
The two fields where I want distinct values are a count on SiteOrders.ID and a sum on SiteOrders.Total.
Many thanks
Need nested query for aggregation of SiteOrders. Consider:
SELECT SiteOrders.CustomerID, Sum(SiteOrderItems.Quantity) AS SumQuantity,
Sum(SiteOrderItems.Total) AS SumTotal, Query1.CountOrderID, Query1.SumOrderTotal
FROM (SELECT CustomerID, Count(ID) AS CountOrderID, Sum(Total) AS SumOrderTotal
FROM SiteOrders
WHERE (((OrderDate) Between #4/1/2017# And #3/31/2020#) AND ((Paid)=True))
GROUP BY CustomerID) AS Query1
INNER JOIN (SiteOrders INNER JOIN (SiteProducts INNER JOIN SiteOrderItems
ON SiteProducts.ID = SiteOrderItems.ProductID)
ON SiteOrders.ID = SiteOrderItems.OrderID)
ON Query1.CustomerID = SiteOrders.CustomerID
GROUP BY SiteOrders.CustomerID, Query1.CountOrderID, Query1.SumOrderTotal;
This is my query.
SELECT dr_trans_dtl.dr_ID, jo_trans_dtl.qty
FROM dr_trans_dtl
LEFT JOIN jo_trans_dtl ON dr_trans_dtl.jo_no = jo_trans_dtl.jo_no
WHERE dr_trans_dtl.dr_no = '3329' GROUP BY dr_trans_dtl.dr_ID
Here is the actual result:
What I wanted is the qty should be like this (500,40,1). Because that is the data in jo_trans_dtl.
It seems you need aggregate function as you used group by
SELECT dr_trans_dtl.dr_ID,sum(jo_trans_dtl.qty) as qty
FROM dr_trans_dtl
JOIN jo_trans_dtl ON dr_trans_dtl.jo_no = jo_trans_dtl.jo_no
WHERE dr_trans_dtl.dr_no = '3329'
GROUP BY dr_trans_dtl.dr_ID
I have this query which i want to get rank from the data on my database
set #urut:=0;
set #rankhrg:=0;
select #urut:=#urut+1 as urut, a.id_tender, b.nama_tender, b.nomor_tender, b.tgl_close1 as tgl_close,
(SELECT rankhrg
from (select sum(tot_harga) as hrg_twr, id_rekanan, id_tender, #rankhrg:=#rankhrg+1 as rankhrg from tb_real_barang where id_tender = s.id_tender group by id_rekanan) as rank_harga
left join tb_master_tender s on s.id_tender = b.id_tender
where rank_harga.id_rekanan = a.id_rekanan
order by rank_harga.hrg_twr asc) as ranking
from tb_real_tender a
left join tb_master_tender b on a.id_tender = b.id_tender
where a.id_rekanan = 1
order by convert(a.id_tender,unsigned) desc
i want to pass id_tender into the select inside the select when i want to get rankhrg :
select sum(tot_harga) as hrg_twr, id_rekanan, id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang
where id_tender = s.id_tender
group by id_rekanan
but I always get error that said that s.id_tender is unknown in where clause.
can someone guide me how to pass the parameter into that insert?
thank you :)
You are not joining with that table tb_master_tender and neither it's present in outer query FROM clause. So, you need to do a JOIN separately for that inner query like below
select sum(trb.tot_harga) as hrg_twr,
trb.id_rekanan,
trb.id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang trb
left join tb_master_tender s on trb.id_tender = s.id_tender
group by trb.id_rekanan
I'm trying to select and group by all the contentid values of the table below where the match criteria can be several different values.
the contentid values actually represent cars, so I need to select [and group by] all the contentis where the values are 'GMC' and the values are 'sedan' and the value is 'automatic.
i.e. I'm trying to select all the GMC sedans with an automatic transmission.
a query like this fails [obviously]:
select * from modx_site_tmplvar_contentvalues WHERE
`value` = 'gmc' and
`value` = 'tacoma'
group by contentid
I have no idea how to create a query like that. Any suggestions?
You need to "pivot" these data on "tmplvarid", but unfortunately for you MySQL doesn't have a PIVOT statement like other RDBMS. However, you can pivot it yourself by joining in the table multiple times for each variable you care about:
SELECT
contents.contentid,
transmission.value as transmission,
type.value as type,
make.value as make
FROM
(SELECT DISTINCT contentid FROM modx_site_tmplvar_contentvalues) AS contents
LEFT JOIN
modx_site_tmplvar_contentvalues AS transmission
ON contents.contentid = transmission.contentid
AND transmission.tmplvarid = 33 -- id for transmission
LEFT JOIN
modx_site_tmplvar_contentvalues AS make
ON contents.contentid = make.contentid
AND make.tmplvarid = 13 -- id for make
LEFT JOIN
modx_site_tmplvar_contentvalues AS type
ON contents.contentid = type.contentid
AND type.tmplvarid = 17 -- id for type
WHERE
type.value = 'sedan'
AND make.value = 'GMC'
AND transmission.value = 'automatic'
You can expand this with additional joins for other criteria such as year (id 15) or mileage (id 16).
If you need to use the value only, you could try:
SELECT DISTINCT
contents.contentid,
transmission.value as transmission,
type.value as type,
make.value as make
FROM
(SELECT DISTINCT contentid FROM modx_site_tmplvar_contentvalues) AS contents
INNER JOIN
modx_site_tmplvar_contentvalues AS transmission
ON contents.contentid = transmission.contentid
AND transmission.value = 'automatic'
INNER JOIN
modx_site_tmplvar_contentvalues AS make
ON contents.contentid = make.contentid
AND make.value = 'GMC'
INNER JOIN
modx_site_tmplvar_contentvalues AS type
ON contents.contentid = type.contentid
AND type.value = 'sedan'
In any case, make sure you have an index on the value column; these queries are going to get slow.
please try this:
SELECT *
FROM modx_site_tmplvar_contentvalues t1 INNER JOIN modx_site_tmplvar_contentvalues t2 ON t1.contentid = t2.content_id
WHERE
t1.`value` = 'gmc'
AND t2.`value` = 'tacoma';
You can do this with a group by. This is the most flexible in terms of expressing the conditions. In MySQL, multiple joins will often perform better:
select contentid
from modx_site_tmplvar_contentvalues
group by contentid
having sum(`value` = 'gmc') > 0 and
sum(`value` = 'tacoma') > 0;
This is always false:
`value` = 'gmc' and
`value` = 'tacoma'
Instead, use OR:
`value` = 'gmc' OR
`value` = 'tacoma'
In a condition "and" means "this and this is true at the same time". If you want all foos and all bars, then your condition is "foo OR bar".
EDIT:
To select groups containing your values, you can write subqueries:
SELECT DISTINCT name FROM table WHERE name IN (SELECT name FROM table WHERE value='value1') AND name IN (SELECT name FROM table WHERE value='value2')