group-by

How to write the sql query to select and group by type and count per type?

≡放荡痞女 提交于 2019-12-12 03:49:38
问题 Below is my database table. id name type 1 a 1 2 a 1 3 a 1 4 a 0 5 a 0 6 b 1 7 b 1 8 b 0 9 b 0 10 b 0 11 c 1 12 c 1 13 c 0 I would like to select and group by type and count per type. How to write the sql query to get the following results? name count_type_0 count_type_1 a 2 3 b 3 2 c 1 2 回答1: Just use conditional aggregation: select name, sum(type = 0) as count_0, sum(type = 1) as count_1 from t group by name; This uses a MySQL feature that treats boolean values as numbers in a numeric

The difference between each row value - Sum Error

血红的双手。 提交于 2019-12-12 03:49:09
问题 My main query was solved at the following post TSQL - Get the difference between each row value I have one problem of summing the reading values between each row. id device_id time reading shift_id 150323 3 2013-02-25 15:22:01.273 999948.00 43 150324 1 2013-02-25 15:22:01.423 999962.00 43 150325 3 2013-02-25 15:22:01.463 999966.00 43 150326 1 2013-02-25 15:22:01.610 999979.00 43 150327 3 2013-02-25 15:22:01.650 999983.00 43 150328 1 2013-02-25 15:22:01.810 999997.00 43 for the above scenario

Get last update values from MySQL table

情到浓时终转凉″ 提交于 2019-12-12 03:44:38
问题 I have a mysql table with the following structure and data. I want to show last inserted record on any id. id lc_counter lc_timestamp 1 15 2013-03-01 11:54:43 1 13 2013-03-01 11:48:56 10 7 2013-03-01 11:54:43 10 5 2013-03-01 11:48:56 100 5 2013-03-01 11:54:43 100 3 2013-03-01 11:54:43 SELECT inv_id, lc_counter, lc_timestamp FROM link_counter group by inv_id order by inv_id asc, lc_timestamp desc I want to get this result: id lc_counter lc_timestamp 1 15 2013-03-01 11:54:43 10 7 2013-03-01 11

ORACLE SELECT GROUP BY + something that I don't know

别来无恙 提交于 2019-12-12 03:35:26
问题 I have a table in the ORACLE database, details below: -------------------------------------------- | FRUITS | -------------------------------------------- | FRUIT_NAME | GROWTH_TIME | GROWTH_PLACE | -------------------------------------------- | melon | 0600 | shelf1 | | melon | 0630 | shelf1 | | melon | 0700 | shelf1 | | melon | 0730 | shelf1 | | melon | 0800 | shelf1 | | orange | 0600 | shelf5 | | orange | 0630 | shelf5 | | orange | 0700 | shelf5 | | orange | 0730 | shelf5 | | orange | 0800

Groupby and transform two columns in df into matrix R [duplicate]

淺唱寂寞╮ 提交于 2019-12-12 03:35:14
问题 This question already has answers here : Create columns from factors and count [duplicate] (2 answers) Closed 3 years ago . I would like to convert the following data.frame into a matrix where the the number of each bike station id that occurs per hour is counted. > dim(test) [1] 80623 5 head(test, n = 10) bikeid end.station.id start.station.id diff.time hour 1 16052 244 322 6544 14 2 16052 284 432 3406 21 3 16052 461 519 33416 3 4 16052 228 519 26876 13 5 16052 72 435 388 17 6 16052 319 127

join multiple mysql table with prefix and group sum

放肆的年华 提交于 2019-12-12 03:33:05
问题 I have three mysql tables have achieved to have all invoices from the orders table along with Invoice_No_Amt - Refund_Amount, group sum of settlement value Now I would like to have fullname column from sku_mapping table also to be listed in the result. prefix_sku is the primary key in sku_mapping table fk_orders |order_item_id |order_id |Invoice_No |Invoice_No_Amt |Qty |Refund_Qty |Refund_Amount | sku --------------------------------------------------------------------------------------------

How can I group on the SAME table, order by date and limit the number of rows returned?

对着背影说爱祢 提交于 2019-12-12 03:28:52
问题 I would like to return a set that is grouped by ID_F, limited in count within ID_F and sorted by date within ID_F Here is my database setup: CREATE TABLE [dbo].[U]( [ID] [uniqueidentifier] NOT NULL, [Name] [nvarchar](max) NULL, CONSTRAINT [PK_U] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[F]( [ID] [uniqueidentifier] NOT NULL, [Name]

LINQ: how to group by so element can be added to many groups

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 03:25:33
问题 I have 3 types: Parameter , Facility and Location . Parameter has one Facility and Facility is related many-to-many with Location . So, if we make a join, we can get such a table: ParamId FacilityId LocationId 1 1 1 1 1 2 2 2 1 2 2 3 Now i want to group my parameters by locations so that i get a Dictionary>, like this: LocationId Parameters 1 1,2 2 1 3 2 I can't do a straight GroupBy because i don't know how to write a keySelector. I Also tried SelectMany like this: parametersList.SelectMany

Check existence of distinct values for each group

最后都变了- 提交于 2019-12-12 03:22:21
问题 EDITED: Suppose I have the following table in MySQL: CREATE TABLE `events` ( `pv_name` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL, `time_stamp` bigint(20) UNSIGNED NOT NULL, `value` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin, PRIMARY KEY (`pv_name`, `time_stamp`) ) ENGINE=InnoDB; I can find each pv_name that has more than one distinct value in this table using the following query: SELECT events.pv_name FROM events GROUP BY events.pv_name HAVING COUNT(DISTINCT events.value) > 1; The

Using a Result Set From a Sub SELECT When Duplicate Rows are Encountered

南笙酒味 提交于 2019-12-12 02:57:50
问题 First of all, the table I'm struggling with: DispatchLocations ================= DispatchID int StopNumber int Arrived bool Departed bool The idea is that on a trucking route, there are many stops in a dispatch. This is a list of each location on each dispatch (essentially, it's a route table). In this view, there should only be one dispatch for each row of output, which points to the "current stop" that the dispatch is at. SELECT TOP 4 DispatchID, Min(StopNumber) AS NextStop, Arrived,