Solved

VB.net SQL Count number of x and -

Posted on 2013-10-31
3
359 Views
Last Modified: 2013-10-31
Hi

I am using the following SQL query to get the result below

Select [Date],[Time],[UserID],[Name],[Gratitude], [Reframing], [Empathy], [Adaptable], [Teamwork] From Diary2 Where CompanyID = '" & Me.Label_Company.Text & "'"

How do I modify the query to get a count of the number of "x" entries for each email address
in the column UserID


1
0
Comment
Question by:murbro
3 Comments
 
LVL 4

Accepted Solution

by:
rshq earned 500 total points
Comment Utility
Hi

please test this query

select UserId,sum(Countx) as Totalx
from (select  UserId,
            (case when [Gratitude]='x' 1 else 0)+
            (case when [Reframing]='x' 1 else 0)+
            (case when [Empathy]='x' 1 else 0)+
            (case when [Adaptable]='x' 1 else 0)+
            (case when [Teamwork]='x' 1 else 0) as Countx
 From Diary2 Where CompanyID = '" & Me.Label_Company.Text & "'")
group by UserId

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
Another way is to add a subquery in your T-SQL that counts the email addresses, then JOIN to your Dairy2 table, then add the count to your SELECT clause
Select [Date],[Time],Dairy2.[UserID], uic.UserID_Count, [Name],[Gratitude], [Reframing], [Empathy], [Adaptable], [Teamwork] 
From Diary2 
  JOIN (SELECT UserID, COUNT([UserID]) as UserID_Count FROM Diary2 Where CompanyID = '" & Me.Label_Company.Text & "' GROUP BY UserID) uic ON Dairy2.UserID = uic.UserID
Where CompanyID = '" & Me.Label_Company.Text & "'

Open in new window


<edit>
Disregard, didn't read the question correctly.
0
 

Author Closing Comment

by:murbro
Comment Utility
thanks very much
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now