只使用WHERE子句而不是ORDER BY来获取每个应用程序的最新状态,使用MAX函数。

huangapple go评论126阅读模式
英文:

Get only the latest status by MAX function for each Application using WHERE clausule instead of ORDER BY

问题

I would like to obtain the same result of query using WHERE instead of GROUP BY. I have applications and each application has statuses with date. I need to return applications with their latest status date.

我想要通过使用 WHERE 而不是 GROUP BY 来获得相同的查询结果。我有应用程序,每个应用程序都有日期的状态。我需要返回带有其最新状态日期的应用程序。

My query with ORDER BY: (the result is correct)

我的查询使用 ORDER BY:(结果是正确的)

  1. select a.guid, MAX(s.request_time) as last_request_time
  2. from public.applications_status s inner join public.applications a on a.guid = s.guid_application
  3. group by a.guid;

result:

结果:

  1. guid |last_request_time |
  2. ------------------------------------|-------------------|
  3. 330d32d5-2496-4cce-9d11-29e59333766a|2020-07-22 13:06:25|
  4. 5b46cda9-b954-4d8b-82cf-f1d83f77b175|2020-07-22 13:07:25|
  5. 34071189-ab3d-47ff-9ee1-aca6fa806bc9|2020-08-03 10:45:15|
  6. a8961058-a6ee-4d71-b325-9aca83b22237|2020-08-03 10:45:39|
  7. ff98695f-e1a8-439e-8a6c-7991348b6cd7|2020-07-29 14:38:18|

I try this but it return me only the one application with the latest status date:

我尝试这个,但它只返回了具有最新状态日期的一个应用程序:

  1. select a.guid, s.request_time
  2. from public.applications_status s inner join public.applications a on a.guid = s.guid_application
  3. where request_time = (select MAX(applications_status.request_time) from applications_status );

result:

结果:

  1. guid |request_time |
  2. ------------------------------------|-------------------|
  3. a8961058-a6ee-4d71-b325-9aca83b22237|2020-08-03 10:45:39|

Applications table

应用程序表

  1. CREATE TABLE public.applications (
  2. id bigserial NOT NULL,
  3. guid varchar(40) NOT NULL,
  4. "name" varchar(60) NOT NULL,
  5. latest_status_date timestamp NULL,
  6. latest_status bool NOT NULL,
  7. id_production bigserial NOT NULL,
  8. CONSTRAINT applications_guid_key UNIQUE (guid),
  9. CONSTRAINT applications_pkey PRIMARY KEY (id),
  10. CONSTRAINT uk_gtuqgycxk8ulkir3io2p49yn1 UNIQUE (guid),
  11. CONSTRAINT fkaid_prod FOREIGN KEY (id_production) REFERENCES productions(id) ON DELETE CASCADE
  12. );

Applications_status table

应用程序状态表

  1. CREATE TABLE public.applications_status (
  2. id bigserial NOT NULL,
  3. status bool NOT NULL,
  4. guid_application varchar(50) NOT NULL,
  5. log varchar(200) NULL,
  6. request_time timestamp NOT NULL,
  7. CONSTRAINT status_pkey PRIMARY KEY (id),
  8. CONSTRAINT fkaguid_application FOREIGN KEY (guid_application) REFERENCES applications(guid) ON UPDATE CASCADE ON DELETE CASCADE
  9. );

Why I need this way? I try to return Applications with their latest status in Spring Boot using @Where annotation in @OneToMany relation in Entity.

为什么我需要这种方式?我尝试在Spring Boot中使用@Where注解在Entity中的@OneToMany关系中返回带有其最新状态的应用程序。

  1. @OneToMany(cascade = CascadeType.ALL, mappedBy = "application", fetch = LAZY)
  2. @JsonManagedReference
  3. @Where(clause = "request_time = (SELECT MAX(applications_status.request_time) FROM applications_status )")
  4. @OrderBy("requestTime DESC")
  5. private List<ApplicationStatus> applicationStatuses;

I also try to use @BatchSize(size = 1) but it doesn't work.

我还尝试使用 @BatchSize(size = 1) 但它不起作用。

英文:

I would like to obtain the same result of query using WHERE instead of GROUP BY. I have applications and each application has statuses with date. I need to return applications with their latest status date.
My query with ORDER BY: (the result is correct)

  1. select a.guid, MAX(s.request_time) as last_request_time
  2. from public.applications_status s inner join public.applications a on a.guid = s.guid_application
  3. group by a.guid ;

result:

  1. guid |last_request_time |
  2. ------------------------------------|-------------------|
  3. 330d32d5-2496-4cce-9d11-29e59333766a|2020-07-22 13:06:25|
  4. 5b46cda9-b954-4d8b-82cf-f1d83f77b175|2020-07-22 13:07:25|
  5. 34071189-ab3d-47ff-9ee1-aca6fa806bc9|2020-08-03 10:45:15|
  6. a8961058-a6ee-4d71-b325-9aca83b22237|2020-08-03 10:45:39|
  7. ff98695f-e1a8-439e-8a6c-7991348b6cd7|2020-07-29 14:38:18|

