Solved

VB.net SQL Count number of x and -

Posted on 2013-10-31
3
371 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

810 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