group-by

SQL HAVING SUM GROUP BY

别来无恙 提交于 2020-01-04 09:15:30
问题 Using SQL Server 2005. I am building an inventory/purchasing program and I’m at the point where I need the user to “check out” equipment. When he selects a product, I need to query which stock locations have the available Qty, and tell the user which location to walk to/ retrieve product. Here is a query for a particular [StockLocation_Products].ProductID, with a particular assigned [ProductUsages].ProductUsageID. SELECT PROD.ProductID, PROD.ProductName, SL.Room, SL.StockSpace, SLPPU.ResvQty,

Grouping by Distinct Values

浪尽此生 提交于 2020-01-04 06:59:27
问题 I have, after some help (MSChart - Forcing a line chart to draw from the Origin), managed to put together the following MSSQL query for use in a line chart. WITH AllDays AS ( SELECT CAST('20120101' as datetime) AS days UNION ALL SELECT DATEADD(dd, 1, days) FROM AllDays WHERE DATEADD(dd, 1, days) < cast('20120201' as datetime) ) SELECT MIN(ad.days) AS Date, ISNULL((SELECT SUM(value) FROM jobs WHERE dateinvoiced >= CAST('20120101' as datetime) AND dateinvoiced <= ad.days)/100,0) AS Value FROM

select and delete rows within groups using mysql

自古美人都是妖i 提交于 2020-01-04 06:27:10
问题 I've seen examples for duplicate row manipulation, but I can't figure out how to map them to solve my problem. +----+------------+------+---------+ | id | date | file | status | +----+------------+------+---------+ | 1 | 2011-12-01 | 1 | Pending | | 2 | 2011-12-02 | 1 | Pending | | 3 | 2011-12-03 | 1 | Done | | 4 | 2011-12-04 | 1 | Pending | | 5 | 2011-12-05 | 1 | Done | | 6 | 2011-12-06 | 1 | Pending | | 7 | 2011-12-07 | 1 | Pending | | 8 | 2011-12-08 | 1 | Pending | | 9 | 2011-12-09 | 2 |

pyspark: aggregate on the most frequent value in a column

时光怂恿深爱的人放手 提交于 2020-01-04 06:20:14
问题 aggregrated_table = df_input.groupBy('city', 'income_bracket') \ .agg( count('suburb').alias('suburb'), sum('population').alias('population'), sum('gross_income').alias('gross_income'), sum('no_households').alias('no_households')) Would like to group by city and income bracket but within each city certain suburbs have different income brackets. How do I group by the most frequently occurring income bracket per city? for example: city1 suburb1 income_bracket_10 city1 suburb1 income_bracket_10

How to include missing data for multiple groupings within the time span?

China☆狼群 提交于 2020-01-04 05:33:33
问题 I have below referenced query which groups studies counts by teacher, study year-month, and room for the past 12 months (including current month). The result I get is correct, however, I would like to include rows with zero counts for when the data is missing. I looked at several other related posts but could not get desired output: Postgres - how to return rows with 0 count for missing data? Postgresql group month wise with missing values Best way to count records by arbitrary time intervals

MySQL group by consecutive appearances

夙愿已清 提交于 2020-01-04 04:47:10
问题 I would like to group query results by consecutive appearances of a column values. Let's say I have a table which lists the winners of a competition for each year as follows: year team_name 2000 AAA 2001 CCC 2002 CCC 2003 BBB 2004 AAA 2005 AAA 2006 AAA I would like a query which outputs: start_end total team_name 2000 1 AAA 2001-2002 2 CCC 2003 1 BBB 2004-2006 3 AAA I'm not too much worried about the format of "start_end" at long as I have the start and end or range (.e.g. one could use GROUP

LINQ GroupBy on object with several levels

梦想的初衷 提交于 2020-01-04 04:32:34
问题 I've got some classes declared like this: public class UserSpend { public string UserName{ get; set; } public MonthSpend[] Spend { get; set; } } public class MonthSpend { public DateTime Month { get; set; } public SpendDetail[] Detail { get; set; } } public class SpendDetail { public string Description { get; set; } public decimal Amount { get; set; } } which as you can see has several levels. I have declared a variable like this: UserSpend[] allSpend; which I populate so that I have several

LINQ GroupBy on object with several levels

ⅰ亾dé卋堺 提交于 2020-01-04 04:32:32
问题 I've got some classes declared like this: public class UserSpend { public string UserName{ get; set; } public MonthSpend[] Spend { get; set; } } public class MonthSpend { public DateTime Month { get; set; } public SpendDetail[] Detail { get; set; } } public class SpendDetail { public string Description { get; set; } public decimal Amount { get; set; } } which as you can see has several levels. I have declared a variable like this: UserSpend[] allSpend; which I populate so that I have several

sql - getting the id from a row based on a group by

断了今生、忘了曾经 提交于 2020-01-04 03:49:06
问题 Table A tableAID tableBID grade Table B tableBID name description Table A links to Table b from the tableBID found in both tables. If I want to find the row in Table A, which has the highest grade, for each row in Table B, I would write my query like this: select max(grade) from TableA group by tableBID However, I don't just want the grade, I want the grade plus id of that row. 回答1: You could try something like SELECT a.* FROM TableA a INNER JOIN ( SELECT tableBID, MAX(grade) MaxGrade FROM

How to filter a crosstab created in pandas by a specific column

余生颓废 提交于 2020-01-04 02:58:50
问题 I have created a cross tabulation in pandas using: grouped_missing_analysis = pd.crosstab(clean_sessions.action_type, clean_sessions.action, margins=True).unstack() print(grouped_missing_analysis[:20]) Which leads to displaying: action action_type 10 Missing 0 Unknown 0 booking_request 0 booking_response 0 click 0 data 0 message_post 3215 modify 0 partner_callback 0 submit 0 view 0 All 3215 11 Missing 0 Unknown 0 booking_request 0 booking_response 0 click 0 data 0 message_post 716 modify 0