1

In the below query I have a table name indent I have different indentid count for different locations. But it shows the indentid count for last location. I want to display all location indentID count.

I want to display count like this

    15
    20
     1
  • 15 - for first location
  • 20 - second location
  • 1 - third Location

Code:

 DECLARE @LocationID int

 SELECT @LocationID = LocationID FROM Locations

 SELECT COUNT(IndentID) AS OpenedIndent 
 FROM Indent I 
 WHERE POStatusID IN (1,2) 
   AND I.LocationID = @LocationID
2
  • It would help if you gave us a data sample of the table being queried. Commented May 13, 2015 at 7:25
  • It is problematic to use SELECT @LocationID = LocationID FROM Locations without a where condition because it is not limiting the results to 1 record (you can only assign 1 value to a scalar variable). Commented May 13, 2015 at 8:15

1 Answer 1

1

You need to use group by as below

SELECT COUNT(IndentID) AS OpenedIndent 
FROM Indent I 
WHERE POStatusID IN (1,2) 
GROUP BY I.LocationID
Sign up to request clarification or add additional context in comments.

1 Comment

Yes I don't want to see indentID .I want to display count of all locations .

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.