SQL 按每季度和组织最后创建的记录分组
SQL Group By last created record per quarter and org
我的数据库中有 3 个 table - Quarter
、Organization
、History
,我正在使用 Dynamics CRM 来存储我的数据。
Quarter
列:
- quarterId (guid)
- 季度(选择列表)
- 年(选择列表)
Organization
列:
- organizationId (guid)
History
列:
- historyId (guid)
- 计算(浮点数)
- 证书(选择列表)
- 组织 (organizationId)
- 季度 (quarterId)
- 创建日期(日期时间)
我想构建以下 SQL 查询:检索每个 Quarter
和 Organization
的所有 History
记录,其 create-date
值是上次创建的.
例如 History
table:
| guid | organization | cert | calc | create-date | quarter
| 1 | org1 | 2 | 2.5 | 2021-07-06 00:00:00 | Q3 2021
| 2 | org2 | 2 | NULL | 2021-07-25 00:00:00 | Q3 2021
| 3 | org3 | 3 | NULL | 2021-07-25 00:00:00 | Q3 2021
| 4 | org1 | 3 | NULL | 2021-08-01 00:00:00 | Q3 2021
| 5 | org1 | 3 | NULL | 2021-08-01 00:00:00 | Q3 2021
| 6 | org1 | 3 | NULL | 2021-09-15 00:00:00 | Q3 2021
| 7 | org1 | 2 | 2.25 | 2021-11-18 00:00:00 | Q4 2021
查询应该return以下记录:(减去 guid 列)
| guid | organization | cert | calc | create-date | quarter | year
| 2 | org2 | 2 | NULL | 2021-07-25 00:00:00 | Q3 | 2021
| 3 | org3 | 3 | NULL | 2021-07-25 00:00:00 | Q3 | 2021
| 6 | org1 | 3 | NULL | 2021-09-15 00:00:00 | Q3 | 2021
| 7 | org1 | 2 | 2.25 | 2021-11-18 00:00:00 | Q4 | 2021
我的查询:
SELECT
QTR.new_year AS [year],
CASE
WHEN QTR.new_quarter = 100000000 THEN 'Q1'
WHEN QTR.new_quarter = 100000001 THEN 'Q2'
WHEN QTR.new_quarter = 100000002 THEN 'Q3'
ELSE 'Q4'
END AS [quarter],
/* FORMAT(HISTORY.new_calc, 'N2') AS [calc],
HISTORY.new_cert AS [cert], */
MAX(HISTORY.new_create_date) AS [create-date],
ORG.new_name AS [org]
FROM
dbo.new_quarterBase AS QTR
INNER JOIN
dbo.new_historyBase AS HISTORY ON QTR.new_quarterId = HISTORY.new_quarter
INNER JOIN
dbo.new_organizationBase AS ORG ON HISTORY.new_organization = ORG.new_organizationId
GROUP BY
QTR.new_quarter, QTR.new_year, ORG.new_name
查询工作正常,但我希望查询也 return 注释掉的列,我不希望它们出现在 GROUP BY
子句中。
我该如何解决这个问题?
最简单的方法是创建一个新的 tmp table(或视图),然后 select 从新的 table/view 中获得您想要的一切,例如:
create table tmp_table_name as
select *
from ...
order by...;
select column1_name, column2_name
from tmp_table_name;
I'd like to construct the following SQL query: Retrieve all History records per Quarter and Organization whose create-date value is last created.
我不知道 Dynamics CRM 的基础数据库是什么,但在 SQL 中执行此操作的方法使用 row_number()
或 rank()
:
SELECT qh.*
FROM (SELECT q.new_year AS [year],
(CASE WHEN q.new_quarter = 100000000 THEN 'Q1'
WHEN q.new_quarter = 100000001 THEN 'Q2'
WHEN q.new_quarter = 100000002 THEN 'Q3'
ELSE 'Q4'
END) AS [quarter],
FORMAT(h.new_calc, 'N2') AS [calc],
h.new_cert AS [cert],
h.new_create_date,
o.new_name AS org,
RANK() OVER (PARTITION BY q.new_quarter, q.new_year, o.new_name ORDER BY h.new_create_date DESC) as seqnum
FROM dbo.new_quarterBase q JOIN
dbo.new_historyBase h
ON q.new_quarterId = h.new_quarter JOIN
dbo.new_organizationBase o
ON h.new_organization = o.new_organizationId
) qh
WHERE seqnum = 1;
row_number()
和rank()
的区别在于后者returns记录一个季度的所有历史记录,如果有并列为最近的。前者returns只有一个。
我的数据库中有 3 个 table - Quarter
、Organization
、History
,我正在使用 Dynamics CRM 来存储我的数据。
Quarter
列:
- quarterId (guid)
- 季度(选择列表)
- 年(选择列表)
Organization
列:
- organizationId (guid)
History
列:
- historyId (guid)
- 计算(浮点数)
- 证书(选择列表)
- 组织 (organizationId)
- 季度 (quarterId)
- 创建日期(日期时间)
我想构建以下 SQL 查询:检索每个 Quarter
和 Organization
的所有 History
记录,其 create-date
值是上次创建的.
例如 History
table:
| guid | organization | cert | calc | create-date | quarter
| 1 | org1 | 2 | 2.5 | 2021-07-06 00:00:00 | Q3 2021
| 2 | org2 | 2 | NULL | 2021-07-25 00:00:00 | Q3 2021
| 3 | org3 | 3 | NULL | 2021-07-25 00:00:00 | Q3 2021
| 4 | org1 | 3 | NULL | 2021-08-01 00:00:00 | Q3 2021
| 5 | org1 | 3 | NULL | 2021-08-01 00:00:00 | Q3 2021
| 6 | org1 | 3 | NULL | 2021-09-15 00:00:00 | Q3 2021
| 7 | org1 | 2 | 2.25 | 2021-11-18 00:00:00 | Q4 2021
查询应该return以下记录:(减去 guid 列)
| guid | organization | cert | calc | create-date | quarter | year
| 2 | org2 | 2 | NULL | 2021-07-25 00:00:00 | Q3 | 2021
| 3 | org3 | 3 | NULL | 2021-07-25 00:00:00 | Q3 | 2021
| 6 | org1 | 3 | NULL | 2021-09-15 00:00:00 | Q3 | 2021
| 7 | org1 | 2 | 2.25 | 2021-11-18 00:00:00 | Q4 | 2021
我的查询:
SELECT
QTR.new_year AS [year],
CASE
WHEN QTR.new_quarter = 100000000 THEN 'Q1'
WHEN QTR.new_quarter = 100000001 THEN 'Q2'
WHEN QTR.new_quarter = 100000002 THEN 'Q3'
ELSE 'Q4'
END AS [quarter],
/* FORMAT(HISTORY.new_calc, 'N2') AS [calc],
HISTORY.new_cert AS [cert], */
MAX(HISTORY.new_create_date) AS [create-date],
ORG.new_name AS [org]
FROM
dbo.new_quarterBase AS QTR
INNER JOIN
dbo.new_historyBase AS HISTORY ON QTR.new_quarterId = HISTORY.new_quarter
INNER JOIN
dbo.new_organizationBase AS ORG ON HISTORY.new_organization = ORG.new_organizationId
GROUP BY
QTR.new_quarter, QTR.new_year, ORG.new_name
查询工作正常,但我希望查询也 return 注释掉的列,我不希望它们出现在 GROUP BY
子句中。
我该如何解决这个问题?
最简单的方法是创建一个新的 tmp table(或视图),然后 select 从新的 table/view 中获得您想要的一切,例如:
create table tmp_table_name as
select *
from ...
order by...;
select column1_name, column2_name
from tmp_table_name;
I'd like to construct the following SQL query: Retrieve all History records per Quarter and Organization whose create-date value is last created.
我不知道 Dynamics CRM 的基础数据库是什么,但在 SQL 中执行此操作的方法使用 row_number()
或 rank()
:
SELECT qh.*
FROM (SELECT q.new_year AS [year],
(CASE WHEN q.new_quarter = 100000000 THEN 'Q1'
WHEN q.new_quarter = 100000001 THEN 'Q2'
WHEN q.new_quarter = 100000002 THEN 'Q3'
ELSE 'Q4'
END) AS [quarter],
FORMAT(h.new_calc, 'N2') AS [calc],
h.new_cert AS [cert],
h.new_create_date,
o.new_name AS org,
RANK() OVER (PARTITION BY q.new_quarter, q.new_year, o.new_name ORDER BY h.new_create_date DESC) as seqnum
FROM dbo.new_quarterBase q JOIN
dbo.new_historyBase h
ON q.new_quarterId = h.new_quarter JOIN
dbo.new_organizationBase o
ON h.new_organization = o.new_organizationId
) qh
WHERE seqnum = 1;
row_number()
和rank()
的区别在于后者returns记录一个季度的所有历史记录,如果有并列为最近的。前者returns只有一个。