如果您有任何专栏示例,请加入 可电动的柱子 createdon,modifiedon,姓名,ID
详细信息表 createdon,modifiedon,姓名,ID,mastertableid
现在每个月都会在master上自动添加一行 并且它可以在这个主人的细节上有很多行,所以 当一个用户不想等下个月他运行创建的过程时 master上的新行,以及其他详细行。 所以这个月我将有两位大师, 现在我只需要选择年份和月份 如果我这样选择,这个自动生成的第一个主数据的总和细节和为用户生成的第二个主数据,现在我不希望它发生,我只想要一个。 我不介意这是最后一个,还是第一个,所以我需要一个
然后我的问题是,如何选择最后创建的主人?(每年每个月) 所以我不介意,如果这最后一个是为用户创建的,还是autimaticallity,只有我需要最后一个主人及其详细信息
这是我当前的查询,但这会全部选择
SELECT
createdby
, createdbyname
, createdbyyominame
, createdon
, createdonutc
, createdonbehalfby
, createdonbehalfbyname
, createdonbehalfbyyominame
, importsequencenumber
, modifiedby
, modifiedbyname
, modifiedbyyominame
, modifiedon
, modifiedonutc
, modifiedonbehalfbyname
, modifiedonbehalfbyyominame
, modifiedonbehalfby
, ope_censoid
, ope_name
, ope_rutasid
, ope_rutasidname
, organizationid
, organizationidname
, overriddencreatedon
, overriddencreatedonutc
, statecode
, statecodename
, StatusCode
, statuscodename
, timezoneruleversionnumber
, utcconversiontimezonecode
FROM
ope_censo
ORDER BY
ope_censoid
, modifiedon
我用最后一个查询得到了这个结果
createdby createdbyname createdbyyominame createdon createdonutc createdonbehalfby createdonbehalfbyname createdonbehalfbyyominame importsequencenumber modifiedby modifiedbyname modifiedbyyominame modifiedon modifiedonutc modifiedonbehalfbyname modifiedonbehalfbyyominame modifiedonbehalfby ope_censoid ope_name ope_rutasid ope_rutasidname organizationid
71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-01 07:03:24.000 NULL NULL NULL NULL NULL 71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-01 07:03:24.000 NULL NULL NULL NULL 581EB2D0-A24C-E111-97AC-005056977FBC 1 - Febrero - 2012 FF7C3DBD-3EBA-E011-8C48-005056977FBC R 197 AGUA PURIF HOG LOC F94F39E4-7C6B-E011-8C48-005056977FBC
71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-13 15:33:41.000 NULL NULL NULL NULL NULL 71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-13 15:33:41.000 NULL NULL NULL NULL 63FFFF15-5856-E111-97AC-005056977FBC 13 - Febrero - 2012 FF7C3DBD-3EBA-E011-8C48-005056977FBC R 197 AGUA PURIF HOG LOC F94F39E4-7C6B-E011-8C48-005056977FBC
71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-24 00:29:27.000 NULL NULL NULL NULL NULL 71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-24 00:29:27.000 NULL NULL NULL NULL 3D14A09B-7E5E-E111-97AC-005056977FBC 23 - Febrero - 2012 FF7C3DBD-3EBA-E011-8C48-005056977FBC R 197 AGUA PURIF HOG LOC F94F39E4-7C6B-E011-8C48-005056977FBC
71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-01 07:02:57.000 NULL NULL NULL NULL NULL 71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-01 07:02:57.000 NULL NULL NULL NULL 7F53A7C4-A24C-E111-97AC-005056977FBC 1 - Febrero - 2012 CC7C3DBD-3EBA-E011-8C48-005056977FBC R128 AGUA PURIF HOG LOC F94F39E4-7C6B-E011-8C48-005056977FBC
71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-13 15:33:28.000 NULL NULL NULL NULL NULL 71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-13 15:33:28.000 NULL NULL NULL NULL 49540510-5856-E111-97AC-005056977FBC 13 - Febrero - 2012 CC7C3DBD-3EBA-E011-8C48-005056977FBC R128 AGUA PURIF HOG LOC F94F39E4-7C6B-E011-8C48-005056977FBC
71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-24 00:37:06.000 NULL NULL NULL NULL NULL 71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-24 00:37:06.000 NULL NULL NULL NULL 753C4BAD-7F5E-E111-97AC-005056977FBC 23 - Febrero - 2012 CC7C3DBD-3EBA-E011-8C48-005056977FBC R128 AGUA PURIF HOG LOC F94F39E4-7C6B-E011-8C48-005056977FBC
71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-01 07:06:55.000 NULL NULL NULL NULL NULL 71D3CE46-1B71-E011-8C48-005056977FBC Administrador CRM BBebidas Administrador CRM BBebidas 2012-02-01 07:06:55.000 NULL NULL NULL NULL 88650A4F-A34C-E111-97AC-005056977FBC 1 - Febrero - 2012 CD7C3DBD-3EBA-E011-8C48-005056977FBC R129 AGUA PURIF HOG LOC F94F39E4-7C6B-E011-8C48-005056977FBC
所以你可以看到“rutaidname”我有3次“R 197 AGUA PURIF HOG LOC”所以 你可以看到createdon是在2012年2月创建的我想要选择最后一个 “rutasidname”等等,如果这个“rutaidname”有和我想要的其他月份(一行多一行) 那个月的最后一个,所以,我需要为每个“rutaidname”创建的行是最后一个,每个月,每年。 “R 197 AGUA PURIF HOG LOC”的例子我想要2011年2月的最后一个,“2011年3月”的最后一位大师“2011年4月”的lsat大师。
答案 0 :(得分:4)
;WITH x AS
(
SELECT d = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', createdon), '19000101'),
rn = ROW_NUMBER() OVER (PARTITION BY DATEDIFF(MONTH, '19000101', createdon)
ORDER BY createdon), -- or ORDER BY createdon DESC, or ORDER BY id DESC
createdon, modifiedon, name, id, mastertableid
FROM dbo.mastertable
)
SELECT d,modifiedon,name,id,mastertableid
FROM x
WHERE rn = 1
ORDER BY d;
使用新要求和实际表/列名称:
SELECT
createdby, createdbyname
, createdbyyominame, createdon
, createdonutc, createdonbehalfby
, createdonbehalfbyname
, createdonbehalfbyyominame
, importsequencenumber
, modifiedby, modifiedbyname
, modifiedbyyominame, modifiedon
, modifiedonutc
, modifiedonbehalfbyname
, modifiedonbehalfbyyominame
, modifiedonbehalfby
, ope_censoid
, ope_name
, ope_rutasid
, ope_rutasidname
, organizationid
, organizationidname
, overriddencreatedon
, overriddencreatedonutc
, statecode
, statecodename
, StatusCode
, statuscodename
, timezoneruleversionnumber
, utcconversiontimezonecode
FROM
(
SELECT *, rn = ROW_NUMBER() OVER (PARTITION BY ope_rutasidname,
DATEDIFF(MONTH, '19000101', createdon) ORDER BY createdon DESC)
FROM dbo.ope_censo
) AS x
WHERE rn = 1
ORDER BY ope_censoid, modifiedon;
答案 1 :(得分:0)
在我看来,您只想要按月/年分组的MAX
日期值
未经测试的伪代码如下:
SELECT
MAX(someDate)
, CAST(YEAR(someDate) AS VARCHAR(4)) + CAST(MONTH(someDate) AS VARCHAR(2)) AS yearAndMonth
FROM
someTable
GROUP BY
CAST(YEAR(someDate) AS VARCHAR(4)) + CAST(MONTH(someDate) AS VARCHAR(2))