0
SELECT 
    MProduct.ProductCode, MProduct.ProductName, COUNT(*) AS Ranges
FROM 
    TProblem 
FULL OUTER JOIN
    MProduct ON TProblem.ProductCode = MProduct.ProductCode 
GROUP BY  
    MProduct.ProductCode, MProduct.ProductName 
ORDER BY 
    Ranges DESC

This is my query but I want to hide the Ranges column from output

2
  • 1
    Well if you want to hide it - then don't list it in the SELECT list of columns! Commented Sep 26, 2017 at 5:28
  • 1
    actually its is imp so i dont want to show it Commented Sep 26, 2017 at 5:31

2 Answers 2

3

To maintain the order of your results, just move the count from your select to the order by:

SELECT 
    MProduct.ProductCode, MProduct.ProductName
FROM 
    TProblem 
FULL OUTER JOIN
    MProduct ON TProblem.ProductCode = MProduct.ProductCode 
GROUP BY  
    MProduct.ProductCode, MProduct.ProductName 
ORDER BY 
    count(*) DESC
Sign up to request clarification or add additional context in comments.

Comments

-1

I understand where @marc_s is coming from. It looks like you are trying to get a list of DISTINCT rows

SELECT ProductCode, ProductName
FROM
(
SELECT TOP 100 PERCENT
    MProduct.ProductCode, MProduct.ProductName, COUNT(*) AS Ranges
FROM 
    TProblem 
FULL OUTER JOIN
    MProduct ON TProblem.ProductCode = MProduct.ProductCode 
GROUP BY  
    MProduct.ProductCode, MProduct.ProductName 
ORDER BY 
    Ranges DESC
) AS DATA

Or alternatively

SELECT DISTINCT 
    MProduct.ProductCode, MProduct.ProductName
FROM 
    TProblem 
FULL OUTER JOIN
    MProduct ON TProblem.ProductCode = MProduct.ProductCode 
    MProduct.ProductCode, MProduct.ProductName 

3 Comments

hey i am getting the follwing error for this query .The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
The inner query requires a TOP 100 PERCENT to cater for ORDER BY. Answer has been updated. This answer also assumes you are using SQL Server
The multi-part identifier "MProduct.ProductName" could not be bound. this is next error

Your Answer

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