这是表格-CANo有数百个客户帐户,每个客户帐户都有成千上万的记录。
我想找到每个客户的最新最新记录。
现在,我首先运行以下查询以获取所有唯一的CANo。在桌子上
SELECT DISTINCT [CANo]
FROM [energymeters].[datalogging].[ardeecity]
ORDER BY CANo
然后另一个查询以获取最大的时间戳,以查找此特定CANo的最后一条记录或最新记录。
但是我的实际要求是运行查询以完成这两项任务并给我一张桌子。
USE [energymeters]
GO
SELECT
MAX(TimeStamp) AS TIMESTAMP
FROM [datalogging].[ardeecity]
WHERE CANo = 1220110001
GO
表设计类似于以下代码---
USE [energymeters]
GO
CREATE TABLE [datalogging].[ardeecity](
[IndexRecords] [int] IDENTITY(1,1) NOT NULL,
[TimeStamp] [datetime] NOT NULL,
[CANo] [bigint] NULL,
[DCUID] [int] NOT NULL,
[DevID] [tinyint] NOT NULL,
[EnergyEB] [decimal](15, 2) NULL,
[EnergyDG] [decimal](15, 2) NULL,
[EnergyBalance] [decimal](15, 2) NULL,
[TimeAvailable] [int] NULL,
[LastTopUpUnits] [decimal](15, 2) NULL,
[LastTopUpDateTime] [datetime] NULL,
[PreviousDayEnergy] [decimal](10, 2) NULL,
[PresentDayEnergy] [decimal](10, 2) NULL,
[PreviousMonthEnergyEB] [decimal](10, 2) NULL,
[PreviousMonthEnergyDG] [decimal](10, 2) NULL,
[PresentMonthEnergyEB] [decimal](10, 2) NULL,
[PresentMonthEnergyDG] [decimal](10, 2) NULL,
[RelayStatus] [tinyint] NULL,
[EBDGStatus] [tinyint] NULL,
[VoltageR] [decimal](8, 2) NULL,
[VoltageY] [decimal](8, 2) NULL,
[VoltageB] [decimal](8, 2) NULL,
[CurrentR] [decimal](8, 2) NULL,
[CurrentY] [decimal](8, 2) NULL,
[CurrentB] [decimal](8, 2) NULL,
[PFTotal] [decimal](5, 3) NULL,
[Frequency] [decimal](5, 2) NULL,
[KVATotal] [decimal](10, 2) NULL,
[KWTotal] [decimal](10, 2) NULL,
[KVARTotal] [decimal](10, 2) NULL,
[TamperStatus] [tinyint] NULL,
[LastMonthRupees] [decimal](15, 2) NULL,
[TokenStatus] [tinyint] NULL,
[PhaseType] [tinyint] NULL,
[MaximumLoadEB] [decimal](10, 2) NULL,
[MaximumLoadDG] [decimal](10, 2) NULL,
[MaximumDemandEB] [decimal](10, 2) NULL,
[MDIDateTimeEB] [datetime] NULL,
[MaximumDemandDG] [decimal](10, 2) NULL,
[MDIDateTimeDG] [datetime] NULL,
[BalanceRupees] [decimal](15, 2) NULL,
[MeterSerialNo] [nvarchar](50) NULL,
[Dummy1] [nvarchar](50) NULL,
[Dummy2] [nvarchar](50) NULL,
[Dummy3] [nvarchar](50) NULL,
[Token] [nvarchar](50) NULL,
[TokenRTC] [nvarchar](50) NULL,
[EBTariff] [decimal](10, 2) NULL,
[DGTariff] [decimal](10, 2) NULL,
[AdminStatus] [tinyint] NULL,
[SuperAdminStatus] [tinyint] NULL,
[PaymentType] [tinyint] NULL,
[IntegrationType] [tinyint] NULL,
[EmergencyPushButton] [tinyint] NULL,
[MDIDelayTime] [tinyint] NULL,
[SerialNoWriatable] [tinyint] NULL,
[VoltageMultFactorR] [decimal](10, 2) NULL,
[VoltageMultFactorY] [decimal](10, 2) NULL,
[VoltageMultFactorB] [decimal](10, 2) NULL,
[CurrentMultFactorR] [decimal](10, 2) NULL,
[CurrentMultFactorY] [decimal](10, 2) NULL,
[CurrentMultFactorB] [decimal](10, 2) NULL,
[PhaseErrorR] [decimal](10, 2) NULL,
[PhaseErrorY] [decimal](10, 2) NULL,
[PhaseErrorB] [decimal](10, 2) NULL,
[DeviceID] [int] NULL,
[Password] [int] NULL,
[RAMClear] [tinyint] NULL,
[EBDGStatusWritable] [tinyint] NULL,
[MDIResetWritable] [tinyint] NULL,
[VoltageRReadOnly] [decimal](10, 2) NULL,
[VoltageYReadOnly] [decimal](10, 2) NULL,
[VoltageBReadOnly] [decimal](10, 2) NULL,
[CurrentRReadOnly] [decimal](10, 2) NULL,
[CurrentYReadOnly] [decimal](10, 2) NULL,
[CurrentBReadOnly] [decimal](10, 2) NULL,
[KWRReadOnly] [decimal](10, 2) NULL,
[KWYReadOnly] [decimal](10, 2) NULL,
[KWBReadOnly] [decimal](10, 2) NULL,
[MeterStatus] [nvarchar](50) NULL,
[GPRSStatus] [nvarchar](50) NULL,
[TagNo] [int] NOT NULL
) ON [PRIMARY]
GO
每个CANo或客户帐户都具有成千上万个具有相同CANo的数据记录条目,并且成千上万的客户具有独特的CANo。
如何在每个CANo中查找最新或最后一条记录
非常感谢您的帮助-