aggregate-functions

MySQL Query - Using Aggregate and Group By to generate separate results

I have a table where one or more entries with the same 'id' value can be inserted into our log / fact table (contains over 100+ million records) At a set frequency a new record is inserted into this table with a new value for the columns 'created' and 'view_percent' (percentage of a video viewed). With two different queries, I would like...

mysql select data from multiple rows

I have a table id, name, keyword 1 bob guy 2 bob developer 3 mary girl 4 joe guy Q1 : What would be the sql to get back the row (bob) containing both keywords 'guy' AND 'developer'? Intuitively, I thought it'd be SELECT * FROM TABLE WHERE keyword = 'guy' AND keyword = 'developer' Q2: But I suppose the first cond...

MySQL percentage (a bit complex?)

There are a few of these around, but I am either slow tonight or I am trying to do something not possible. I have this setup `id` int(11) NOT NULL AUTO_INCREMENT, `score` float NOT NULL, `userId` int(11) NOT NULL `movieId` int(11) NOT NULL Taking that into account, I want to get a percentage of all the experiences from all the users i...

MySql Select Query

I have 2 tables like this Table 1 Id f1 f2 1 ABC red,green 2 DEF blue,yellow Table 2 id color value 1 red r 2 green g 3 blue b 4 yellow y How Can I get result like this f1 f2 values ABC red,green r,g DEF blue,yellow b,y Thanks i...

Getting multiple counts across joined tables

I have 3 related tables: business id name inspection id business_id date violation id inspection_id title I'd like to get out: [business.id, Count(Inspections), Count(Violations)] Where Count(Inspections) is the total number of inspections this business has had and Count(Violations) is the total number of violations...

mysql group by confusion

so i have this table; mysql> describe player_weapon_stats; +------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL |...

counting mysql values

I have a table with two fields, TAG_ID and TAG_DESC my primary is tag_id. I'm looking for a query that will display each TAG_DESC and the amount of times it occurs next to it. Thanks, ...

Problem using nested avg(..) aggregate function in hibernate hql

I am using HQL to get data through DAO class but it throws as error stated below : ERROR org.hibernate.hql.PARSER - <AST>:0:0: unexpected AST node: query Below is my Hql Query : select new com.shaikh.dto.UserResult ( user.userSurName, avg((select avg(v1.age) from com.shaikh.dto.UserResult v1 where v1.joinDate between to_date(:dayF...

SQL MAX and MIN in one column

I have a problem to get the max and the min value, I want the result to be XXL and XXS SELECT MAX(tblSizeXL.SizeXLName) AS maxSize, MIN(tblSizeXL.SizeXLName) AS minSize FROM Product JOIN tblSizeXL ON Product.SizeXLID = tblSizeXL.SizeXLID WHERE (Product.GroupID = @GroupID) GROUP BY tblSizeXL.SizeXLID ORDER BY ...

How can I use annotate() to count a subset of related models in Django?

I'm trying to use Django's annotate feature to add the count of a related model to a queryset. However, I don't want a full count of related objects, I only want to count the active ones (i.e., "is_active=True"). I can't figure out how to filter down the count. The (simplified) relevant models: class Post(models.Model): user = mode...

How can I combine sql aggregate functions with distinct more cleanly?

I've been running into the following case repeatedly lately, either I need to apply MAX() or SUM() to one column on a table, but I need DISTINCT sets of values for other columns. For example consider the following table and associated columns representing details of a login to StackOverflow. SoUserLogins (OpenIdToken, Name, IpAdress, L...

Slow search of minimum in list of maximums with sql with-clause

I have question about this query, a little bit slower for me :( My problem : I'm searching the minimum ticks in a list which contains each maximum of each list of ticks by seed. => Equal to min( max(seed1), max(seed2), max(seed 3), etc. ) PS : Each seed contains ~ 10000 rows (1 row = 1 ticks) PS2 : I have one table with contains all ...

Mysql - Count how many rows have a certain number?

Hello, I have a query that checks a tables and groups all entries from a user and counts those entries SELECT username, count(userid) FROM orders GROUP BY userid This returns a list of username's and how many orders they have submitted username count(userid) ------------------------ test 1 test2 1 tes...

SQL:Getting count from many tables for a user record in USER table.Whats the best approach ?

I have 4 SQL server(2008 version) tables 1) USER- to store user information (Fields : UserId,UserName) 2) FILES - to store files uploaded by user (FileId,FileName,UserId) 3) PHOTOS -to store files uploaded by user (PhotoId,PhotoName,UserId) 4) GROUPS= to store groups created by user ( GroupId,GroupName,UserId) Now I want to get a USE...

Using AVG() function between two tables

I have two tables, and I need to determine the company that offers the highest average salary for any position. My tables are as follows: employer eID (primary key), eName, location position eID (primary key), pName (primary key), salary) The code I wrote determines all avg salaries that are higher than one, but I need to find only ...

Simple SQL Problem

Hey...I am in a bind here. I am not much of a programmer but the guy who does the sql at my business is out sick. If I have a table like this (I am simplifying this a lot but this is where I am stuck). Name Object Payment Joe A 100 Jan A 200 Joe A 300 Ron A 500 Jan A 100 Joe B ...

MySQL Update Column +1?

Hello, I was wondering what would be the easiest way to update a column by +1? I will be updating a post count of a category based on when users submits a new post. Thanks. ...

How to do fast counting on large tables?

I have large MySQL tables with hundreds of thousands of rows. I need to write a query on a customers table which gets the count of when customers will be available to contact again. eg. SELECT 'This week', COUNT(*) FROM customers WHERE sales_person_id = 1 AND DATEDIFF(NOW(), available_date) < 7 UNION SELECT 'Next week', COUNT(*) ...

Help with Simple Query?

I have the following tables I need to find out the sum. Table A ID Name 1 Jason 2 Peter 3 Ravi Table B ID ID_SEC 1 11 1 12 1 13 2 21 2 22 2 23 3 31 3 32 3 33 Table C ID_SEC Value Include_Ind 11 100 Y 12 200 Y 13 300 N 21 10 Y 22 20 N 23 30 N 31 1000 N 32 2...

Oracle query to extract distinct routes

Burning out a brain cell... there has to be a simple way to do this. I've inherited the following tables: approval_path approval_path_steps applications application_roles requests role_approvals A user requests an application role for an application, which must go through an approval path, the steps of which are defined in approval_p...