Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Table. The SID is declared nvarchar and the rest of the columns are declared with int datatypes. The int columns are populated with varying values of 1, 2 & 3 at random. My challenge is how to create a view or subprocedure that will add up the 1s, the 2s and the 3s Row by Row to produce separate result columns.
Posted
Updated 18-May-12 2:42am
v2
Comments
Om Prakash Pant 18-May-12 8:39am    
How do you want to display the result?
Wosu 18-May-12 9:06am    
The result should be displayed as a View. Thanks
Maciej Los 18-May-12 8:49am    
We need an example.
Wosu 18-May-12 9:41am    
Imagine that you need to keep record of how many times in the days of a week or month or year that Rain fell, Sun shine and Dew covered a certain geographic region. Now imagine that this parameters are to be recorded in the mornings, Afternoons, and Evenings. So if I decide to assign 1 for Rainfall, 2 for Sun shine and 3 for Dew Cover as nature presents them in the Morning, Afternoon and Evening of the days of a certain week for instance. I know that I will need to divide the sum total of the Sun shine and Dew cover by 2 and 3 respectively to be able to have the accurate number of times the event occured. OK, my challenge is how to create a View that would accurately Sum the times Rainfell(i.e the 1s) and the times Sun shine(i.e the 2s) and the times Dew covered(i.e the 3s) Row wish. Thanks
Mohamed Mitwalli 18-May-12 9:25am    
Share your code , what you did so far

1 solution

Still is not clear, but i've made an example.

I use PIVOT[^] statement to count.

Copy below code into MS SQK Managment Studio and paste into new query, than run.
SQL
IF NOT OBJECT_ID(N'#Weather') IS NULL 
	DROP TABLE #Weather

CREATE TABLE #Weather ([SID] VARCHAR(30), [TimeOfDay] VARCHAR(30), [Weather] VARCHAR(30))

INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Europe','Morning','Rainfall')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Europe','Afternoon','Sun')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Europe','Evening','Dew Cover')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Europe','Morning','Sun')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Europe','Afternoon','Sun')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Europe','Evening','Rainfall')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Europe','Morning','Dew Cover')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Asia','Morning','Sun')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Asia','Afternoon','Sun')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Asia','Evening','Rainfall')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Asia','Morning','Sun')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Asia','Afternoon','Sun')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Asia','Evening','Rainfall')
INSERT INTO #Weather ([SID] , [TimeOfDay], [Weather] )
	VALUES('Asia','Morning','Dew Cover')

DECLARE @cols VARCHAR(200)
DECLARE @dt VARCHAR(400)
DECLARE @pt VARCHAR(1000)

SET @cols = '[Dew Cover],[Rainfall],[Sun]'
SET @dt = 'SELECT * ' +
		'FROM #Weather '
SET @pt = 'SELECT [SID], [TimeOfDay], ' + @cols + ' ' + 
		'FROM (' + @dt + ') AS DT ' +
		'PIVOT (COUNT([Weather]) FOR [Weather] IN(' + @cols + ')) AS PT ' + 
		'ORDER BY PT.[SID]'
EXEC( @pt)

DROP TABLE #Weather


If you have any question, call ;)
 
Share this answer
 
Comments
Wosu 21-May-12 7:58am    
Thanks losmac. I appreciate ur time and codes, however in my case scenario, my Database Table t1 and its Attributes are connected programatically to 3 Combo Boxes that I created on a 5 VB.NET Window Forms to represent 5 different days for instance. The 3 Combo Boxes on the 5 different Forms are labelled Morning, Afternoon and Evening respectively. Each of the Combo Box provides the items- Rainfall; Sun shine; and Dew Cover. With this application interface, a User is only expected to select from the Combo Box the item that applies for the time of the day and finally clicks on Update button - which updates the Table t1 defualt value of zero(0). This is the logic by which t1 is populated. My challenge is just how to Add up the 1s returned for Rainfall, the 2s returned for Sunshine and the 3s returned for the Dew cover row-wise. In order to provide accurate result for the No. of times it Rained; The No. of times it Shine & No. of times Dew covered. For the number of days in question.

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