简体   繁体   English

从多个表中选择MAX和SUM

[英]SELECT with MAX and SUM from multiple tables

I have 3 tables : 我有3张桌子:

  • weather_data (hourly_date, rain) weather_data(小时日期,下雨)
  • weather_data_calculated (hourly_date, calc_value) weather_data_calculated(小时日期,calc_value)
  • weather_data_daily (daily_date, daily_value) weather_data_daily(每日日期,每日值)

I would like to get a list of DAILY value from these 3 tables using this select : 我想使用以下select方法从这3个表中获取DAILY值的列表:

SELECT daily_date, daily_value, SUM(rain), MAX(calc_value)

The SUM and the MAX need to be done for all the hour of the day. SUM和MAX需要一天中的所有时间进行。

This is what I did : 这就是我所做的:

SELECT
date_format(convert_tz(daily_date, 'GMT', 'America/Los_Angeles'), '%Y-%m-%d 00:00:00') as daily_date_gmt,
daily_value,
SUM(rain),
MAX(calc_value)
FROM weather_data_daily wdd, weather_data wd, weather_data_calculated wdc
WHERE daily_date_gmt=date_format(convert_tz(wd.hourly_date, 'GMT', 'America/Los_Angeles'), '%Y-%m-%d 00:00:00')
and daily_date_gmt=date_format(convert_tz(wdc.hourly_date, 'GMT', 'America/Los_Angeles'), '%Y-%m-%d 00:00:00')
group by daily_date_gmt
order by daily_date_gmt;

This didn't work because I don't know how to deal with the group by in this case. 这是行不通的,因为在这种情况下,我不知道该如何与小组打交道。

I also try to use a temporary table but without success too. 我也尝试使用临时表,但也没有成功。

Thanks for your help! 谢谢你的帮助!

Either include daily_value in your group by, or use two queries. 在您的分组依据中包含daily_value,或者使用两个查询。 One will contain the date column and the two aggregates, the other will contain the date column and daily value. 一个将包含日期列和两个聚合,另一个将包含日期列和每日值。 you can then use a single outer query to join these result sets on the date column. 然后,您可以使用单个外部查询在date列上连接这些结果集。

EDIT: You say in your comment that including daily_value in the group by means the query doesn't complete. 编辑:您在评论中说,将组中的daily_value包括在内,表示查询未完成。 This is because (probably) you have no join criteria between all the tables your query includes. 这是因为(可能)查询中包含的所有表之间没有连接条件。 This will result in a potentially VERY large result set which would take a very long time. 这将导致潜在的非常大的结果集,这将花费很长时间。 I don't mind helping with the actual SQL but you will need to update your question so that we can see which fields are coming from which tables. 我不介意使用实际的SQL,但是您需要更新您的问题,以便我们可以查看哪些字段来自哪些表。

假设“ weather_data_daily”中的“ daily_date,daily_value”只有一个条目,则应该“ GROUP BY daily_date,daily_value”,然后您的聚集(SUM和MAX)将按正确的分组进行操作。

try this: 尝试这个:

select a.daily_date, a.daily_value, SUM(b.rain), MAX(c.calc_value)
from weather_data_daily a,weather_data b,weather_data_calculated c
where convert(varchar, a.daily_date, 101)=convert(varchar, b.hourly_date, 101)
and convert(varchar, a.daily_date, 101)=convert(varchar, c.hourly_date, 101)
group by a.daily_date, a.daily_value

You have to connect the tables together somehow (this uses an inner join). 您必须以某种方式将表连接在一起(这使用内部联接)。 This requires getting the hourly dates and other dates in the same format. 这要求以相同的格式获取小时日期和其他日期。 This gives them the format MM/DD/YYYY. 这使它们的格式为MM / DD / YYYY。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM