Click here to Skip to main content
15,906,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem,I have a table
ImpID 001
Number01 X
Number02 X
NumBer03 X
Number04 X
........
Number31 X

I were Load Report:

EmpID01020304....31SUM
001XXXX....X31


How I can query by SQL for count char 'X'?I plan to convert rows into columns, but I query fails. Please help me. Thanks all
Posted
Comments
Maciej Los 7-Jun-12 13:19pm    
Not clear! Please, provide more details. What exactly is the structure of table (example data with column-headers)? Please, elaborate it. Use "Improve question" button.
What have you done till now? Show us your query...
Corporal Agarn 7-Jun-12 13:41pm    
Does your table have only two columns? Does the EmpID changing signify that the count is to start again? Can there be missed NumberXX say no Number04 but a Number05?
As Iosmac said please show what you have done.

I think you should not change rows to columns...What I estimate from your question is:

You have (should have)
a.) a table EMP with fields
EMP.EMPID
EMP.ANYTHING_OTHER
EMP.THIS_AN_THAT

b.) a table EMP_DTL with fields
EMP_DTL.EMPID
EMP_DTL.EMP_DTL_POS
EMP_DTL.NUMBER

If this assumption is right you can easily solve your request.
SQL
SELECT 
	COUNT(*)
FROM EMP_DTL
WHERE EMP_DTL.EMPID = '001' AND EMP_DTL.NUMBER = 'X'



or better with parameters to count the "X" or the "Y" (query parameter :NUMBER) etc. for any Imp (parameter :EMPID)

XML
SELECT
    COUNT(*)
FROM EMP_DTL
WHERE EMP_DTL.EMPID = :EMPID AND EMP_DTL.NUMBER = :NUMBER



And like this you much more possibilities using GROUP BY, HAVING...

Hope I estimated right. Regards.
 
Share this answer
 
Comments
Manas Bhardwaj 7-Jun-12 14:46pm    
Good answer 5!
Let's say, you have a table name MyData like this: EmpID VARCHAR(3), Number VARCHAR(3), IsSelected VARCHAR(3)
It stores the values:

EmpIdNumberIsSelected
00101x
00102x
00103x
00104x
00105x
...
00131x


SQL
DECLARE @sqry NVARCHAR(2000)
DECLARE @dtqry NVARCHAR(2000)
DECLARE @pvqry NVARCHAR(2000)
DECLARE @cols NVARCHAR(2000)

--select all Numbers to use it as dynamic columns
SET @cols = STUFF(( SELECT DISTINCT '],[' + [Number]
		FROM [dbo].[Mydata]
		ORDER BY '],[' + [Number]
		FOR XML PATH('')), 1, 2, '') + ']'

--count numbers for each empid
SET @dtqry = 'SELECT [EmpId], [Number] , COUNT([IsSelected]) AS [CIS] ' +
		'FROM [dbo].[MyData] ' +
		'GROUP BY [EmpId]'
EXECUTE (@dtqry)

--pivot empid for number
SET @ptqry = 'SELECT [EmpId], ' + @cols + ' ' +
			'FROM (' + @dtqry + ') AS DT ' + 
			'PIVOT (SUM(DT.[CIS]) FOR DT.[Number] IN (' + @cols + ')) AS PT ' + 
			'ORDER BY PT.[EmpId]'
EXECUTE (@ptqry)


Resultset:

EmpId0102030405...31
00111111...1


More about: Using Pivot and Upivot[^]

How to get total? See my answers in similar discussions (examples):
Find 3 item association rules in MS SQL[^]
rows to columns change in sql[^]
Problem in calculation[^]
 
Share this answer
 
v2
Comments
Manas Bhardwaj 7-Jun-12 14:46pm    
Very nice 5+ :)
Maciej Los 7-Jun-12 14:49pm    
Thank you, Manas ;)
Sandeep Mewara 7-Jun-12 14:47pm    
Good answer 5+
Maciej Los 7-Jun-12 14:49pm    
Thank you, Sandeep ;)
codeBegin 8-Jun-12 2:19am    
good one, nicely formatted +5

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