Skip to main content
nikos101
Inspiring
November 18, 2008
Question

What is the best way to Aggregate query results

  • November 18, 2008
  • 13 replies
  • 1482 views
What is the best way to Aggregate query results that are retrived in a ms sql while statement,into a single query result?
This topic has been closed for replies.

13 replies

nikos101
nikos101Author
Inspiring
November 19, 2008
One of the things I don't like about table variables is

"Unlike ordinary tables, table variables can be used in join operator only by specifying alias name. "
Inspiring
November 19, 2008
> forget it I figured it all out :)

So, what did you end up doing?

> One of the things I don't like about table variables is
> "Unlike ordinary tables, table variables can be used in join operator only by specifying alias name. "

Why is that a bad thing? You should use aliases with joins anyway, unless you like typing out full table names ;-)
nikos101
nikos101Author
Inspiring
November 19, 2008
forget it I figured it all out :)
God bless you all :)
nikos101
nikos101Author
Inspiring
November 19, 2008
If you have select statement with subqueries is it possible to do a join also?

select tb3.col1 (subquery1),(subquery2),(subquery3) left outer join table3 as tb3

this doesn't work as you need a "from table" statement before left outer join
Inspiring
November 18, 2008
Duplicate
Inspiring
November 18, 2008
> select month01=(select count(*) from table where month = 1),
> month02=(select count(*) from table where month = 2), etc

As subqueries can be often be slower than an equivalent join, I would investigate whether it is actually needed. People sometimes use subqueries because they do not know how to write an equivalent join.

But it is hard to recommend anything concrete with the scant details provided.
November 18, 2008
How about

select month01=(select count(*) from table where month = 1),
month02=(select count(*) from table where month = 2), etc

You could probably do a join with a table containing rows with simply 1 thru 12 in the only column too.
Inspiring
November 18, 2008
It is difficult to make accurate recommendations without knowing the structure.

> select ( (select aggregate from table1),select column from table2
> I don't think this works

That would be a subquery.
http://msdn.microsoft.com/en-us/library/ms189575.aspx

However, joins (inner or outer) are often a better option in terms of performance. Joins can also be used with derived tables.

http://www.sql-server-performance.com/articles/per/derived_temp_tables_p1.aspx
nikos101
nikos101Author
Inspiring
November 18, 2008
I can't really go into detail with the table.

Can you select

something like this

select ( (select aggregate from table1),select column from table2

I don't think this works
Inspiring
November 18, 2008
> must be in a loop, the loop gathers data for each month,
> which may not have any data

Not necessarily. It depends. What is the structure of your data and what does the loop achieve that cannot be done with a JOIN?
nikos101
nikos101Author
Inspiring
November 18, 2008
must be in a loop, the loop gathers data for each month, which may not have any data