Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

GROUP BY rows and do mixed aggregation (MIN by different column)

$
0
0

Hi,

I have a Problem I don't get resolved.
First to the conditions.

My table Looks like this:

Table
---------------------------------------------------
(IDENTITY)    ID                 |      bigint
(PK)             InverterID     |      bigint
(PK)             TimeStamp    |      datetime
                   DayYield        |      decimal
                   Error              |      varchar

Here the reproduce script:

CREATE TABLE [dbo].[InverterData](
	[ID] [bigint] IDENTITY(1,1) NOT NULL,
	[InverterID] [bigint] NOT NULL,
	[TimeStamp] [datetime] NOT NULL,
	[DayYield] [decimal](18, 2) NULL,
	[Error] [nvarchar](50) NULL,
 CONSTRAINT [PrimaryKey_e149e28f-5754-4889-be01-65fafeebce16] PRIMARY KEY CLUSTERED 
(
	[InverterID] ASC,
	[TimeStamp] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)


What I now want as SELECT result is to GROUP the table by [TimeStamp] and [InverterID] and get this values as result:
   [InverterID]
   [TimeStamp]
   MIN([TimeStamp])
   MAX([TimeStamp])
   COUNT(DISTINCT [Error])
   [DayYield] from the row with MIN([TimeStamp])  <--- this is my problem

My select Looks like this until now:

SELECT 
	[InverterID], 
	[TimeStamp],
 	MIN([TimeStamp]) AS [FirstTimeStamp], 
 	MAX([TimeStamp]) AS [LastTimeStamp],
 	COUNT(DISTINCT [Error]) AS [ErrorCount],<????> AS [FirstDayYield]

	FROM [InverterData]
	GROUP BY [TimeStamp], [InverterID];
But I don't have a clue how to SELECT the [DayYield] over the MIN of a differnet column. :(

I hope my examples are clear, if you need more information, please ask.

Thanks for your help!

Steffen



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>