aggregate-functions

SQL and Counting

南楼画角 提交于 2019-12-13 09:19:00
问题 I have a table which consists of the following Student Name Grade Class -------------------------------------------------------- User 1 A English User 1 B Math User 2 B Math I'm trying to create a query where it will list all students, total pass grades (grades A,B & C), total A's, Total B's, Total C's Can someone help me create the query or atleast put me in the right direction? 回答1: Give this a try: select name, count(case when grade in ('A', 'B', 'C') then 1 end) totalPass, count(case when

How to best calculate n-level aggregation data based on (n-1)-level data (Oracle)

淺唱寂寞╮ 提交于 2019-12-13 09:10:29
问题 In this answer (with all executable simplified and documented sample code + helpful comments) I did a kind of a trick there to calculate the last two rows of the following table: DESCR SUM ---------------------------------- ---------- money available in 2013 33233235.3 money spent in 2013 4253235.3 money bound to contracts in 2013 34333500 money spent 2013 in % of available 12 money bound 2013 in % of available 103 Does anybody know of a better (performance, amount of code, understandability)

How to compare scientific notation and decimal numbers in mysql MIN() aggregate function?

这一生的挚爱 提交于 2019-12-13 07:02:13
问题 How can i apply mysql MIN() over mixed decimal value and scientific notation values. I need to get minimum distance using latitude longitude equation in a mysql query with MIN(). It is working if the distance are decimal. But it is not working properly, if some distance are too small like "5.89872212195226e-05" The problem is MIN(4,5.89872212195226e-05) in a mysql query will always return '4'. Here is the part of query MIN ( (acos(sin((".$searchLat."*pi()/180)) * sin((b_loc.locLatitude*pi()

Postgres nested SQL query to count field

别等时光非礼了梦想. 提交于 2019-12-13 05:58:59
问题 I have a few tables: users - id users_matches - user_id, match_id, team matches - id, winner I'd like to count how many wins, losses, and ties a user has. team is an integer that could be 1 or 2. winner is also an integer, but it can be 1 (team 1 wins), 2 (team 2 wins), or 3 (tie). I'm not sure how to mix a grouped count with a nested query in Postgres. 回答1: Assuming referential integrity and Postgres 9.4: SELECT *, total - wins - ties AS losses FROM ( SELECT count(*) AS total , count(*)

Sum two columns that corresponds a agregate from a columns of two tables

柔情痞子 提交于 2019-12-13 05:15:14
问题 Yesterday I posted a question on which said sum of columns of a table. See here! However, I got into some other complications. I have a second table that needs to have a column added in correspondence with the first. Here's what I need: The column 'sum2' is new and I am not able to sum it correctly! 回答1: I'm not sure where the issue is, without seeing your code. I built a small sample in SQLFIddle: SQLFiddle It's giving me the same result as you have in your post: IDPARENT COLUMN_1 COLUMN_2 1

SQL Group By Having Where Statements

社会主义新天地 提交于 2019-12-13 04:57:19
问题 I have a MS Access table tracking quantities of products at end month as below. I need to generate the latest quantity for a specified ProductId at a specified date e.g. The Quantity for ProductId 1 on 15-Feb-12 is 100 , The Quantity for ProductId 1 on 15-Mar-12 is 150 . ProductId | ReportingDate | Quantity| 1 | 31-Jan-12 | 100 | 2 | 31-Jan-12 | 200 | 1 | 28-Feb-12 | 150 | 2 | 28-Feb-12 | 250 | 1 | 31-Mar-12 | 180 | 2 | 31-Mar-12 | 280 | My SQL statement below bring all previous values

SQL Statement to eliminate duplicates based on value in another column

帅比萌擦擦* 提交于 2019-12-13 04:33:57
问题 I have data that is in two tables and I have a query combining the data as below. I am trying to eliminate duplicates based on the Id column where I pick the record with the oldest split date. Could anyone kindly assist me with the SQL statement to effect this? |ID |SecID |ReportingDate |SplitDate |Adjustor| |1465 |2 |31-Dec-09 |01-Nov-10 |0.1 | |1465 |2 |31-Dec-09 |27-Dec-12 |0.2 | |1466 |2 |31-Dec-10 |27-Dec-12 |0.2 | |1468 |2 |31-Dec-11 |27-Dec-12 |0.2 | |1469 |2 |31-Dec-08 |01-Nov-10 |0.1

Correcting the SQL syntax?

匆匆过客 提交于 2019-12-13 03:24:49
问题 Thanks very much for those responded. Is there a way in SQL server that takes the data from table1 and outputs the data like table2? Thanks! Table1: +---------+-----------+----------+------------------+ | Name | DOB | Agent ID | Agent Name | +---------+-----------+----------+------------------+ | subject | 4/20/1960 | 4444 | Smith | +---------+-----------+----------+------------------+ | subject | 4/20/1960 | 4444 | John | +---------+-----------+----------+------------------+ | subject | 4/20

Aggregation of an expression in Django query spanning multiple tables

大城市里の小女人 提交于 2019-12-13 02:35:12
问题 Is it possible to compute an aggregation (ex.: sum) of an expression (ex.: subtraction) of fields from a related table in a Django ORM query? For a full example of what I'm trying to achieve, see this working SQL Fiddle. I broke it down into two questions (other one here). In this case, I have a model Sequence that represents a sequence of numbers, and a model Part that represent a "link" in this sequence: Sequence Sequence Sequence Sequence ... 0 5 20 15 ... |-- Part --|-- Part --|-- Part --

CASE expressions with MAX aggregate functions Oracle

倖福魔咒の 提交于 2019-12-13 02:24:09
问题 Using Oracle, I have selected the title_id with its the associated month of publication with: SELECT title_id, CASE EXTRACT(month FROM pubdate) WHEN 1 THEN 'Jan' WHEN 2 THEN 'Feb' WHEN 3 THEN 'Mar' WHEN 4 THEN 'Apr' WHEN 5 THEN 'May' WHEN 6 THEN 'Jun' WHEN 7 THEN 'Jul' WHEN 8 THEN 'Aug' WHEN 9 THEN 'Sep' WHEN 10 THEN 'Oct' WHEN 11 THEN 'Nov' ELSE 'Dec' END MONTH FROM TITLES; Using the statement: SELECT MAX(Most_Titles) FROM (SELECT count(title_id) Most_Titles, month FROM (SELECT title_id,