create view as
select Job, JobDate, count(*) as CountOfProcess,
(select top 1 Process from Processes P2
where P2.Job = Processes.Job and P2.JobDate = Processes.JobDate
order by P2.ID desc) LatestProcess
from Processes
group by Job, JobDate
... I figured there'd be some kind of group by aggregate function to do this in SQL by now. But I couldn't find one. So if anyone has a better way???
select top(1)
job
,[job date]
,process
,count(*) over(partition by job) as countofprocess
from yourtable
order by countofprocess desc
,id desc
;
If multiple result is allowed (in case several jobs have highest count):
select top(1) with ties
job
,[job date]
,process
,count(*) over(partition by job) as countofprocess
from yourtable
order by countofprocess desc
,row_number() over(partition by job order by [job date] desc,id desc)
;
again I have modified the view here how it displays
id process job date
5 1 208 6/3/2015
208 15 208 6/3/2015
7704 1 208 3/8/2017
I need to group to see only one row per date per job id process job date
5 1 208 6/3/2015
7704 1 208 3/8/2017
I don't need to be concerned which process or id i am displaying, in fact I can remove it from the view.
If I am reading this correctly - you want one row per job by date and you want to also get the id and process associated with that row as well as a count of the number of processes:
--==== Test Table
Declare @testTable Table (Id int, Process int, Job int, JobDate date);
Insert Into @testTable (Id, Process, Job, JobDate)
Values (5, 1, 208, '2015-06-03')
, (208, 15, 208, '2015-06-03')
, (7744, 1, 208, '2017-03-08');
Select * From @testTable;
--==== Code
With jobs
As (
Select Id
, Process
, Job
, JobDate
, row_number() over(Partition By Job, JobDate Order By Process desc) As rowNumber
, count(*) over(Partition By Job, JobDate) As NumberJobs
From @testTable
)
Select Id
, Process
, Job
, JobDate
, NumberJobs
From jobs
Where rowNumber = 1;
If you want the first row to be returned - remove 'desc' from the order by in the row_number function.
Thank you this works great, it lists , however I get an warning message while trying to save this view? will this cause problem when querying the table?
You have nothing to worry about. But you need to understand it.
When you use the view in an SQL query, if you want the results to be ordered, you must specify the order in that query, rather than relying on the order by inside your view.
How have you modified the query? There is no ORDER BY or TOP used in the query other than for the row_number function...
However, if you are using the GUI to create the view instead of just executing a CREATE VIEW statement then maybe the GUI is seeing the row_number order by and popping up a warning.
Note: instead of using the GUI to create a view it is much easier to do that in code:
Create View dbo.MyView
As
With jobs
As (
Select Id
, Process
, Job
, JobDate
, row_number() over(Partition By Job, JobDate Order By Process desc) As rowNumber
, count(*) over(Partition By Job, JobDate) As NumberJobs
From @testTable
)
Select Id
, Process
, Job
, JobDate
, NumberJobs
From jobs
Where rowNumber = 1;
Go