Thanks, however COUNT(t.gross_amount) AS TradeCount does give a count, but it will not be tied to the SUM() period (date range) i need .
In proc body there are other periods like ytd, wtd, mtd.
Main Topics
Browse All TopicsCan I get sum() and count(*) in one statement like:
CASE WHEN t.posting_date between @period_start and @prm_cmp_start_date
THEN SUM(t.gross_amount) AS Sales, COUNT(t.gross_amount) AS TradeCount
Purpose is query performance by reading one pass and getting two columns sales and tradecount.
Is it possible, if so, how if not thru CASE stmt above. Thank you.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Can you not just do
SELECT SUM(t.gross_amount) AS Sales, COUNT(*) AS TradeCount
FROM myTable t
WHERE t.posting_date between @period_start and @prm_cmp_start_date
If you need the count of records within the same date range, but there are other periods, can you try:
SELECT SUM(CASE WHEN t.posting_date between @period_start and @prm_cmp_start_date THEN t.gross_amount ELSE 0 END) AS Sales,
SUM(CASE WHEN t.posting_date between @period_start and @prm_cmp_start_date THEN 1 ELSE 0 END) AS TradeCount
I agree with nmcdermaid's approach, the others will not give you a true count of what you need.
This is only doing one seek on the table and is evaluating two different result sets.
I would however look at what is being indexed to see if your supposition that this is indeed faster is correct by trying the other approaches, sometimes if you are doing subqueries then the values from one is cached into the other and the two independent queries can leverage off the other creating something that is actually fastter.
Depending on the conditions that you are using you could try putting the results of your query into a sub query, and then doing two sums (without conditional logic attached to them)
something like
select
sum(q.counter) as TradeCount,
sum(q.Sales) as Sales
from
(
SELECT 1 as counter, t.gross_amount AS Sales
FROM myTable t
WHERE t.posting_date between @period_start and @prm_cmp_start_date
) as q
which is putting less tests on the results being returned and may reduce your processor stress.
I will test sub-query for performance. Thanks. On another note, when I have to do same ( sales, tradecount) for different date intervals ( ytd, mtd, wtd ) is using
posting_date between @period_start and @prm_cmp_start_date with different range variables, the best approarch or are there other ways to achive same results with better query performance ?
Business Accounts
Answer for Membership
by: cezarFPosted on 2007-09-11 at 17:48:06ID: 19873167
try
SUM(CASE WHEN t.posting_date between @period_start and @prm_cmp_start_date
THEN t.gross_amount
ELSE 0 END) AS Sales,
COUNT(t.gross_amount) AS TradeCount