I am looking to create a script which selects the first 6 months of data from each client. What I have at the moment is most definitely not right, and can someone please help me with this.
select Ticket_ClientName, count(Ticket_ClientName) as DisplayNameCount,
concat(datepart(year, cast(t.ticket_opendate as date)), RIGHT('00'+ convert(NVARCHAR(2),(datepart(month, cast(t.ticket_opendate as date)))),2)) as OpenDate,
datename(month, cast(t.ticket_opendate as date)) as Month_Name
from dbo.Ticket t
where t.Ticket_ClientName is not null
and Ticket_DisplayId not like 'EH%'
and Ticket_Statusname not like 'Deleted'
group by ticket_ClientName,concat(datepart(year, cast(t.ticket_opendate as date)), RIGHT('00'+ convert(NVARCHAR(2),(datepart(month, cast(t.ticket_opendate as date)))),2)), datename(month, cast(t.ticket_opendate as date))
I know I would need a top 6 in somewhere, but I can't wrap my head around this. I would normally think of a for loop, so for example
for distinct ticket_clientname in tickets:
I'm just struggling for the best way to approach this. When I run the current query I get:
Ticket_ClientName DisplayNameCount OpenDate Month_Name
------------------------------------------------------------
ClientName 1 202006 June
ClientName 1 202008 August
ClientName 13 202009 September
The data I would like is the top 6 per client I look forward to hearing from someone.