Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
TechNess
Starting Member
11 Posts |
Posted - 2011-01-31 : 23:26:55
|
| Hi All,I am having a problem with procedure.What could be the correction?Here is my table:ID Product Type1 A 12 A 13 A 24 B 15 B 26 B 27 C 1I wrote a stored procedure so that I my output will be like this:Product Total Type1 Type2A 3 2 1B 3 1 2C 1 1 0Here is my procedure:CREATE PROC myProcedure @product varchar(10), @type varchar(10)AS BEGIN if (@product = '' or @product = '0') set @product = null if (@type = '' or @type = '0') set @type = null SELECT Product, COUNT(*) AS Total, ISNULL((select COUNT(*) from Product where Product in (ISNULL(@product,Product)) and Type=1),0) AS Type1, ISNULL((select COUNT(*) from Product where Product in (ISNULL(@product,Product)) and Type=2),0) AS Type2 From Product Where Product = isnull(@product,Product) AND Type = isnull(@type, Type) Group by Product ENDHere is the Output which is wrong: exec myProcedure '','' Product Total Type1 Type2A 3 4 3B 3 4 3C 1 4 3when I execute as exec myProcedure 'A', '' it works fineProduct Total Type1 Type2A 3 2 1Thanks in Advance.. |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-02-01 : 00:30:04
|
| replace your select statement with this oneSELECT product ,COUNT(Id) total ,sum(case when type=1 then 1 else 0 end) as Type1 ,sum(case when type=2 then 1 else 0 end) as Type2From @casetableWhereProduct = isnull(@product,Product) AND Type = isnull(@type, Type)Group by Product |
 |
|
|
TechNess
Starting Member
11 Posts |
Posted - 2011-02-01 : 02:15:23
|
Thanks MIK_2008! It works perfect **TechNess** |
 |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-02-01 : 02:18:19
|
| yrw :) |
 |
|
|
|
|
|
|
|