Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 count a total after a top rows number.

Author  Topic 

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2014-10-17 : 10:34:59
hi.
I am getting a total of codes by using this query:

select top 9 film_strcode from dbo.ZZ_rptTicketsSummary
GROUP BY film_strcode
--having SUM(TicketsSold)=(select max(TicketsSold) FROM(select SUM(TicketsSold) as TicketsSold from dbo.ZZ_rptTicketsSummary GROUP BY Film_strTitle) as t)
order by SUM(TicketsSold) desc

This will usually give 10-15 rows.
Using this query in a loop on vb code i will iterate through the first 9 rows and get the total for each cinema:

SELECT CONVERT(VARCHAR,CinemaGroupName + ' : ' +
CASE
WHEN MainTickets <> 0 AND CoolTickets <> 0 THEN MainTickets + ' Main ' + CoolTickets + ' U'
WHEN MainTickets <> 0 THEN MainTickets
WHEN CoolTickets <> 0 THEN CoolTickets
END)
FROM
(
SELECT CinemaGroupName,
CONVERT(VARCHAR,SUM(CASE WHEN IsCool = 0 THEN TicketsSold ELSE 0 END)) AS MainTickets,
CONVERT(VARCHAR,SUM(CASE WHEN IsCool = 1 THEN TicketsSold ELSE 0 END)) AS CoolTickets,
SortOrder
from dbo.ZZ_rptTicketsSummary M
join dbo.ZZ_ProgressBoardCinemaOrder PBCO
ON PBCO.CinemaName = CONVERT(VARCHAR,LTRIM(REPLACE(REPLACE(REPLACE(REPLACE(m.CinOperator_strName,'My',''),'Cinemas',''),'Cinema',''),'at the','')))
WHERE M.film_strcode = 'HO00AAAA59'
GROUP BY CinemaGroupName, SortOrder
) T
ORDER BY SortOrder


My problem is on how i will get a total for the next cinemas. A sample will be something like:

--(movie X -- this comes from a previous loop NOT in the above)
Cinema1 : 109
Cinema2: 200
Cinema3: 103

--(movie X11 -- this comes from a previous loop NOT in the above)
Cinema1 : 101
Cinema2: 20
Cinema3: 1103

---loop loop loop until first 9 codes

--So in here in need all the other codes after the first 9 to sum up --in:
--(movies other - so total. Ignore)
Cinema1: 5000
Cinema2: 3000
Cienam3: 1800
---This will be the total WITHOUT the first 9 movies

Anything else needed please ask but i will be at the office to view the db on Monday.
Thanks.
   

- Advertisement -