group-by

Problem with Select Mysql Condition Group By Order

﹥>﹥吖頭↗ 提交于 2019-12-13 00:21:21
问题 Each service has different KPIs. These KPIs are ordered with the field order in the table checklist_has_kpi. I want a list of separate services ordered by the order assigned to the KPI. I have this query: SELECT s.serviceid, s.servicenameit, s.servicenameen FROM services s, kpi k, checklist_has_kpi chk WHERE s.serviceid=k.serviceid AND k.kpiid=chk.kpiid AND k.inreport='yes' AND chk.checklistid=61 GROUP BY s.serviceid ORDER BY chk.order ASC But it does not produce the result that I expect and

Getting maximum without grouping

拥有回忆 提交于 2019-12-12 22:11:35
问题 Say that I have a table like that: name | age a | 1 b | 2 c | 3 d | 4 e | 5 f | 6 Normally, when we select MAX(age), it returns (f,6) tuple. But what I want is that it should return the table as it is, but all of the age values will be the maximum. Such as: name | age a | 6 b | 6 c | 6 d | 6 e | 6 f | 6 Is there a way to do this? 回答1: Try this: SELECT `name`, (SELECT MAX(age) FROM MyTable) AS `age` FROM MyTable; 来源: https://stackoverflow.com/questions/9712660/getting-maximum-without-grouping

Challenging LEFT OUTER JOIN query grouping by MAX

跟風遠走 提交于 2019-12-12 21:42:30
问题 I have the following two tables: BillingMatrixDefinition - id - amount BillingMatrix - definition (FK to table above) - service_id (FK) - provider_id (FK) - amount (Decimal) I need to get all BillingMatrixDefinitions that have the service_id and provider_id that I specify. Here is the SQL query I currently have: select def.id, service_id, provider_id, (case when matrix.amount is not null then matrix.amount else def.amount end) amount from billing_billingdefinition def left outer join billing

Get the first instance of a row using MS Access

馋奶兔 提交于 2019-12-12 21:09:27
问题 EDITED: I have this query wherein I want to SELECT the first instance of a record from the table petTable . SELECT id, pet_ID, FIRST(petName), First(Description) FROM petTable GROUP BY pet_ID; The problem is I have huge number of records and this query is too slow. I discovered that GROUP BY slows down the query. Do you have any idea that could make this query faster? or better, a query wherein I don't need to use GROUP BY ? 回答1: "The problem is I have huge number of records and this query is

TOP N problem with GROUP BY clause

谁都会走 提交于 2019-12-12 19:24:10
问题 The problem: I need to find all active [GiftPledges] that have the last three [GiftDetails] have a zero amount. SELECT gp.PledgeId FROM GiftPledge gp INNER JOIN GiftDetail gd ON gp.PledgeId = gd.PledgeId WHERE gp.PledgeStatus = 'A' GROUP BY PledgeId HAVING COUNT(PledgeId) >= 3 Now, I have all my [GiftPledges] that have at least three [GiftDetails]. SELECT TOP 3 gdi.Amt FROM GiftDetail gdi INNER JOIN GiftHeader ghi ON gdi.GiftRef = ghi.GiftRef WHERE gdi.PledgeId = gp.PledgeId ORDER BY ghi

MySQL - get sum() grouped max() of group

穿精又带淫゛_ 提交于 2019-12-12 19:17:41
问题 I have table structure like below. Each row is one played game, each person can play many or none times in each month. id | person | score | date | ------------------------------------ 1 | 32 | 444 | 2011-05 | 2 | 65 | 528 | 2011-05 | 3 | 77 | 455 | 2011-05 | 4 | 32 | 266 | 2011-06 | 5 | 77 | 100 | 2011-06 | 6 | 77 | 457 | 2011-06 | 7 | 77 | 457 | 2011-06 | 8 | 65 | 999 | 2011-07 | 9 | 32 | 222 | 2011-07 | I am trying to get for each person sum of its best score in each month. S result of

EF LINQ translation: complex query

时光怂恿深爱的人放手 提交于 2019-12-12 19:17:02
问题 I have this code (Note l.FirstOrDefault().Name ): var qry = from peron in db.Persons join room in db.Rooms on peron.Room.Id equals room.Id join passport in db.Passports on peron.Passport.Id equals passport.Id select new {peron.Fio, room.Name, passport.Number, Count = 0}; qry = qry.GroupBy(l => new {l.Fio, l.Number}) .Select(l => new { l.Key.Fio, l.FirstOrDefault().Name, l.Key.Number, Count = l.Count() }); This translates to this: SELECT 1 AS [C1], [Project4].[Fio] AS [Fio], [Project4].[C1] AS

Django complex query to fetch data from groupby and having clause

穿精又带淫゛_ 提交于 2019-12-12 18:23:38
问题 I want to execute group by clause on MyUser table having CNT.Status exactly one the raw query would be like below. SELECT user_id from user_table GROUP BY user_id HAVING COUNT(status)=1 I tried various options using Django model API but its adding "id" too in group by clause. 回答1: It seems you are looking for filtering on annotations: qs = ( MyUser .objects .annotate(num_status=Count('status')) .filter(num_status__gt=1) ) Notice: I supose that status is a 1:N related model. 来源: https:/

ORA-00979 Not a GROUP BY Expression with complex EXTRACTVALUE

心不动则不痛 提交于 2019-12-12 17:26:50
问题 I'm trying to execute a rather simple Oracle query against a table with a XMLTYPE column: Select POB_CODPOL, CODSIS From ( Select T1.POB_CODPOL, EXTRACTVALUE(T1.POB_XMLPOL, '/Polbas/polfun[nomfun="filterBySystem"]/extpar[codele="codsis"]/valele/text()') CODSIS From TDTX_POLITICA_CLOB T1 Where T1.POB_CODEMP = '001840' ) Group By POB_CODPOL, CODSIS This throws a ORA-00979 Not a GROUP BY Expression , which I don't really understand. Even worse: when I execute the exact same query, but with a

Pandas get rows after groupby

我们两清 提交于 2019-12-12 17:03:30
问题 Suppose I have the following dataset: uid iid val 1 1 2 1 2 3 1 3 4 1 4 4.5 1 5 5.5 2 1 3 2 2 3 2 3 4 3 4 4.5 3 5 5.5 From this data, I want to first groupby uid, then get last 20% of number of rows from each uid. That is, since uid=1 has 5 rows, I want to obtain last 1 row (20% of 5) from uid=1. The following is what I want to do: df.groupby('uid').tail([20% of each uid]) Can anyone help me? 回答1: You can try applying a custom function to groupby object. Inside the function calculate how many