In my dataset, I'm trying to find how many Stores have had at least 10 events where the value of each event was greater than $100.
SELECT COUNT(EventId) >=10
FROM Event
WHERE Event.EventValue >100
GROUP BY Site.SiteName
I appear to just be getting 1's and 0's instead of getting the site names that fit this criteria
Should work in your case.
SELECT CASE WHEN COUNT(*) >= 10 THEN 1 ELSE 0 END
FROM (
SELECT COUNT(EventId) AS T
FROM Event
WHERE Event.EventValue >100
GROUP BY Site.SiteName
) DATA
Try this:
SELECT
COUNT(1)
FROM
(
SELECT
eventid
FROM
events
WHERE
eventvalue > 100
GROUP BY
eventid
HAVING
COUNT(1) >= 10
)
Your query is returning 0 and 1 to mean FALSE and TRUE, because your SELECT clause is asking whether the COUNT(EventId) is greater than 10. Instead, try selecting the SiteId and putting your COUNT > 10 criterion in a HAVING clause.
Try this query.
select Site.SiteId, Site.SiteName
from (select SiteId, count(distinct EventId)
from Event
where EventValue > 100
group by SiteId
having count(distinct EventId) >= 10
) EV
join Site
on EV.SiteId = Site.SiteId
;
1/0 are the answer as it is showing True/False as your SELECT cause is asking for the count greater than 10 not the name. It should work if you ask for the SiteID and look at count >10 as it will show names not a true false answer.
Related
select count(patientNUM) as totalpatients
from [dbo] (nolock)
where patientId in (
'97210219',
'97210221',
'97210222'
)
50
100
20
So each patientsID contain numbers of patients, 100, 20 or 50. And I want to go through each patient rows and to lost them as partial or full. For example if there are 40 of 50 patientID rows, it will list as partial. If 50, it will list as full. Is there way to use count or in at the same time?
So basically I want to create two columns, patientID, and fullorpartial in the second column.
Is there way to go through each row and count each rows and then return and compare the result in a second column?
You need to know the "capacity" as well as the patientId. I would suggest a derived table:
select t.patientId,
(case when count(*) < v.capacity then 'partial'
when count(*) = v.capacity then 'full'
end) as full_or_partial
from t join
(values ('97210219', 50),
('97210221', 100),
('97210222', 20)
) v(patientId, capacity)
on v.patientId = t.patientId
group by t.patientId;
Since I don't know what exactly your data looks like and what you want. But try this:
use over is a good choice
select patientId,count(patientNUM) over(partition by patientId) as totalpatients
from [dbo] (nolock)
where patientId in (
'97210219',
'97210221',
'97210222'
)
this will count patientNUM exist times for each patientId.
And about the 'partialorfull' col I think can achieve by using case
case
when patientId = '97210219' and totalpatients < 50 then 'partial'
when ...... --condition keep going on
else 'full'
end as partialorfull
Every time I used 'Count()' to count the duplicate PointIDs' on the query I get this error.
I have narrowed down the problem is with Count() function, used MAX() with Group by and didn't have any problem. This is on access database populated using ODBC connection. All the help is appriciated - I have done all the research and this is my last online resort.
SELECT Event1.PointID, Event1.LogTimeStamp, Count(Event1.PointID) AS acount
FROM Event1
GROUP BY Event1.PointID, Event1.LogTimeStamp;
I suspect you actually want to return all duplicate records which is a 2 step operation.
Step 1 get find the ids which are duplicated
SELECT Event1.PointID, Count(*) AS NumOfRecords
FROM Event1
GROUP BY Event1.PointID
HAVING COUNT(*) > 1
Step 2 join that result back to the original table to find the records
SELECT e.*, d.NumOfRecords
FROM
Event1 e
INNER JOIN (
SELECT Event1.PointID, Count(*) AS NumOfRecords
FROM Event1
GROUP BY Event1.PointID
HAVING COUNT(*) > 1
) d
ON e.PointId = d.PointId
This is the standard syntax for what you want to do:
SELECT Event1.PointID, Event1.LogTimeStamp, Count(*) AS acount
FROM Event1
GROUP BY Event1.PointID, Event1.LogTimeStamp;
I have the following query which as you can see does multiple Count(CompetitorID) calls. Is this a performance issue, or does SQL Server 2008 'cache' the Count? If this is a performance issue, is it possible to store the Count to prevent the multiple lookups?
SELECT EventID,Count(CompetitorID) AS NumberRunners,
CASE WHEN Count(CompetitorID)<5 THEN 1
WHEN Count(CompetitorID)>=5 AND Count(CompetitorID)<=7 THEN 2
ELSE 3 END AS NumberPlacings
FROM Comps
GROUP BY EventID Order By EventID;
Its always a better practice to get the value once and use it subsequently whenever possible. In your case, you can always use Inner query to get the count only once and compute other (derived) columns off its value as shown below:
SELECT EventID, NumberRunners,
CASE WHEN NumberRunners <5 THEN 1
WHEN NumberRunners >=5 AND NumberRunners <=7 THEN 2
ELSE 3
END AS NumberPlacings
FROM (
SELECT EventID,
NumberRunners = Count(CompetitorID)
FROM Comps
GROUP BY EventID
) t
Order By EventID;
simplest would be this:
SELECT EventID,Count(distinct CompetitorID) AS NumberRunners,
CASE WHEN Count(distinct CompetitorID)<5 THEN 1
WHEN Count(distinct CompetitorID)>=5 AND Count(distinct CompetitorID)<=7 THEN 2
ELSE 3 END AS NumberPlacings
FROM Comps
GROUP BY EventID Order By EventID;
Could you please Explain this strange behavior of count (distinct client_id) statement.
for :
SELECT count(distinct client_id)
from clients_data where bank_name ='SABB' and tstatus = 0
I have got 6000 rows
and for
SELECT count(distinct client_id)
from clients_data where bank_name ='SABB' and tstatus = 0 and cif_type = 'CARD'
I have got 5964
and for
SELECT count(distinct client_id)
from clients_data where bank_name ='SABB' and tstatus = 0 and cif_type = 'LOAN'
I have got 42 rows .
but
5964 + 42 = 6006
and the first query gets only 6000 .
where these extra 6 rows come from ?
Note : Data now is not Available for any suggestions .
the image is here :
There could be six client_id that have both a CARD and a LOAN.
SELECT client_id FROM clients_data GROUP by client_id HAVING count(client_id) > 1
Do you have multiple records for a specific client_id? Running the above will tell you.
Suggest 6 of your client_ids appear in rows with a cif_type of 'LOAN' and also in rows with a cif_type of 'CARD'. Try counting rows in your 3 conditions without the DISTINCT. You should find the numbers then add up.
May be u have some values which have empty or null cif_type .
I have a table called trends_points, this table has the following columns:
id (the unique id of the row)
userId (the id of the user that has entered this in the table)
term (a word)
time (a unix timestamp)
Now, I'm trying to run a query on this table which will get the rows in a specific time frame ordered by how many times the column term appears in the table during the specific timeframe...So for example if the table has the following rows:
id | userId | term | time
------------------------------------
1 28 new year 1262231638
2 37 new year 1262231658
3 1 christmas 1262231666
4 34 new year 1262231665
5 12 christmas 1262231667
6 52 twitter 1262231669
I'd like the rows to come out ordered like this:
new year
christmas
twitter
This is because "new year" exists three times in the timeframe, "christmas" exists twice and "twitter" is only in one row.
So far I've asummed it's a simple WHERE for the specific timeframe part of the query and a GROUP BY to stop the same term from coming up twice in the list.
This makes the following query:
SELECT *
FROM `trends_points`
WHERE ( time >= <time-period_start>
AND time <= <time-period_end> )
GROUP BY `term`
Does anyone know how I'd do the final part of the query? (Ordering the query's results by how many rows contain the same "term" column value..).
Use:
SELECT tp.term,
COUNT(*) 'term_count'
FROM TREND_POINTS tp
WHERE tp.time BETWEEN <time-period_start> AND <time-period_end>
GROUP BY tp.term
ORDER BY term_count DESC, tp.term
See this question about why to use BETWEEN vs using the >=/<= operators.
Keep in mind there can be ties - the order by defaults to alphabetically shorting by term value when this happens, but there could be other criteria.
Also, if you want to additionally limit the number of rows/terms coming back you can add the LIMIT clause to the end of the query. For example, this query will return the top five terms:
SELECT tp.term,
COUNT(*) 'term_count'
FROM TREND_POINTS tp
WHERE tp.time BETWEEN <time-period_start> AND <time-period_end>
GROUP BY tp.term
ORDER BY term_count DESC, tp.term
LIMIT 5
Quick answer:
SELECT
term, count(*) as thecount
FROM
mytable
WHERE
(...)
GROUP BY
term
ORDER BY
thecount DESC
SELECT t.term
FROM trend_points t
WHERE t.time >= <time-period_start> AND t.time <= <time-period_end>
ORDER BY COUNT(t.term) DESC
GROUP BY t.term
COUNT() will give you the number of rows in the group, so just order by that.
SELECT * FROM `trends_points`
WHERE ( `time` >= <time-period_start> AND `time` <= <time-period_end> )
ORDER BY COUNT(`term`) DESC
GROUP BY `term`