Click here to Skip to main content
15,882,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have the following table and produces the output as show in the last.



SQL
CREATE TABLE [dbo].[1test](
        [ID] [int] NOT NULL,
        [Plan] [int] NOT NULL,
        [Year] [int] NOT NULL,
        [1] [bit] NULL,
        [2] [bit] NULL,
        [3] [bit] NULL,
        [4] [bit] NULL,
        [5] [bit] NULL,
        [6] [bit] NULL,
        [7] [bit] NULL,
        [8] [bit] NULL,
        [9] [bit] NULL,
        [10] [bit] NULL,
        [11] [bit] NULL,
        [12] [bit] NULL,
        [13] [bit] NULL,
        [14] [bit] NULL,
        [15] [bit] NULL,
        [16] [bit] NULL,
        [17] [bit] NULL,
        [18] [bit] NULL,
        [19] [bit] NULL,
        [20] [bit] NULL,
        [21] [bit] NULL,
        [22] [bit] NULL,
        [23] [bit] NULL,
        [24] [bit] NULL,
        [25] [bit] NULL,
        [26] [bit] NULL,
        [27] [bit] NULL,
        [28] [bit] NULL,
        [29] [bit] NULL,
        [30] [bit] NULL,
        [31] [bit] NULL
    ) ON [PRIMARY]


INSERT [dbo].[1test]  VALUES (237, 100, 2013, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,0)
INSERT [dbo].[1test]  VALUES (227, 150, 2013, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1)
INSERT [dbo].[1test]  VALUES (257, 50, 2013, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,0)
INSERT [dbo].[1test]  VALUES (267, 200, 2013, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,1)



SELECT
  SUM( CASE WHEN [1]=0 THEN 0 ELSE 1 END+ CASE WHEN [2]=0 THEN 0 ELSE 1 END + CASE WHEN [3]=0 THEN 0 ELSE 1 END + CASE WHEN [4]=0 THEN 0 ELSE 1 END + CASE WHEN [5]=0 THEN 0 ELSE 1 END + CASE WHEN [6]=0 THEN 0 ELSE 1 END + CASE WHEN [7]=0 THEN 0 ELSE 1 END + CASE WHEN [8]=0 THEN 0 ELSE 1 END + CASE WHEN [9]=0 THEN 0 ELSE 1 END + CASE WHEN [10]=0 THEN 0 ELSE 1 END + CASE WHEN [11]=0 THEN 0 ELSE 1 END + CASE WHEN [12]=0 THEN 0 ELSE 1 END + CASE WHEN [13]=0 THEN 0 ELSE 1 END + CASE WHEN [14]=0 THEN 0 ELSE 1 END + CASE WHEN [15]=0 THEN 0 ELSE 1 END + CASE WHEN [16]=0 THEN 0 ELSE 1 END + CASE WHEN [17]=0 THEN 0 ELSE 1 END + CASE WHEN [18]=0 THEN 0 ELSE 1 END + CASE WHEN [19]=0 THEN 0 ELSE 1 END + CASE WHEN [20]=0 THEN 0 ELSE 1 END+ CASE WHEN [21]=0 THEN 0 ELSE 1 END + CASE WHEN [22]=0 THEN 0 ELSE 1 END + CASE WHEN [23]=0 THEN 0 ELSE 1 END + CASE WHEN [24]=0 THEN 0 ELSE 1 END + CASE WHEN [25]=0 THEN 0 ELSE 1 END + CASE WHEN [26]=0 THEN 0 ELSE 1 END + CASE WHEN [27]=0 THEN 0 ELSE 1 END + CASE WHEN [28]=0 THEN 0 ELSE 1 END + CASE WHEN [29]=0 THEN 0 ELSE 1 END + CASE WHEN [30]=0 THEN 0 ELSE 1 END + CASE WHEN [31]=0 THEN 0 ELSE 1 END)*[plan] 'Count'
FROM  dbo.[1test]
GROUP BY
[plan]



the above query return the values like Count 1450 2400 4200 5000

what changes should be made so that i'll get sum of above counts means 1450+ 2400+4200+5000
Posted

1 solution

Try this:
SQL
SELECT [ID], [Plan], [Year], ([1] + [2] ... + [31]) * [Plan] AS MyPlanCount
FROM [dbo].[1test]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900