SQL Server 2008 - 查询返回最大日期(包括所有联系)

时间:2017-11-01 17:30:55

标签: sql-server

我希望根据一些条件编写一个返回最大日期的查询。另外 - 我需要灵活调整这个日期

我附上了桌子和屏幕截图。列。

dbo.MonitoringAnswer Columns

SELECT TOP 1000

  ,[ID]
  ,[MT_ID]
  ,[MTA_ID]
  ,[MTDR_ID]
  ,[MTA_DateTime]
  ,[MTA_DateTime_Int]
  ,[MTD_Name]
  ,[USR_Name]
  ,[USR_ID]
  ,[USR_RealID]
  ,[USR_ST_ID]
  ,[CUS_ID]
  ,[CUS_ST_ID]
  ,[CUS_RealID]
  ,[CUS_Name]
  ,[CUS_Addr1]
  ,[CUS_City]
  ,[CUS_CL1_Text]
  ,[CUS_CL1_NAME]
  ,[CUS_CL2_Text]
  ,[CUS_CL2_NAME]
  ,[CUS_CL3_Text]
  ,[CUS_CL3_NAME]
  ,[PRD_ID]
  ,[PRD_ST_ID]
  ,[PRD_RealID]
  ,[PRD_LongName]
  ,[PRC_ID_Name]
  ,[MTDR_Name]
  ,[MTAR_Ans_AsText]
  ,[MTAR_Ans_AsNumber]
  ,[MTAR_Ans_AsDateInt]
  ,[MTAR_Ans_OptionID]
  ,[MTAR_Ans_OptionText]
  ,[MTDR_AnsType_Text]
  ,[MT_ANSWER_PICTURE]
  ,[LinkID]
  ,[PictureLinkToPhoto]
  ,[APP_ID]
  ,[ST_ID]
  ,[ST_Name]
  ,[MTDR_SeqNo]
  ,[DM_LastUpdated]

FROM [ca_mars_quoforecloud]。[dbo]。[MonitoringAnswer]

我需要从表中提取所有列 - 条件是我只需要返回来自Cus_RealID的最新值

其中max date = 2017xxxx来自Mta_DateTime_Int 基于Cus_RealID 以及(xx,xx,xx)中的frm_id

如果MaxDate存在平局,我需要能够返回关系。

在下面的屏幕截图中,我提供了一个示例数据示例 - 请注意,此表中有超过300,000行。

SampleData

