I hope I explain this right and I do not want to make this a store procedure
This query does work for me, but I want it to do something else. I want to modified it to do something else
I have table that changes every other day. I need one column from that table and the data from it. This column can have 200 – 350 rows in it every other day when it changes, and some of the values will be the same so I will need to group them together
I do not want to insert the value in it each day by hand so I am wondering what can I change in that so I do not need to do that
And
I do not want to add in the sum by hand in it either
This way when it changes every other day don’t need to do anything
Here is the query I have
Declare @testTable Table (Eggs int); --I do not want to add in the values by hand
Insert Into @testTable (Eggs)
Values (1), (1), (2), (2), (5), (3), (2), (4), (2), (4), (3), (2), (1);
Select *
From @testTable tt;
--==== Solution
Select egg1 = sum(Case When tt.Eggs = 1 Then 1 Else 0 End) -- I do not want to add sum by hand
, egg2 = sum(Case When tt.Eggs = 2 Then 1 Else 0 End)
, egg3 = sum(Case When tt.Eggs = 3 Then 1 Else 0 End)
, egg4 = sum(Case When tt.Eggs = 4 Then 1 Else 0 End)
, egg5 = sum(Case When tt.Eggs = 5 Then 1 Else 0 End)
From @testTable tt;
The out put would look like some like this
Egg1 | egg2 | egg3| egg4 | egg5
1 3 | 5 | 2 | 2 | 1