I have two tables 1- `FormTemplate` where the columns are Id, CreatedDate, Creator, Schema and 2 - `FormSubmissions` where the columns are Id, FormTemplateId(Foreign key), Created Date, Creator.
FormTemplate
| Id | CreatedDate | Creator | Schema |
|------------|------------------------|---------|---------|
| 1 | 2023-01-25 00:52:43.717| John | {} |
| 2 | 2023-01-25 00:52:43.717| Same | {} |
| 3 | 2023-01-25 00:52:43.717| Alee | {} |
FormSubmission
| Id | FormTemplateId (Foreign key)| CreatedDate | Creator |
|--------|-----------------------------|------------------------|------------|
| 1 | 2| 2023-05-25 00:52:43.717| Kyle |
| 2 | 2| 2023-02-25 00:52:43.717| Kayla |
| 3 | 3| 2019-03-08 00:52:43.717| Arthur |
| 4 | 3| 2019-02-25 00:52:43.717| Milli |
I would like to get all FormTemplate
rows where the LATEST FormSubmission
was 6 months ago. in the above table: I should get only the 3rd row (Id=3) from FormTemplate
我想获得6个月前最新FormSubmission所在的所有FormTemplate行。在上表中:我应该只从FormTemplate中获得第三行(Id=3)
I cannot create a query or find a way to fetch FormTemplates with this criteria efficiently.
我无法创建查询,也找不到使用此条件有效获取FormTemplates的方法。
更多回答
T-SQL is a SQL dialect that is used by multiple products including Sybase, SQL Server, and Azure Synapse. What is the (R)DBMS product you are using here? Edit your question to tag that product, and (if relevant) the version tag for the product as well.
T-SQL是一种SQL方言,可用于多种产品,包括Sybase、SQL Server和Azure Synapse。您在这里使用的(R)DBMS产品是什么?编辑您的问题以标记该产品,以及(如果相关)该产品的版本标记。
@ThomA I am using this on an Azure SQL database. Edited to add the tag.
@ThomA我正在Azure SQL数据库上使用它。已编辑以添加标记。
DROP TABLE IF EXISTS #FormTemplate;
DROP TABLE IF EXISTS #FormSubmission;
CREATE TABLE #FormTemplate
(
Id INT NOT NULL,
CreatedDate datetime NOT NULL,
Creator varchar(50) NOT NULL
);
INSERT INTO #FormTemplate
VALUES
(1, '2023-01-25 00:52:43.717', 'John')
, (2, '2023-01-25 00:52:43.717', 'Same')
, (3, '2023-01-25 00:52:43.717', 'Alee')
;
CREATE TABLE #FormSubmission
(
Id INT NOT NULL,
FormTemplateId INT NOT NULL,
CreatedDate datetime NOT NULL,
Creator varchar(50) NOT NULL
);
INSERT INTO #FormSubmission
VALUES
(1, 2, '2023-05-25 00:52:43.717', 'Kyle')
, (2, 2, '2023-02-25 00:52:43.717', 'Kayla')
, (3, 3, '2019-03-08 00:52:43.717', 'Arthur')
, (4, 3, '2019-02-25 00:52:43.717', 'Milli')
;
SELECT T.Id, MAX(T.CreatedDate) AS CreatedDate, T.Creator
FROM #FormTemplate AS T
INNER JOIN #FormSubmission AS S
ON T.ID = S.FormTemplateId
GROUP BY T.Id, T.Creator
HAVING MAX(S.CreatedDate) <= DATEADD(MONTH, -6, GETDATE())
;
You can also use a pre-grouped join.
也可以使用预分组联接。
SELECT
t.*,
s.*
FROM FormTemplate t
JOIN (
SELECT
s.FormTemplateId,
MaxCreated = MAX(s.CreatedDate)
FROM FormSubmission s
GROUP BY s.FormTemplateId
HAVING MAX(s.CreatedDate) <= DATEADD(MONTH, -6, GETDATE())
) s ON t.ID = s.FormTemplateId;
To get the actual ID of the subquery, you can instead use an APPLY
subquery with a TOP 1
要获得子查询的实际ID,可以使用带有TOP 1的APPLY子查询
SELECT
t.*,
s.*
FROM FormTemplate t
CROSS APPLY (
SELECT TOP (1)
s.FormTemplateId,
s.CreatedDate
FROM FormSubmission s
WHERE t.ID = s.FormTemplateId
ORDER BY s.CreatedDate DESC
) s
WHERE s.CreatedDate <= DATEADD(MONTH, -6, GETDATE());
As far as the other answers are concerned, as long as you are grouping by t.ID
the performance will almost certainly be the same.
就其他答案而言,只要你按t.ID分组,性能几乎肯定是一样的。
select FormTemplateId
into #old
from FormSubmission
group by FormTemplateId
having max(CreatedDate)<=dateadd(month,-6,getdate())
select * from FormTemplate where Id in (select FormTemplateId from #old)
更多回答
I am running your query but would want to get the FormSubmission.Id in the result which is not showing atm with the above. I am getting is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
I would like to know which FormSubmission is the latest entry (submitted more than 6 months ago)
我正在运行您的查询,但希望在结果中获得FormSubmission.Id,该结果没有显示上面的atm。我在选择列表中得到的是无效的,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中。我想知道哪个FormSubmission是最新的条目(6个多月前提交)
you are a legend. that worked. :) Thank you
你是个传奇。成功了。:)非常感谢。
does it also delete #old after query?
它是否也会在查询后删除#old?
#tables die automatically at the end of the session
#会话结束时,表自动失效
我是一名优秀的程序员,十分优秀!