I try this but it return me only the one application with latest status date:

  1. select a.guid, s.request_time
  2. from public.applications_status s inner join public.applications a on a.guid = s.guid_application
  3. where request_time = (select MAX(applications_status.request_time) from applications_status );

result:

  1. guid |request_time |
  2. ------------------------------------|-------------------|
  3. a8961058-a6ee-4d71-b325-9aca83b22237|2020-08-03 10:45:39|

Applications table

  1. CREATE TABLE public.applications (
  2. id bigserial NOT NULL,
  3. guid varchar(40) NOT NULL,
  4. &quot;name&quot; varchar(60) NOT NULL,
  5. latest_status_date timestamp NULL,
  6. latest_status bool NOT NULL,
  7. id_production bigserial NOT NULL,
  8. CONSTRAINT applications_guid_key UNIQUE (guid),
  9. CONSTRAINT applications_pkey PRIMARY KEY (id),
  10. CONSTRAINT uk_gtuqgycxk8ulkir3io2p49yn1 UNIQUE (guid),
  11. CONSTRAINT fkaid_prod FOREIGN KEY (id_production) REFERENCES productions(id) ON DELETE CASCADE
  12. );

Applications_status table

  1. CREATE TABLE public.applications_status (
  2. id bigserial NOT NULL,
  3. status bool NOT NULL,
  4. guid_application varchar(50) NOT NULL,
  5. log varchar(200) NULL,
  6. request_time timestamp NOT NULL,
  7. CONSTRAINT status_pkey PRIMARY KEY (id),
  8. CONSTRAINT fkaguid_application FOREIGN KEY (guid_application) REFERENCES applications(guid) ON UPDATE CASCADE ON DELETE CASCADE
  9. );

Why I need this way? I try to return Applications with their latest status in Spring Boot using @Where annotation in @OneToMany relation in Entity.

  1. @OneToMany(cascade = CascadeType.ALL, mappedBy = &quot;application&quot;, fetch = LAZY)
  2. @JsonManagedReference
  3. @Where(clause = &quot;request_time = (SELECT MAX(applications_status.request_time) FROM applications_status )&quot;)
  4. @OrderBy(&quot;requestTime DESC&quot;)
  5. private List&lt;ApplicationStatus&gt; applicationStatuses;

I also try to use @BatchSize(size = 1) but it doesn't work.

答案1

得分: 0

The question is tagged both "sql" and "postgres," so this is a Postgres solution.

Use distinct on:

  1. select distinct on (a.guid) a.*, s.*
  2. from public.applications_status s inner join
  3. public.applications a
  4. on a.guid = s.guid_application
  5. order by a.guid, s.request_time desc;

distinct on is a very handy Postgres extension that returns one row (the "first" row) for each group in the parentheses. The particular row is based on the order by.

英文:

The question is tagged both "sql" and "postgres", so this is a Postgres solution.

Use distinct on:

  1. select distinct on (a.guid) a.*, s.*
  2. from public.applications_status s inner join
  3. public.applications a
  4. on a.guid = s.guid_application
  5. order by a.guid, s.request_time desc;

distinct on is a very handy Postgres extension that returns one row (the "first" row) for each group in the parentheses. The particular row is based on the order by.

答案2

得分: 0

SQL 查询:

  1. 选择 a.guids.request_time
  2. 从公共应用程序状态 s 内联加入公共应用程序 a on a.guid = s.guid_application
  3. 其中 request_time = (选择 MAX(applications_status.request_time)
  4. applications_status
  5. 其中 applications_status.guid_application = s.guid_application;
  6. Spring-Boot
  7. @OneToMany(cascade = CascadeType.ALL, mappedBy = "application", fetch = LAZY)
  8. @JsonManagedReference
  9. @Where(clause = "request_time = (SELECT MAX(applications_status.request_time) FROM applications_status where guid_application = applications_status.guid_application )")
  10. @OrderBy("requestTime DESC")
  11. private List<ApplicationStatus> applicationStatuses;
英文:

Through trial and error, I found a solution:

SQL query:

  1. select a.guid, s.request_time
  2. from public.applications_status s inner join public.applications a on a.guid = s.guid_application
  3. where request_time = (select MAX(applications_status.request_time)
  4. from applications_status
  5. where applications_status.guid_application = s.guid_application );

Spring-Boot:

  1. @OneToMany(cascade = CascadeType.ALL, mappedBy = &quot;application&quot;, fetch = LAZY)
  2. @JsonManagedReference
  3. @Where(clause = &quot;request_time = (SELECT MAX(applications_status.request_time) FROM applications_status where guid_application = applications_status.guid_application )&quot;)
  4. @OrderBy(&quot;requestTime DESC&quot;)
  5. private List&lt;ApplicationStatus&gt; applicationStatuses;

huangapple
  • 本文由 发表于 2020年8月3日 22:38:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63231485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定