如何从sql数据库中按自定义id获取特定产品和组的最大计数
我有以下数据库,需要从SQL数据库中获取具有group by customer id的特定产品的最大计数 我尝试使用SQL子查询,但无法解决。请帮我解决这个问题 如果产品名称为如何从sql数据库中按自定义id获取特定产品和组的最大计数,sql,ruby-on-rails,mysqli,sqlite,Sql,Ruby On Rails,Mysqli,Sqlite,我有以下数据库,需要从SQL数据库中获取具有group by customer id的特定产品的最大计数 我尝试使用SQL子查询,但无法解决。请帮我解决这个问题 如果产品名称为bajari,我需要如下输出: 标准SQL使用分组依据子查询使用聚合: select customer_id, Product_name, count(*) mostcount from table t group by customer_id, Product_name having count(*) = (selec
bajari
,我需要如下输出:
标准SQL使用分组依据子查询使用聚合:
select customer_id, Product_name, count(*) mostcount
from table t
group by customer_id, Product_name
having count(*) = (select max(counts)
from (select customer_id, count(*) counts
from table t
group by customer_id, Product_name)
);
标准SQL使用groupby
clause和subquery
聚合:
select customer_id, Product_name, count(*) mostcount
from table t
group by customer_id, Product_name
having count(*) = (select max(counts)
from (select customer_id, count(*) counts
from table t
group by customer_id, Product_name)
);
使用以下查询
select customer_id, product_name, count(product_name) as mostcount from `products` where product_name='bajari' group by customer_id having count(product_name) =
( select max(mostcount) as highest_total
from (
select customer_id
, count(product_name) as mostcount
from `products` where product_name='bajari'
group
by customer_id
) as t
);
对于分步解决方案,请检查相同的解决方案
希望这对你有帮助 使用以下查询
select customer_id, product_name, count(product_name) as mostcount from `products` where product_name='bajari' group by customer_id having count(product_name) =
( select max(mostcount) as highest_total
from (
select customer_id
, count(product_name) as mostcount
from `products` where product_name='bajari'
group
by customer_id
) as t
);
<% find_array_max = [] %>
<% count = 0%>
<%@customers.each do |customer|%>
<% count = @products.where(product_name: 'bajari', customer_id: customer.id).sum(:weight)%>
<% find_array_max.push(count) %>
<% end -%>
<%= find_array_max.max%>.kg
<%= find_array_max.min%>.kg
对于分步解决方案,请检查相同的解决方案
希望这对你有帮助
<% find_array_max = [] %>
<% count = 0%>
<%@customers.each do |customer|%>
<% count = @products.where(product_name: 'bajari', customer_id: customer.id).sum(:weight)%>
<% find_array_max.push(count) %>
<% end -%>
<%= find_array_max.max%>.kg
<%= find_array_max.min%>.kg
公斤
公斤
公斤
公斤
编辑你的问题并显示你想要的结果。是的,我已更新我的问题如果有联系怎么办?编辑你的问题并显示你想要的结果。是的,我已更新我的问题如果有联系怎么办?