选择月份的最大ID [英] select max ID of the month

查看:85
本文介绍了选择月份的最大ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ID列表如下所示。



AT0920130004

AT0920130005

AT0920130006
AT0920130007

AT0920130008

AT0920130009

AT0920130010

AT1020130001

AT1020130002

AT1020130003

AT1020130004

AT1020130005

AT1120130003

AT1120130004

AT1120130005

AT1120130006



这里的示例记录格式为ATmmyyyyxxxx。



其中



`AT`代表`location`,



`mm `代表`月',例如10将是'十月'



`yyyy`代表`年`例如。 `2013`



`xxxx`代表`数字增长的种子'。



现在我需要选择在月末生成的ID。

例如:9月的最后一个ID,即AT0920130010。

任何建议都表示赞赏。

解决方案

这实际上非常复杂,因为您必须将每个ID分解为各个部分(年,月,日)并将其转换为数字才能使用它们。虽然这并不困难 - 它只是SUBSTRING和CAST或CONVERT操作 - 你必须做几次才能得到你想要的东西。



它可能更好地改变存储ID的方式,如果可以的话,使用DATETIME和INT列中的COMPUTED列 - 它使得排序和分组很多,更容易!

http://technet.microsoft.com/en-us/library/ms188300.aspx [ ^ ]


< pre lang =sql> 选择 max([id])
来自 table
其中 cast(substring([id], 5 4 as int )= 2013 -
cast(substring([id], 3 2 as int )= 9 -

看看例子:

  DECLARE   @tmp  (ID  VARCHAR  30 ))

INSERT INTO @ tmp (ID)
VALUES ' < span class =code-string> AT0920130004'),
' AT0920130005'),
' AT0920130006'),
' AT0920130007'),
' AT0920130008' ),
' AT0920130009'),
(< span class =code-string>' AT0920130010'),
' AT1020130001'),
' AT1020130002'),
' AT1020130003'),
' AT1020130004'),
' AT1020130005'),
' AT1120130003'),
' AT1120130004'),
' AT1120130005'),
' AT1120130006'


SELECT 位置,Mth,Yer,MAX(种子)< span class =code-keyword> AS MaxOfSeed
FROM
SELECT LEFT (ID, 2 AS 位置, CONVERT INT ,SUBSTRING(ID, 3 2 )) AS Mth, CONVERT INT ,SUBSTRING(ID, 5 4 )) AS 是的, CONVERT INT RIGHT (ID, 4 )) AS 种子
FROM @ tmp
AS T
GROUP BY 位置,Mth,是的





结果:

 Loca。 Mth Yer MaxOfSeed 
at 9 2013 10
at 10 2013 5
at 11 2013 6


I have a list of ID’s Shown below.

AT0920130004
AT0920130005
AT0920130006
AT0920130007
AT0920130008
AT0920130009
AT0920130010
AT1020130001
AT1020130002
AT1020130003
AT1020130004
AT1020130005
AT1120130003
AT1120130004
AT1120130005
AT1120130006

Here an example record has the format `ATmmyyyyxxxx`.

where

`AT` represents `location`,

`mm` represents `month` eg 10 would be `October`

`yyyy` represents `year` eg. `2013`

`xxxx` represents the `increasing seed of numbers`.

Now I need to select an ID which is generated in the `end of the month`.
For ex: last id of September i.e. AT0920130010.
Any suggestions are appreciated.

解决方案

That's actually quite complicated, because you will have to break up each ID into the various parts (year, month, day) and convert those to numbers before you can use them. While that isn't difficult particularly - it's just SUBSTRING and CAST or CONVERT operations - you will have to do it several times to get what you want.

It would probably be better to change the way you store IDs, to use a COMPUTED column from a DATETIME and an INT column if you can - it makes sorting and grouping a lot, lot easier!
http://technet.microsoft.com/en-us/library/ms188300.aspx[^]


select max([id])
from table
where cast(substring([id], 5, 4) as int) = 2013 -- year
    and cast(substring([id], 3, 2) as int) = 9 -- month


Have a look at example:

DECLARE @tmp TABLE (ID VARCHAR(30))

INSERT INTO @tmp (ID)
VALUES('AT0920130004'),
    ('AT0920130005'),
    ('AT0920130006'),
    ('AT0920130007'),
    ('AT0920130008'),
    ('AT0920130009'),
    ('AT0920130010'),
    ('AT1020130001'),
    ('AT1020130002'),
    ('AT1020130003'),
    ('AT1020130004'),
    ('AT1020130005'),
    ('AT1120130003'),
    ('AT1120130004'),
    ('AT1120130005'),
    ('AT1120130006')


SELECT Location, Mth, Yer, MAX(Seed) AS MaxOfSeed
FROM (
    SELECT LEFT(ID,2) AS Location, CONVERT(INT, SUBSTRING(ID,3,2)) AS Mth, CONVERT(INT, SUBSTRING(ID,5,4)) AS Yer, CONVERT(INT, RIGHT(ID,4)) AS Seed
    FROM @tmp
) AS T
GROUP BY Location, Mth, Yer



Result:

Loca.   Mth     Yer     MaxOfSeed
AT	9	2013	10
AT	10	2013	5
AT	11	2013	6


这篇关于选择月份的最大ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