group-by

AVG + Inner join

随声附和 提交于 2020-01-16 05:11:34
问题 I'm trying to make something like a chart list and my actual query is like this: SELECT user.username, songs.*, albums.desc, albums.release, albums.name, AVG(songrating.rating) FROM songs INNER JOIN user ON songs.userid=user.id INNER JOIN albums ON songs.albumid=albums.id INNER JOIN songrating ON songs.id=songrating.songid GROUP BY songrating.songid it only shows entries with at least one rating entry, but I also want these without a rating I tried to use it with if / case but it doesn't work

AVG + Inner join

北城余情 提交于 2020-01-16 05:11:15
问题 I'm trying to make something like a chart list and my actual query is like this: SELECT user.username, songs.*, albums.desc, albums.release, albums.name, AVG(songrating.rating) FROM songs INNER JOIN user ON songs.userid=user.id INNER JOIN albums ON songs.albumid=albums.id INNER JOIN songrating ON songs.id=songrating.songid GROUP BY songrating.songid it only shows entries with at least one rating entry, but I also want these without a rating I tried to use it with if / case but it doesn't work

PostgreSQL: Grouping then filtering table, with condition for nonexistence

こ雲淡風輕ζ 提交于 2020-01-16 04:15:07
问题 In PostgreSQL, I have a table that, abstractly, looks like this: ╔═══╦═══╦═══╦═══╗ ║ A ║ B ║ C ║ D ║ ╠═══╬═══╬═══╬═══╣ ║ x ║ 0 ║ y ║ 0 ║ ║ x ║ 0 ║ x ║ 1 ║ ║ x ║ 1 ║ y ║ 0 ║ ║ x ║ 1 ║ z ║ 1 ║ ║ y ║ 0 ║ z ║ 0 ║ ║ y ║ 0 ║ x ║ 0 ║ ║ y ║ 1 ║ y ║ 0 ║ ╚═══╩═══╩═══╩═══╝ I want to transform it in a query into this: ╔═══╦═══╦══════╗ ║ A ║ B ║ D ║ ╠═══╬═══╬══════╣ ║ x ║ 0 ║ 1 ║ ║ x ║ 1 ║ null ║ ║ y ║ 0 ║ null ║ ║ y ║ 1 ║ 0 ║ ╚═══╩═══╩══════╝ …such that: The input table’s rows are grouped by A and B, and

Different result query when use mysql and mariadb

自闭症网瘾萝莉.ら 提交于 2020-01-16 04:07:11
问题 Here is my problem: My database have table Book, Post. Each book has many post Table posts has field ' book_id ', that is foreign key reference table Book primary key (id). This is my index page. The idea is to get latest post from each book and order by published date. When I code on localhost, every thing is OK. I can get latest post from each book and order by publish date. But when I deploy it in vps. It didn't get latest post, it get first post from each book. I didn't have any

MySql :: Group by where date time max

泪湿孤枕 提交于 2020-01-16 01:29:28
问题 I have a table result like this idCHAT USER_NAME TEXT CURRENT_DATE_TIME CHAT_COUNTERPARTY party 54 taruna red hat 2014-10-30 05:44:26 pooja pooja 55 pooja cat 2014-10-30 05:45:48 taruna pooja After executing below query SELECT `idCHAT`, `USER_NAME`, `TEXT`, `CURRENT_DATE_TIME`, `CHAT_COUNTERPARTY`, IF(USER_NAME='taruna',`CHAT_COUNTERPARTY`,`USER_NAME`) AS party FROM ( SELECT c.* FROM chat c JOIN ( SELECT MAX(`CURRENT_DATE_TIME`) AS CURRENT_DATE_TIME, `CHAT_COUNTERPARTY` FROM chat GROUP BY

Unique results for SQL command with GROUP, MIN and NULL values

北城余情 提交于 2020-01-15 12:15:47
问题 I have a table inquiry with columns job , gender ( TRUE for women, FALSE for men) and salary . None of the columns is unique, salary may contain NULL . How to find the minimum salary per job for women ( TRUE )? If e.g. there are entries [pilot ; TRUE ; 100] , [pilot ; FALSE ; 100] , [pilot ; TRUE ; NULL] and [pilot ; FALSE ; 120] , the code below returns [pilot ; 100] twice instead of once. SELECT TOP (100) PERCENT T.JOB, T.SALARY FROM INQUIRY AS T INNER JOIN (SELECT JOB, MIN(SALARY) AS SL

Core Data Group By Monthly and Sum

南楼画角 提交于 2020-01-15 11:36:22
问题 Is there any way to GROUP BY core data based on its month then SUM its properties? I have data like this ------------------------ date | amount ------------------------ 30-08-2017 | 124000000 28-10-2017 | 124000000 16-10-2017 | 124000000 14-12-2017 | 124000000 20-12-2017 | 124000000 ------------------------ I just need to show one latest year, so I created my fetch request like this NSDate *now = [NSDate date]; NSDate *oneYearAgo = [now addYears:-1]; NSEntityDescription *entity =

How to group this data so I can pull out specific rows from each group

老子叫甜甜 提交于 2020-01-15 10:44:03
问题 I have the dataset shown below. From this, I want to select the first row from each group where the PersonID s status has changed to a different status than the previous one. For example, from this dataset, I would want the rows 1, 4, 7 and 11. Any help on this? If I do a GROUPBY , it just lumps together all New and all Pending in 2 groups. PersonID Status WhenChanged 101 New 27/01/2017 15:27 101 New 27/01/2017 16:40 101 New 27/01/2017 16:40 101 Pending 27/01/2017 16:40 101 Pending 27/01/2017

Dplyr summarise with list of function and dependence on other data column

假装没事ソ 提交于 2020-01-15 10:15:30
问题 Sorry for the terrible title, but it's hard to explain. I have the following data and functions I want to summarize the data with: library(tidyverse) # generate data df <- map(1:4, ~ runif(100)) %>% set_names(c(paste0('V', 1:3), 'threshold')) %>% as_tibble() %>% mutate(group = sample(c('a', 'b'), 100, replace = T)) # generate function list fun_factory_params <- 1:10 fun_factory <- function(param){ function(v, threshold){ sum((v * (threshold >= 1/2))^param) } } fun_list <- map(fun_factory

Dplyr summarise with list of function and dependence on other data column

…衆ロ難τιáo~ 提交于 2020-01-15 10:15:28
问题 Sorry for the terrible title, but it's hard to explain. I have the following data and functions I want to summarize the data with: library(tidyverse) # generate data df <- map(1:4, ~ runif(100)) %>% set_names(c(paste0('V', 1:3), 'threshold')) %>% as_tibble() %>% mutate(group = sample(c('a', 'b'), 100, replace = T)) # generate function list fun_factory_params <- 1:10 fun_factory <- function(param){ function(v, threshold){ sum((v * (threshold >= 1/2))^param) } } fun_list <- map(fun_factory