aggregate-functions

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

梦想的初衷 提交于 2020-01-24 12:23:27
问题 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 USER record with id=5 along with Total number of Files uploaded by userid 5,Total Photos uploaded by user id 5,Total groups created by userid 5.Can i get all these

MAX function used with GROUP BY clause

限于喜欢 提交于 2020-01-24 12:05:30
问题 I have database with name, salary and department of employees. I need a query for getting employee(s) with highest salaries in each department. Database: create table test( employee_name VARCHAR(255), department VARCHAR(255), salary INT ); Data: INSERT INTO test(employee_name, department, salary) VALUES ("John", "DepartmentA", 1500), ("Sarah","DepartmentA", 1600), ("Romel","DepartmentA", 1400), ("Victoria","DepartmentB", 1400), ("Maria", "DepartmentB", 1600); My tries: 1.1 WHERE MAX(salary) =

Count max. number of concurrent user sessions per day

时光毁灭记忆、已成空白 提交于 2020-01-22 20:38:46
问题 Situation We have a PostgreSQL 8.4 database containing user sessions with login date/time and logout date/time per row. Our web application records this time and also handles the case when user does not logout explicitly (session timeout). So a login date/time and logout date/time are given in every case. Goal I need user statistics of the max number of concurrent sessions a day. So, I can say the following: "At 2015-03-16 the peak of concurrent users logged in was six ." Similar questions A

PySpark Numeric Window Group By

♀尐吖头ヾ 提交于 2020-01-20 08:20:06
问题 I'd like to be able to have Spark group by a step size, as opposed to just single values. Is there anything in spark similar to PySpark 2.x's window function for numeric (non-date) values? Something along the lines of: sqlContext = SQLContext(sc) df = sqlContext.createDataFrame([10, 11, 12, 13], "integer").toDF("foo") res = df.groupBy(window("foo", step=2, start=10)).count() 回答1: You can reuse timestamp one and express parameters in seconds. Tumbling: from pyspark.sql.functions import col,

Select grouped by column only, not the aggregate

岁酱吖の 提交于 2020-01-16 06:57:47
问题 In a MySql select statement involving aggregation, is it possible to select just the grouped by column without the aggregate? Basically I want to select IDs in subquery according to a criteria based on an aggregate, in this case the total payments to a client: select idclient, business_name from client where idclient in ( select idclient, sum(amount) as total from payment group by idclient having total > 100 ) ... but this fails with error Operand should contain 1 column(s) because the

Select grouped by column only, not the aggregate

岁酱吖の 提交于 2020-01-16 06:57:09
问题 In a MySql select statement involving aggregation, is it possible to select just the grouped by column without the aggregate? Basically I want to select IDs in subquery according to a criteria based on an aggregate, in this case the total payments to a client: select idclient, business_name from client where idclient in ( select idclient, sum(amount) as total from payment group by idclient having total > 100 ) ... but this fails with error Operand should contain 1 column(s) because the

Resolve aggregate and Non-aggregate mix error for IF results in Tableau

半城伤御伤魂 提交于 2020-01-16 05:15:30
问题 I am trying to do the following in Tableau. If the chosen key figure is sales or volume (non-aggregate fields) then call calculation1 else calculation 2. IF ATTR([Choose Key Figure])= "Sales" THEN [Periodic Calculation] ELSEIF ATTR([Choose Key Figure])= "Volume" THEN [Periodic Calculation] ELSEIF ATTR([Choose Key Figure])="profit" THEN [Periodic Calculation 2] ELSEIF ATTR([Choose Key Figure])="price per unit" THEN [Periodic Calculation 2] END But I am getting the following error: "Cannot mix

mysql aggregate functions in query with two joins gives unexpected results

老子叫甜甜 提交于 2020-01-16 05:06:16
问题 Given the following (very simplified) mysql table structure: products id product_categories id product_id status (integer) product_tags id product_id some_other_numeric_value I am trying to find every product that has an association to a certain product_tag, and that a relation to at least one category whichs status-attribute is 1. I tried the following query: SELECT * FROM `product` p JOIN `product_categories` pc ON p.`product_id` = pc.`product_id` JOIN `product_tags` pt ON p.`product_id` =

Array not being returned by SELECT query inside plpgsql test case

一笑奈何 提交于 2020-01-16 05:03:08
问题 SELECT query I am using: SELECT ARRAY[table_name,pg_size_pretty(table_size)] FROM ( SELECT table_name, pg_table_size(table_name) AS table_size, pg_indexes_size(table_name) AS indexes_size, pg_total_relation_size(table_name) AS total_size FROM ( SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name FROM information_schema.tables where table_schema not in ('pg_catalog', 'information_schema')and table_schema not like 'pg_toast%' ) AS all_tables ORDER BY total_size DESC ) AS

How to subset or aggregate large amounts of data so I can make separate pie charts

非 Y 不嫁゛ 提交于 2020-01-15 12:18:05
问题 My data looks like this: Trip_Set sex 119_4 hembra 119_4 hembra 119_7 hembra 161_7 macho 193_8 hembra 255_7 macho 271_6 hembra 271_6 macho 271_6 hembra 328_7 hembra 403_3 hembra 428_2 hembra 655_4 hembra As you can see, each Trip_Set has a number of males or females (some just have one). I wish to make a separate pie chart for each trip set to show the ratio of males to females for each. This is just a snippet of my dataset (the real one is much larger with hundreds of Trip_Sets). My code for