Solved

VB.net SQL Count number of x and -

Posted on 2013-10-31
3
364 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
ID: 39613520
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
ID: 39614113
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
ID: 39614131
thanks very much
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

861 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

24 Experts available now in Live!

Get 1:1 Help Now