0

I have one table having two columns named OrderId and OrderType

OrderId OrderType
------- ---------
1       Drawer
2       Pickup
3       Delivery
4       Delivery
5       Drawer

In my stored procedure I want to pass the argument of OrderType which would return a result only on matching OrderType Column data to the argument i.e. if I pass the argument 'Drawer' then it should return count of OrderType column where OrderType column data is 'Drawer' and so on.

How can i do that using Count?

Currently I am doing:

select @tempvar1=case @tempvar
when 'Drawer'
Then Count(CASE_TABLE.OrderType) --What Should i put here to get desired result?
end
select @tempvar1

Instead of counting the whole column I want only the count matching OrderType.

3
  • Are you asking where to use a WHERE clause to get your specific data? If so use a WHERE clause after your second select like you would in a normal query. Commented Feb 26, 2014 at 15:35
  • No i am asking What should i put in count to get the desired matching data instead of counting whole column rows Commented Feb 26, 2014 at 15:36
  • See @MattJohnson answer below. It look like it does what you want with Count() Commented Feb 26, 2014 at 15:39

2 Answers 2

1

I'd so something like this:

DECLARE @argument VARCHAR(9) = 'Drawer'

SELECT
  OrderType,
  COUNT(OrderType) AS [Count]
FROM
  [Order]
WHERE
  OrderType = @argument
GROUP BY
  OrderType

Results

OrderType Count
--------- -----
Drawer    2

You can test it out at SqlFiddle here.

Sign up to request clarification or add additional context in comments.

1 Comment

This is assuming SQL Server 2008+ (the DECLARE statement)
0

Invoke SP:-

Exec GetOrderType 'Drawer'

SP:-

CREATE PROCEDURE GetOrderType
     @OrderType nvarchar(50)
AS
BEGIN
    SET NOCOUNT ON;
    SELECT 
        COUNT(*) 
    FROM
        your_table_name
    WHERE
        OrderType = @OrderType
    GROUP BY
        OrderType

END
GO

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.