window-functions

PostgreSQL - Referencing another aggregate column in a window function

倖福魔咒の 提交于 2019-12-25 04:19:15
问题 Here's the query: SELECT name, group_name, number1, -- associated with either the name or the group_name number2, ... (number1 * number2) - SUM(number3) AS total_difference, SUM(total_difference) OVER (PARTITION BY group_name) AS grand_total FROM t GROUP BY name, group_name, number1, number2, ...; A single group_name may have many associated names. total_difference is generated for each name. My goal is for grand_total to be the sum of the total_difference for every name in group_name. total

DB2 count(*) over(partition by fieldname) giving -104 z/OS version 7

独自空忆成欢 提交于 2019-12-25 03:26:35
问题 I have slimmed down the query to remove potential complications, in addition I have verified that the fields are correct. DB2 UDB zSeries V7 is my db2 version. SELECT STDINSTRCD, COUNT(*) OVER(PARTITION BY STDINSTRCD), CAST(STDINSTRDESC AS VARCHAR(1000)) AS INSTR, C.STDINSTRSEQ, 1 FROM SYST.SCC004 C WHERE C.STDINSTRCD = '098' I have tried a subquery as well. select H2.FRSTSTDINSTRCD, (select count(*) from SYST.scC004 Ci where '098'=Ci.STDINSTRCD) as cnt, cast(STDINSTRDESC as varchar(1000)), C

Partition by in Impala SQL throwing an error

北慕城南 提交于 2019-12-25 01:03:13
问题 I am trying to calculate the running total of loss by the months on Impala using TOAD The following query is throwing the error - select list expression not produced by aggregation output (missing from group by clause ) select segment, year(open_dt) as open_year, months, sum(balance) sum(loss) over (PARTITION by segment,year(open_dt) order by months) as NCL from tableperf where year(open_dt) between 2015 and 2018 group by 1,2,3 回答1: You are mixing aggregation and window functions. I think you

Calculate Final outcome based on Results/ID

北慕城南 提交于 2019-12-24 23:15:19
问题 For a Table T1 +----------+-----------+-----------------+ | PersonID | Date | Employment | +----------+-----------+-----------------+ | 1 | 2/28/2017 | Stayed the same | | 1 | 4/21/2017 | Stayed the same | | 1 | 5/18/2017 | Stayed the same | | 2 | 3/7/2017 | Improved | | 2 | 4/1/2017 | Stayed the same | | 2 | 6/1/2017 | Stayed the same | | 3 | 3/28/2016 | Improved | | 3 | 5/4/2016 | Improved | | 3 | 4/19/2017 | Worsened | | 4 | 5/19/2016 | Worsened | | 4 | 2/16/2017 | Improved | +----------+-

Sum over partition not working

倖福魔咒の 提交于 2019-12-24 20:03:18
问题 I've got some code with the partition function, but it's not working. I get an error message that says Incorrect syntax near 'Sales' Does anyone know why? I looked at the other partition questions, didn't find an answer, The code (below) is supposed to select PriceZoneID and Sales from the Aggregated Sales History table then sum up the total sales using the OVER function and put that data in a new column called Total Sales. It should then sum up the sales for each zone using the OVER

Row Level Security Enabled update is not working while standard update does using the same logic

大憨熊 提交于 2019-12-24 19:34:14
问题 Working on postgres 10.4 (on RDS if it makes a difference) I am trying to enforce application user permissions using Row Level Security. I have a permissions table which looks something like user_group_id | entity1 | entity2 | entity3 | permission ==============|=========|=========|=========|============ 1 |1 |null |null | write 1 |1 |1 |null | read entity1(root)-entity2-entity3(leaf) is a hierarchy of items stored in different tables. entity1 contains entity2 items, entity2 items contains

SQL Partition data within and outside of a Common Table Expression

百般思念 提交于 2019-12-24 12:38:30
问题 I am using SQL Server 2012 I have the following sample result set. The columns with the Tot Prefix are derived from the columns to the left. What I am trying to do is partition data for a given date, and a given AssetStyle. Based on this criteria I want to sum profit and sum ACB. I then want to divide the results of these two sums for a total return number. The sample data is below. ValuationDate CustodianaccountNum AssetStyle AssetClass Profit ACB NetReturn TotProfit TotACB TotReturn 3/31

Oracle SQL, fill missing value with the closest non-missing

怎甘沉沦 提交于 2019-12-24 07:36:32
问题 I have a dataset in which I want to fill missing values witht the closest non-missing value. I found two elegant solutions in the answers to this question, but I don't understand why they are not working for me. Table: create table Tab1(data date, V1 number); insert into Tab1 values (date '2000-01-01', 1); insert into Tab1 values (date '2000-02-01', 1); insert into Tab1 values (date '2000-03-01', 1); insert into Tab1 values (date '2000-04-01', 1); insert into Tab1 values (date '2000-05-01',

SQL: Gaps and Island Problem - Date not consecutive causing rank inaccurate

霸气de小男生 提交于 2019-12-24 06:22:58
问题 This is a follow up on the other question I asked. Quarter Segment Customer *Counter* Q1 2018 A A1 1 Q2 2018 A A1 2 Q3 2018 A A1 3 Q4 2018 B A1 1 Q1 2019 B A1 2 Q2 2019 A A1 1 Q1 2020 A A1 *1* I want 1 not 2 here because it's not consecutive (we don't have Q3 & Q4 2019) Q2 2020 A A1 *2* I want 2 not 3 here because it reset in Q1 2020. Below query works if the dates are consecutive. How would I adjust the query to get what I'm looking for? I tried adding a new column that is 1 row lag, and

SQL: Gaps and Island Problem - Date not consecutive causing rank inaccurate

与世无争的帅哥 提交于 2019-12-24 06:20:05
问题 This is a follow up on the other question I asked. Quarter Segment Customer *Counter* Q1 2018 A A1 1 Q2 2018 A A1 2 Q3 2018 A A1 3 Q4 2018 B A1 1 Q1 2019 B A1 2 Q2 2019 A A1 1 Q1 2020 A A1 *1* I want 1 not 2 here because it's not consecutive (we don't have Q3 & Q4 2019) Q2 2020 A A1 *2* I want 2 not 3 here because it reset in Q1 2020. Below query works if the dates are consecutive. How would I adjust the query to get what I'm looking for? I tried adding a new column that is 1 row lag, and