查询如何根据单一条件选择数据

时间:2013-12-27 13:56:08

标签: sql postgresql

需要您的帮助才能查看以下请求。

我正在使用以下查询,我想要实现的目标如下

我正在尝试提取new_status_id的最小值,而不是取任何其他状态的任何其他详细信息。基本上我得到一行以上的输出为一个v_id输入,其中所需的输出是一行,而min(timestamp)是在列名下获取的,如下定义的上传

Select
to_char(to_date(date,'DY Mon DD YYYY'),'MM-DD')||' '||Away_team.name||' '||'@'||' '||home_team.name as "Title",
video_jobs.id as "Job Id",
markets.name as "Market",
round as "Round",
to_char(to_date(date,'DY Mon DD YYYY'),'DD-Mon-YY') as "Date Aired",
competitions.name as "Competition",
Home_team.name as "Home Team",
Away_team.name as "Away Team",
job_statuses.name as "Job Status",
networks.name as "Network",
stadiums.name as "Stadium",
Round(images_count/3600.0,2) as "Footage Hours",
round(sum(minutes_in_logo_finder)/60.0,2) as "Logo Picker Time",
Round(sum(minutes_in_hits_editor)/60.0,2) as "Hits Editing Time",
Round(sum(minutes_in_branding)/60.0,2) as "Branding Time",
Round(sum(minutes_in_tagging)/60.0,2) as "Tagging Time",
Round(sum(minutes_in_qc)/60.0,2)as "QC Time",
Round(sum(minutes_in_idle)/60.0,2) as "Idle Time",
Round(sum(loading_time)/60.0,2) as "Loading Time",
Round(sum(minutes_in_logo_finder+minutes_in_hits_editor + minutes_in_branding +minutes_in_tagging +minutes_in_qc+minutes_in_idle+loading_time)/60.0,2) as "Total"

from video_jobs 
Join markets on video_jobs.market_id = markets.id
Join stadiums on video_jobs.stadium_id = Stadiums.id
Join job_statuses on video_jobs.status_id = job_statuses.id
Join competitions on video_jobs.competition_id = competitions.id
Join networks on video_jobs.network_id = networks.id
Join sessions on video_jobs.id = sessions.job_id
join teams as Home_team on video_jobs.home_team_id = home_team.id
join teams as Away_team on video_jobs.away_team_id = Away_team.id
Join video_job_status_history on video_jobs.id = video_job_status_history.video_job_id

Where video_jobs.id > 163026  

group by
video_jobs.id,
markets.name,
video_jobs.round,
stadiums.name,
job_statuses.name,
competitions.name,
networks.name,
video_jobs.images_count,
home_team.name,
Away_team.name,
video_jobs.date

order by 
video_jobs.id

1 个答案:

答案 0 :(得分:0)

将条件添加到where子句中:

select min(timestamp) as "Uplaod"
from v_jobs
where v_id = 1234 and new_status_id = 25;

编辑:

在这种情况下,你想要的是条件聚合:

select min(case when new_status_id = 25 then timestamp end) as MinTimestamp25
. . .