Tuesday, November 17, 2009

Group by and again group by and get results in sql query

Here,
there are two time , group by applied by using sub query in single query
Syntax of sql query:
following query showing with two time group by and data difference



select publisherWeb as Publisher,count(Adcount) as Adcount from

(
select publisherWeb,count(Adinfo) as Adcount from tblICA2008 where (DATEDIFF(Month, CAST(Year AS varchar) + '-' + CAST(Month AS varchar) + '-' + CAST(Day AS varchar), GETDATE()) <6) group by publisherWeb,Adinfo
) t

group by publisherWeb order by Adcount

1 comment:

  1. sub query usually hit the perfroamce as there will multiple select stattements. The same can be altered using joins which can improve the performance by 70%

    ReplyDelete