ID MT_ID MTA_ID MTDR_ID MTA_DateTime MTA_DateTime_Int
50002702979 52 5000270 2 10/26/2017 16:04 20171026
50002703977 52 5000270 3 10/26/2017 16:04 20171026
50002708977 52 5000270 8 10/26/2017 16:04 20171026
50002708978 52 5000270 8 10/26/2017 16:04 20171026
50002704978 52 5000270 4 10/26/2017 16:04 20171026
50002706980 52 5000270 6 10/26/2017 16:04 20171026
50002707979 52 5000270 7 10/26/2017 16:04 20171026
50002703979 52 5000270 3 10/26/2017 16:04 20171026
50002707978 52 5000270 7 10/26/2017 16:04 20171026
50002708980 52 5000270 8 10/26/2017 16:04 20171026
50002704977 52 5000270 4 10/26/2017 16:04 20171026
50002704979 52 5000270 4 10/26/2017 16:04 20171026
50002705978 52 5000270 5 10/26/2017 16:04 20171026
50002706977 52 5000270 6 10/26/2017 16:04 20171026
50002707980 52 5000270 7 10/26/2017 16:04 20171026
50002703980 52 5000270 3 10/26/2017 16:04 20171026
50002704980 52 5000270 4 10/26/2017 16:04 20171026
50002703978 52 5000270 3 10/26/2017 16:04 20171026
50002706979 52 5000270 6 10/26/2017 16:04 20171026
50002702980 52 5000270 2 10/26/2017 16:04 20171026
50002706978 52 5000270 6 10/26/2017 16:04 20171026
50002702977 52 5000270 2 10/26/2017 16:04 20171026
50002702978 52 5000270 2 10/26/2017 16:04 20171026
50002705980 52 5000270 5 10/26/2017 16:04 20171026
50002708979 52 5000270 8 10/26/2017 16:04 20171026
50002705977 52 5000270 5 10/26/2017 16:04 20171026
50002707977 52 5000270 7 10/26/2017 16:04 20171026
50002705979 52 5000270 5 10/26/2017 16:04 20171026
50002085977 52 5000208 5 10/4/2017 15:56 20171004
50002082977 52 5000208 2 10/4/2017 15:56 20171004
50002082978 52 5000208 2 10/4/2017 15:56 20171004
50002087977 52 5000208 7 10/4/2017 15:56 20171004
50002083977 52 5000208 3 10/4/2017 15:56 20171004
50002084978 52 5000208 4 10/4/2017 15:56 20171004
50002086977 52 5000208 6 10/4/2017 15:56 20171004
50002086978 52 5000208 6 10/4/2017 15:56 20171004
50002085978 52 5000208 5 10/4/2017 15:56 20171004
50002087978 52 5000208 7 10/4/2017 15:56 20171004
50002083978 52 5000208 3 10/4/2017 15:56 20171004
50002088977 52 5000208 8 10/4/2017 15:56 20171004
50002088978 52 5000208 8 10/4/2017 15:56 20171004
50002084977 52 5000208 4 10/4/2017 15:56 20171004
5.00015E + 11 33 5000150 12 9/13/2017 22:11 20170913
50001501977 33 5000150 1 9/13/2017 22:11 20170913
50001503978 33 5000150 3 9/13/2017 22:11 20170913
5.00015E + 11 33 5000150 11 9/13/2017 22:11 20170913
50001505978 33 5000150 5 9/13/2017 22:11 20170913
50001501978 33 5000150 1 9/13/2017 22:11 20170913
5.00015E + 11 33 5000150 12 9/13/2017 22:11 20170913
50001502977 33 5000150 2 9/13/2017 22:11 20170913
50001506978 33 5000150 6 9/13/2017 22:11 20170913
50001502978 33 5000150 2 9/13/2017 22:11 20170913
50001506977 33 5000150 6 9/13/2017 22:11 20170913
50001503979 33 5000150 3 9/13/2017 22:11 20170913
50001502979 33 5000150 2 9/13/2017 22:11 20170913
5.00015E + 11 33 5000150 11 9/13/2017 22:11 20170913
50001505979 33 5000150 5 9/13/2017 22:11 20170913
50001503977 33 5000150 3 9/13/2017 22:11 20170913
5.00015E + 11 33 5000150 12 9/13/2017 22:11 20170913
50001505977 33 5000150 5 9/13/2017 22:11 20170913
5.00015E + 11 33 5000150 11 9/13/2017 22:11 20170913
50001501979 33 5000150 1 9/13/2017 22:11 20170913
50001506979 33 5000150 6 9/13/2017 22:11 20170913
5.00004E + 11 33 5000037 11 7/25/2017 13:09 20170725
5.00004E + 11 33 5000037 11 7/25/2017 13:09 20170725
50000376977 33 5000037 6 7/25/2017 13:09 20170725
50000376979 33 5000037 6 7/25/2017 13:09 20170725
50000372978 33 5000037 2 7/25/2017 13:09 20170725
50000375978 33 5000037 5 7/25/2017 13:09 20170725
50000373978 33 5000037 3 7/25/2017 13:09 20170725
5.00004E + 11 33 5000037 10 7/25/2017 13:09 20170725
50000376978 33 5000037 6 7/25/2017 13:09 20170725
50000373980 33 5000037 3 7/25/2017 13:09 20170725
50000375980 33 5000037 5 7/25/2017 13:09 20170725
50000375979 33 5000037 5 7/25/2017 13:09 20170725
50000373977 33 5000037 3 7/25/2017 13:09 20170725
50000375977 33 5000037 5 7/25/2017 13:09 20170725
50000371980 33 5000037 1 7/25/2017 13:09 20170725
50000372977 33 5000037 2 7/25/2017 13:09 20170725
50000376980 33 5000037 6 7/25/2017 13:09 20170725
5.00004E + 11 33 5000037 11 7/25/2017 13:09 20170725
50000371977 33 5000037 1 7/25/2017 13:09 20170725
50000371979 33 5000037 1 7/25/2017 13:09 20170725
5.00004E + 11 33 5000037 11 7/25/2017 13:09 20170725
50000373979 33 5000037 3 7/25/2017 13:09 20170725
50000372979 33 5000037 2 7/25/2017 13:09 20170725
50000372980 33 5000037 2 7/25/2017 13:09 20170725
5.00004E + 11 33 5000037 10 7/25/2017 13:09 20170725
50000371978 33 5000037 1 7/25/2017 13:09 20170725

我希望查询在这个示例中做的是返回此特定cus_realid的所有结果,其中MTA_DateTime_Int是20171026 - 我不关心此日期之前的那些。

1 个答案:

答案 0 :(得分:0)

基于:

  

我希望查询在这个例子中做的是返回   此特定cus_realid的所有结果,其中MTA_DateTime_Int   是20171026 - 我不关心这个日期之前的那些。

  

和(xx,xx,xx)中的frm_id

听起来你只是想:

DECLARE @mydate DATETIME
SELECT @mydate = '10/26/2017'

SELECT *
FROM [ca_mars_quoforecloud].[dbo].[MonitoringAnswer]
WHERE Mta_DateTime_Int=@mydate AND frm_id in (xx, xx, xx)

但也有可能你的意思是你只想要每个Cus_RealID到那个日期的最后一条记录。如果Cus_RealID在10/24而不是在10/25或10/26上有记录,那么你仍然想要那个,因为它是最新的。

DECLARE @mydate DATETIME
SELECT @mydate = '10/26/2017'

SELECT ma.*
FROM [ca_mars_quoforecloud].[dbo].[MonitoringAnswer] ma 
    INNER JOIN 
    (
        SELECT MAX(Mta_DateTime_Int) MaxDate, Cus_RealID
        FROM [ca_mars_quoforecloud].[dbo].[MonitoringAnswer]
        WHERE Mta_DateTime_Int<=@mydate
        GROUP BY Cus_RealID
    ) x on ma.Cus_RealID=x.Cus_RealID AND ma.Mta_DateTime=x.MaxDate
WHERE frm_id in (xx, xx, xx)

如需进一步的帮助,您需要澄清您的问题。