group-by

SQL Group By and min (MySQL)

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-28 06:58:33
问题 I have the following SQL: select code, distance from places; The output is below: CODE DISTANCE LOCATION 106 386.895834130068 New York, NY 80 2116.6747774121 Washington, DC 80 2117.61925131453 Alexandria, VA 106 2563.46708627407 Charlotte, NC I want to be able to just get a single code and the closest distance. So I want it to return this: CODE DISTANCE LOCATION 106 386.895834130068 New York, NY 80 2116.6747774121 Washington, DC I originally had something like this: SELECT code, min(distance)

ORDER BY Alias not working

被刻印的时光 ゝ 提交于 2019-12-28 06:35:36
问题 UPDATING QUESTION: ERROR: column "Fruits" does not exist Running Postgres 7.4(Yeah we are upgrading) Why can't I ORDER BY the column alias? wants tof."TypeOfFruits" in the ORDER BY as well, why? SELECT (CASE WHEN tof."TypeOfFruits" = 'A' THEN 'Apple' WHEN tof."TypeOfFruits" = 'P' THEN 'Pear' WHEN tof."TypeOfFruits" = 'G' THEN 'Grapes' ELSE 'Other' END) AS "Fruits", SUM(CASE WHEN r.order_date BETWEEN DATE_TRUNC('DAY', LOCALTIMESTAMP) AND DATE_TRUNC('DAY', LOCALTIMESTAMP) + INTERVAL '1 DAY'

Can you GROUP BY with a CASE WHEN THEN alias name?

青春壹個敷衍的年華 提交于 2019-12-28 06:27:27
问题 I have a SELECT statement being calculated from a CASE WHEN THEN state (or could use multiple IF statements) aliased as 'Length', and I need to correctly GROUP the results together. The SELECT seems to be working, but the group groups them wrong. Here is my statement: SELECT CASE WHEN DATEDIFF(o.EndDate, o.StartDate) < 30 THEN '<1 Month' WHEN DATEDIFF(o.EndDate, o.StartDate) < 90 THEN '1 - 2 Months' WHEN DATEDIFF(o.EndDate, o.StartDate) < 210 THEN '3 - 4 Months' ELSE '>4 Months' END AS

How to do a case sensitive GROUP BY?

自作多情 提交于 2019-12-28 05:43:09
问题 If I execute the code below: with temp as ( select 'Test' as name UNION ALL select 'TEST' UNION ALL select 'test' UNION ALL select 'tester' UNION ALL select 'tester' ) SELECT name, COUNT(name) FROM temp group by name It returns the results: TEST 3 tester 2 Is there a way to have the group by be case sensitive so that the results would be: Test 1 TEST 1 test 1 tester 2 回答1: You can use an case sensitive collation: with temp as ( select 'Test' COLLATE Latin1_General_CS_AS as name UNION ALL

GROUP BY without aggregate function

守給你的承諾、 提交于 2019-12-27 16:49:48
问题 I am trying to understand GROUP BY (new to oracle dbms) without aggregate function. How does it operate? Here is what i have tried. EMP table on which i will run my SQL. SELECT ename , sal FROM emp GROUP BY ename , sal SELECT ename , sal FROM emp GROUP BY ename; Result ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression" *Cause: *Action: Error at Line: 397 Column: 16 SELECT ename , sal FROM emp GROUP BY sal; Result ORA-00979: not a GROUP BY expression 00979. 00000 -

Pandas groupwise percentage

血红的双手。 提交于 2019-12-25 21:36:13
问题 How can I calculate a group-wise percentage in pandas? similar to Pandas: .groupby().size() and percentages or Pandas Very Simple Percent of total size from Group by I want to calculate the percentage of a value per group. How can I achieve this? My dataset is structured like ClassLabel, Field Initially, I aggregate on both ClassLbel and Field like grouped = mydf.groupby(['Field', 'ClassLabel']).size().reset_index() grouped = grouped.rename(columns={0: 'customersCountPerGroup'}) Now I would

Which record will GROUP BY choose in SQL [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-25 18:48:24
问题 This question already has answers here : GROUP BY behavior when no aggregate functions are present in the SELECT clause (8 answers) Closed 5 years ago . If I have an SQL query like this one: SELECT playerno,town FROM players GROUP BY town In this case, the server will return lets say one town, and for playerno, it will return one value, even if there were multiple values in the database. My question is, which value will be chosen? Is the choice made randomly, or will it take the first result,

MySql JOIN performance go down with group by

混江龙づ霸主 提交于 2019-12-25 18:24:54
问题 I have these tables: table "f" (26000 record) +------------------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+------------------+------+-----+---------+-------+ | idFascicolo | int(11) | NO | PRI | | | | oggetto | varchar | NO |index| | | +------------------+------------------+------+-----+---------+-------+ table "r" (22000 record) +------------------+------------------+------+-----+---------+-------+ | Field | Type |

Get count of particular category and further bifurcated sub category using SQL query

廉价感情. 提交于 2019-12-25 18:13:09
问题 I am looking to get a summary report from my database using sql query to show their count grouped them by category but also to further show the grouped subcategory count under each category. e.g. in attached snapshot below: I want to summarize my data for number of male and female employees but also to show under each gender the count of different employee position. Thanks in advance. 回答1: Assuming a table structure as follows: id -- primary key gender -- 'Male', 'Female' position -- 'Senior

Select all rows in a group where within the group, one column has one specific value, and another column has another specific value on the next row

折月煮酒 提交于 2019-12-25 18:04:14
问题 I am new to SQL, so I’m not sure which approach is best for this kind of task: I have a table where groups of rows all relate to the same item, whose name appears in the first column. Other details appear in the other columns. I am trying to retrieve all rows for every group based on having the same value in the first column, where every time a certain value appears in one column, another value appears in a different column in the following row. | Fruit | Value1| Value2| ---------------------