select case when h.VVID<=15 then COUNT(h.fs) end as [<=15],
case when h.VVID between 16 and 18 then COUNT(h.fs) end as [16 - 18],
case when h.VVID between 19 and 21 then COUNT(h.fs) end as [19 - 21],
case when h.VVID between 22 and 24 then COUNT(h.fs) end as [22 - 24],
case when h.VVID>=25 then COUNT(h.fs) end as [>=25]
from
(
select t.fs,sum(t.VVID) VVID,sum(t.VID) VID,sum(t.ID) ID from
(
select dr.C_FS_Code fs,
case when ds.c_doc_grade='G00001' then COUNT(distinct ds.C_DSC_Code) end as [VVID],
case when ds.c_doc_grade='G00002' then COUNT(distinct ds.C_DSC_Code) end as [VID],
case when ds.c_doc_grade='G00003' then COUNT(distinct ds.C_DSC_Code) end as [ID]
from tbl_dwr dr
join tbl_dwr_details ds on ds.N_Srno=dr.N_Srno
join Tbl_FS_Mst fs on fs.C_Code=dr.C_FS_Code
join Tbl_Doc_Stock_Chem_Add_Mst sd on sd.C_Code=ds.C_DSC_Code
where MONTH(dr.D_Date_Report) in (3,4) and YEAR(dr.D_Date_Report)=2015
and fs.N_Type=1
group by dr.C_FS_Code,ds.c_doc_grade
)t
group by t.fs
order by t.fs
)h
after executing this query am getting error like in below
Msg 1033, Level 15, State 1, Line 34
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
please give solution for this...