MySQL: Count the distinct rows per day
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt FROM tableName GROUP BY DATE(timestamp) SQLFiddle Demo
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt FROM tableName GROUP BY DATE(timestamp) SQLFiddle Demo
Not necessarily the most efficient, but one of the shortest and most readable using C++: std::to_string(num).length()
I haven’t tested it, so the syntax might not be perfect, but what about something like this : select name, count(*) as frequency from your_table group by name order by count(*) desc Should give you unique names and the corresponding number of times each name appears in the table, ordered by that number.
The exact query can be produced using the tuple_() construct: session.query( func.count(distinct(tuple_(Hit.ip_address, Hit.user_agent)))).scalar()
Now let’s try some Java 8 code: static public Map<String, Integer> toMap(List<String> lst) { return lst.stream() .collect(HashMap<String, Integer>::new, (map, str) -> { if (!map.containsKey(str)) { map.put(str, 1); } else { map.put(str, map.get(str) + 1); } }, HashMap<String, Integer>::putAll); } static public Map<String, Integer> toMap(List<String> lst) { return lst.stream().collect(Collectors.groupingBy(s -> s, Collectors.counting())); } I think this … Read more
For Rails 3: Check out the ActiveRecord::Relation docs at the Rails 3 docs. # get the relation rel = User.complex_scope.chained_complex_scope # get the SQL # this does not execute the query sql = rel.to_sql # find out how many records # this executes the query behind the scenes count = rel.size
If you’re going for efficiency: import re count = sum(1 for _ in re.finditer(r’\b%s\b’ % re.escape(word), input_string)) This doesn’t need to create any intermediate lists (unlike split()) and thus will work efficiently for large input_string values. It also has the benefit of working correctly with punctuation – it will properly return 1 as the count … Read more
You can use a CASE expression with your aggregate to get a total based on the outcomeId value: select companyId, sum(case when outcomeid = 36 then 1 else 0 end) SalesCount, sum(case when outcomeid <> 36 then 1 else 0 end) NonSalesCount from yourtable group by companyId; See SQL Fiddle with Demo
var noticesGrouped = notices.GroupBy(n => n.Name). Select(group => new { NoticeName = group.Key, Notices = group.ToList(), Count = group.Count() });
Get-Content and Measure-Object are fine for small files, but both are super inefficient with memory. I had real problems with large files. When counting rows in a 1GB file using either method, Powershell gobbled up all available memory on the server (8GB), then started paging to disk. I left it over an hour, but it … Read more