0

wel 如果您有主表任何列示例主表列 createdon,modifiedon,name,id

detailtable has createdon,modifiedon,name,id,mastertableid

现在每个月都会自动在 master 上添加一行,并且该 master 的详细信息可能有很多行,因此当一个用户不想等待下个月时,他运行在 master 和其他行上创建新行的过程关于细节。so.so 这个月我有两个主人,现在我只需要选择年和月,如果我这样做,第一个主人的总和细节自动生成,第二个主人为用户生成,现在我不希望它发生,我只想要一个。我不介意这是最后一个,还是第一个,所以我需要一个

那么我的问题是,我如何选择最后一个创建的主控?(每年每个月)所以我不介意这个最后一个是为用户创建的,还是自动创建的,只有我需要最后一个主控及其详细信息

这是我当前的查询,但这全选

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 月创建的,我想为这个“rutaidname”选择最后一个,所以,如果这个“rutaidname”有和其他月份(多行)我想要那个月的最后一个,所以,我需要每个“rutaidname”创建的行是最后一个,每个月,每年。例如“R 197 AGUA PURIF HOG LOC”我想要 2011 年 2 月的最后一个,“2011 年 3 月”的最后一个大师,“2011 年 4 月”的 lsat 大师。

4

2 回答 2

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;
于 2012-06-18T17:43:33.183 回答
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))
于 2012-06-18T17:46:21.480 回答