aggregate-functions

Join two tables mysql, one to many relationship

自作多情 提交于 2019-12-24 14:14:22
问题 I have two tables: Points -> id bigint(20) NO PRI NULL auto_increment created_at datetime NO NULL ip varchar(255) NO NULL item_id bigint(20) NO MUL NULL updated_at timestamp YES NULL and Items -> id bigint(20) NO PRI NULL auto_increment author varchar(255) NO NULL created_at datetime NO NULL description varchar(255) NO NULL link varchar(255) NO NULL source varchar(255) NO NULL title varchar(180) NO NULL url_encoded varchar(255) NO UNI NULL updated_at timestamp YES NULL I want to join them

Showing Monthly Totals from Multiple Columns in PostgreSQL

ε祈祈猫儿з 提交于 2019-12-24 12:34:50
问题 I am looking to see the number of firstexam patients seen per month for a given date range this year vs last year, and compare that to the total number of patients seen per month for the same date range. I am able to set up the firstexam patients as follows: select case EXTRACT(month FROM patient_info.firstexam) when 1 then '01 - January' when 2 then '02 - February' when 3 then '03 - March' when 4 then '04 - April' when 5 then '05 - May' when 6 then '06 - June' when 7 then '07 - July' when 8

Aggregates not allowed in WHERE clause in postgreSQL error

◇◆丶佛笑我妖孽 提交于 2019-12-24 11:44:37
问题 I was trying to execute this following query, and I am a beginner in writing SQL Queries, was wondering how can I achieve the following aggregate functions functionality doing in WHERE clause. Any help on this would be a great learning curve for me. select a.name,a.add,a.mobile from user a INNER JOIN user_info aac ON aac.userid= a.userid INNER JOIN info ac ON aac.infoid= ac.infoid WHERE a.total < 8* AVG(ac.total) GROUP BY a.name, a.add, a.mobile; And this is the error I am getting in

Aggregate bitfield values with binary OR

扶醉桌前 提交于 2019-12-24 11:28:40
问题 I have a table with int values being used as bitfields (where each bit is a flag). Now I would like to aggregate them with a binary operation (in my case OR) so that: SELECT 1 AS bitfield INTO #TABLE UNION ALL SELECT 1 + 2 + 8 + 32 UNION ALL SELECT 2 + 128 UNION ALL SELECT 2 + 32 SELECT AND_AGGR(bitfield) -- Invalid because AND_AGGR doesn't exist FROM #TABLE DROP #TABLE would result in the value 171 What would be a good way to do this that hopefully doesn't require a lot of | and MAX (but if

nhibernate group by join query

强颜欢笑 提交于 2019-12-24 10:59:01
问题 I have the following entities: public class Shift { public virtual int ShiftId { get; set; } public virtual string ShiftDesc { get; set; } public virtual IList<ShiftHistory> ShiftHistory { get; set; } } public class ShiftHistory { public virtual System.DateTime ShiftStartLocal { get; set; } public virtual System.DateTime ShiftEndLocal { get; set; } public virtual Zone Zone { get; set; } public virtual Shift Shift { get; set; } public virtual int RowId { get; set; } } public class Zone {

PL/SQL Triggers with aggregate function

时光怂恿深爱的人放手 提交于 2019-12-24 09:36:46
问题 I have a trigger that uses the avg() function to calculate a student's gpa based on the number of course scores that are in the course table. Problem here is, when my trigger executes, the GPA parameter in the table is empty. Could anyone see the issue? drop table courses; drop table student; drop table assignments; create table student (sid integer, sname char(10), saddress char(10), gpa integer); create table courses (sid integer, cid integer, cgrade integer); create table assignments ( sid

Custom aggregate function in PostgreSQL

大城市里の小女人 提交于 2019-12-24 08:58:21
问题 Is it possible to write an aggregate function in PostgreSQL that will calculate a delta value, by substracting the initial (last value in the column) from the current(first value in column) ? It would apply on a structure like this rankings (userId, rank, timestamp) And could be used like SELECT userId, custum_agg(rank) OVER w FROM rankings WINDOWS w AS (PARTITION BY userId ORDER BY timstamp desc) returning for an userId the rank of the newest entry (by timestamp) - rank of the oldest entry

SQL query that reports N or more consecutive absents from attendance table

走远了吗. 提交于 2019-12-24 08:35:15
问题 I have a table that looks like this: studentID | subjectID | attendanceStatus | classDate | classTime | lecturerID | 12345678 1234 1 2012-06-05 15:30:00 87654321 12345678 1234 0 2012-06-08 02:30:00 I want a query that reports if a student has been absent for 3 or more consecutive classes. based on studentID and a specific subject between 2 specific dates as well. Each class can have a different time. The schema for that table is: PK(`studentID`, `classDate`, `classTime`, `subjectID,

SQL Server - Aggregate by number of records returned for all groups

强颜欢笑 提交于 2019-12-24 08:08:24
问题 Suppose I have the following table in my SQL Server 2012 database: MyTable: DateCol FkId Sector Value -------------------------------------------- 2018-01-01 1 A 1 2018-01-02 1 A 2 2018-01-03 1 A 3 2018-01-04 1 A 4 2018-01-01 1 B 1 2018-01-04 1 B 4 2018-01-01 1 C 1 2018-01-03 1 C 3 2018-01-04 1 C 4 2018-01-01 2 A 1 ... And I want to get the average values for each sector for a specific FkId , BUT BASED UPON THE TOTAL NUMBER OF DATES AVAILABLE IN TOTAL FOR THAT FkId . Meaning that if I wanted

SQL STUFF not working, why?

为君一笑 提交于 2019-12-24 07:28:32
问题 I did lot of querys on Oracle, and now I'm working with SQL Server. I saw the way of use similar function like listagg from oracle in sql server (stuff). Select sqd.id_question, STUFF((Select ',' + nm_departament from tb_departament where sqd.id_departament = id_departament for xml path('')),1,1,'') nm_departements from tb_survey_question_departament sqd The sintax its correct, but the result not. The goal is have for example for the 2 top rows, the result as 1 - RH, Planta Brasilia Where is