Solved

VB.net SQL Count number of x and -

Posted on 2013-10-31
3
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

733 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