wel if you have a mastertable any columns example mastertable columns createdon,modifiedon,name,id

detailtable has createdon,modifiedon,name,id,mastertableid

now every month is added automatically a row on master and it could have many rows on detail for this master, so when one user doesn't want to wait for next month he run the process which create a new row on master, and others rows on detail. so.so for this month i'll have two masters, now i need do a select only with year, and month and if i do this select, this sum detail of first master generated automatically and the second master generated for user, now i dont want it happened, i want only one. i dont mind if this is the last, or it is the first, so i need one

then my question is, how do i select the last master created?(for every month for every year) so i dont mind if this last was created for user, or autimaticallity, only i need the last master, and its details

this is my current query but this select all

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

and i got this result with the last query

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

so you could see for "rutaidname" i have 3 times to "R 197 AGUA PURIF HOG LOC" so you could see createdon was created on february 2012 i wanted select the last for this "rutasidname" and so, if this "rutaidname" has and other month (one o more rows) i want the last for that month, so, i need for every "rutaidname" the rows where createdon was the last, for every month, for every year. example for "R 197 AGUA PURIF HOG LOC" i want the last of february 2011, the last master for "march 2011" the lsat master for "april 2011".

有帮助吗?

解决方案

It seems to me like you just want the MAX date value grouped by month/year untested pseudo-code below:

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))

其他提示

;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;

With the new requirements and real table/column names:

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top