April 6, 2020 at 10:34 am
Hi All,
I am having table where below list of columns
OrderNo
ActualHours
PlanedHours
calMonth
now i want to find MIN and MAX ActualHours ,PlanedHours,calMonth alongwith ActualHours and
PlanedHours ,ActualHours for MAX calMonth.
how can fetch this?
thanks
Abhas.
April 6, 2020 at 4:55 pm
If this is SQL 2019, you can use window functions. This might help you. Your requirement isn't clear. Here is some sample data and a query. Not sure if this is what you need.
CREATE TABLE OrderHourList (
OrderNo INT NOT NULL CONSTRAINT OrderHourListPK PRIMARY KEY
, ActualHours SMALLINT
, PlannedHours SMALLINT
, CalMonth VARCHAR(6)
)
GO
INSERT dbo.OrderHourList
(OrderNo, ActualHours, PlannedHours, CalMonth)
VALUES
(1, 10, 10, '202001')
, (2, 10, 20, '202001')
, (3, 20, 40, '202001')
, (4, 20, 15, '202002')
, (5, 20, 10, '202002')
, (6, 5, 10, '202003')
, (7, 40, 50, '202003')
, (8, 10, 20, '202003')
, (9, 20, 10, '202003')
, (10, 30, 5, '202003')
GO
WITH cteMax(calmonth)
AS
( SELECT MAX(calmonth)
FROM dbo.OrderHourList AS ohl
)
SELECT ohl.CalMonth
, MAX(ohl.ActualHours) OVER (ORDER BY (select NULL)) AS MaxActual
, MIN(ohl.ActualHours) OVER (ORDER BY (select NULL)) AS MinActual
, MAX(ohl.PlannedHours) OVER (ORDER BY (select NULL)) AS MaxPlanned
, MIN(ohl.PlannedHours) OVER (ORDER BY (select NULL)) AS MinPlanned
, ohl.ActualHours
, ohl.PlannedHours
FROM dbo.OrderHourList AS ohl
INNER JOIN cteMax ON cteMax.calmonth = ohl.CalMonth
April 7, 2020 at 4:14 pm
Thank you Steve ,
this helped me. Than you so much.
Regards,
Abaso Jadhav
April 7, 2020 at 5:11 pm
You are welcome. If you have more issues, please post back. Including code makes this easier for us, both with setup and seeing your expected results.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply