May 17, 2012 at 9:24 am
I need to work out the percentage of the values from sales. I have split my data into the necessary group like:
status Total ALLTOTAL
1. On Hold 29?
2. Attempting to Contact3
3. In dialogue with customer1
4. 1st Appointment made1
9. Quoted 15
Im trying to get the all total. I have basically got the total by counting status. But want to get the ALLTOTAL column to be 49 all the way down. Is there a way of doing this?
May 17, 2012 at 10:28 am
Reading the following should help:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
May 17, 2012 at 9:47 pm
Sachin 80451 (5/17/2012)
I need to work out the percentage of the values from sales. I have split my data into the necessary group like:status Total ALLTOTAL
1. On Hold 29?
2. Attempting to Contact3
3. In dialogue with customer1
4. 1st Appointment made1
9. Quoted 15
Im trying to get the all total. I have basically got the total by counting status. But want to get the ALLTOTAL column to be 49 all the way down. Is there a way of doing this?
SUM() OVER (ORDER BY (SELECT NULL))
--Jeff Moden
Change is inevitable... Change for the better is not.
May 17, 2012 at 10:13 pm
This would work as well 🙂
SUM([column]) OVER () AS AllTotal
Best regards,
Andre Guerreiro Neto
Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA
May 18, 2012 at 2:21 pm
One other way that it could be done as well is
select status, count(status) as total, (select count(*) from table) as alltotal
from table
group by status
May 20, 2012 at 10:01 pm
Here's yet another way:
DECLARE @t TABLE (status VARCHAR(30))
INSERT INTO @t
SELECT 'On Hold' UNION ALL SELECT 'On Hold' UNION ALL SELECT 'On Hold'
UNION ALL SELECT 'On Hold' UNION ALL SELECT 'On Hold' UNION ALL SELECT 'On Hold'
UNION ALL SELECT 'On Hold' UNION ALL SELECT 'On Hold' UNION ALL SELECT 'On Hold'
UNION ALL SELECT 'Attempting to Contact'
UNION ALL SELECT 'Attempting to Contact'
UNION ALL SELECT 'Attempting to Contact'
UNION ALL SELECT 'In dialogue with customer'
UNION ALL SELECT '1st Appointment made'
UNION ALL SELECT 'Quoted'
;WITH CWR AS (
SELECT status, COUNT(status) AS Count
FROM @t
GROUP BY status WITH ROLLUP)
SELECT CASE WHEN status IS NULL THEN 'All Total' ELSE status END as Status, count
FROM CWR
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
May 21, 2012 at 5:03 am
dwain.c (5/20/2012)
Here's yet another way:
DECLARE @t TABLE (status VARCHAR(30))
INSERT INTO @t
SELECT 'On Hold' UNION ALL SELECT 'On Hold' UNION ALL SELECT 'On Hold'
UNION ALL SELECT 'On Hold' UNION ALL SELECT 'On Hold' UNION ALL SELECT 'On Hold'
UNION ALL SELECT 'On Hold' UNION ALL SELECT 'On Hold' UNION ALL SELECT 'On Hold'
UNION ALL SELECT 'Attempting to Contact'
UNION ALL SELECT 'Attempting to Contact'
UNION ALL SELECT 'Attempting to Contact'
UNION ALL SELECT 'In dialogue with customer'
UNION ALL SELECT '1st Appointment made'
UNION ALL SELECT 'Quoted'
;WITH CWR AS (
SELECT status, COUNT(status) AS Count
FROM @t
GROUP BY status WITH ROLLUP)
SELECT CASE WHEN status IS NULL THEN 'All Total' ELSE status END as Status, count
FROM CWR
Nice alternative using WITH ROLLUP. Thank you.
Best regards,
Andre Guerreiro Neto
Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply