0
select 
    'N' as Content,  
    (select COUNT(*)  from TB1  ) as [Sample Analyzed ],  
    (select COUNT(*)  from TB1  where convert(float,D) between 0.0005 and 0.75) as [Very Low],  
    (select COUNT(*)  from TB1  where convert(float,D) between 0.76 and 1.25 ) as [Low] 
union  
    select 'P' as Content,  
    (select COUNT(*)  from TB1  ) as [Sample Analyzed ],  
    (select COUNT(*)  from TB1  where convert(float,C) between 0.0005 and 0.75) as [Very Low],  
    (select COUNT(*)  from TB1  where convert(float,C) between 0.76 and 1.25 ) as [Low] 
union  
    select 'K' as Content,  
    (select COUNT(*)  from TB1  ) as [Sample Analyzed ],
    (select COUNT(*)  from TB1  where convert(float,B) between 0.0005 and 0.75) as [Very Low],  
    (select COUNT(*)  from TB1  where convert(float,B) between 0.76 and 1.25 ) as [Low] 
union  
    select 'OC' as Content,  
    (select COUNT(*)  from TB1  ) as [Sample Analyzed ],  
    (select COUNT(*)  from TB1  where convert(float,A) between 0.0005 and 0.75) as [Very Low],  
    (select COUNT(*)  from TB1  where convert(float,A) between 0.76 and 1.25 ) as [Low]
ORDER BY
    (
     CASE 
       WHEN  Content = 'N' THEN 0
       WHEN  Content = 'P' THEN 1
       WHEN  Content = 'K' THEN 2
       WHEN  Content = 'OC' THEN 3
       ELSE 4
     END
    )
5
  • Define "doesn't work"? What errors do you get? What is the expected output? Read this: stackoverflow.com/help/mcve Commented Aug 21, 2017 at 14:32
  • Msg 207, Level 16, State 1, Procedure Get_NPK_Report, Line 44 Invalid column name 'Content'. Msg 207, Level 16, State 1, Procedure Get_NPK_Report, Line 44 Invalid column name 'Content'. Msg 207, Level 16, State 1, Procedure Get_NPK_Report, Line 44 Invalid column name 'Content'. Msg 207, Level 16, State 1, Procedure Get_NPK_Report, Line 44 Invalid column name 'Content'. Msg 104, Level 16, State 1, Procedure Get_NPK_Report, Line 50 ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator. Commented Aug 21, 2017 at 14:33
  • above error i got in sql serve r2008 my output now K 7 0 1 N 7 1 1 OC 7 0 0 P 7 0 2 what i want i want order of K,N,OC,P should be like N,P,K,OC Commented Aug 21, 2017 at 14:34
  • Ok? The error is clear and I've given a way to circumvent it. Commented Aug 21, 2017 at 14:41
  • 1
    Please note that additional information that is important to understand the problem, should be included in the question itself, not in the comment section. You can always edit your question and add the information in the question itself. Commented Aug 21, 2017 at 14:53

1 Answer 1

1

...Invalid column name 'Content'....

That's because you can't use a column alias in the ORDER BY

ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator

That's because the column alias isn't part of the select, same as above. It isn't an actual column (yet)

Here's an example by wrapping it in a CTE.

with cte as(
select 
    'N' as Content,  
    (select COUNT(*)  from TB1  ) as [Sample Analyzed ],  
    (select COUNT(*)  from TB1  where convert(float,D) between 0.0005 and 0.75) as [Very Low],  
    (select COUNT(*)  from TB1  where convert(float,D) between 0.76 and 1.25 ) as [Low] 
union  
    select 'P' as Content,  
    (select COUNT(*)  from TB1  ) as [Sample Analyzed ],  
    (select COUNT(*)  from TB1  where convert(float,C) between 0.0005 and 0.75) as [Very Low],  
    (select COUNT(*)  from TB1  where convert(float,C) between 0.76 and 1.25 ) as [Low] 
union  
    select 'K' as Content,  
    (select COUNT(*)  from TB1  ) as [Sample Analyzed ],
    (select COUNT(*)  from TB1  where convert(float,B) between 0.0005 and 0.75) as [Very Low],  
    (select COUNT(*)  from TB1  where convert(float,B) between 0.76 and 1.25 ) as [Low] 
union  
    select 'OC' as Content,  
    (select COUNT(*)  from TB1  ) as [Sample Analyzed ],  
    (select COUNT(*)  from TB1  where convert(float,A) between 0.0005 and 0.75) as [Very Low],  
    (select COUNT(*)  from TB1  where convert(float,A) between 0.76 and 1.25 ) as [Low]
)

select * from cte 
ORDER BY
    (
     CASE 
       WHEN  Content = 'N' THEN 0
       WHEN  Content = 'P' THEN 1
       WHEN  Content = 'K' THEN 2
       WHEN  Content = 'OC' THEN 3
       ELSE 4
     END
    )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.