group-by

sql server pivot : group by with dynamic columns

你离开我真会死。 提交于 2019-12-19 11:32:15
问题 I have done a pivot on a table, generating dynamic columns : DECLARE @cols AS NVARCHAR(MAX); DECLARE @query AS NVARCHAR(MAX); select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Replace(variable,char(CAST(0x0016 as int)),'')) val FROM TABLEDATA ORDER BY val asc FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') , 1, 1, ''); SELECT @query = 'SELECT time, country, disease, ' + @cols + ' FROM TABLEDATA PIVOT ( MAX(value) FOR variable IN( ' + @cols + ' )' + ' ) AS p; '; execute(@query); It

SQL Count records within a month using a unix timestamp

丶灬走出姿态 提交于 2019-12-19 11:28:25
问题 I'm trying to return the count of records within each month and group the result by month / year. Schema looks something like this: id title timestamp I've been searching around but I can't get the result as I expect it. Thanks. 回答1: Format the timestamp, then group by it. Group by Month: SELECT DATE_FORMAT(t.timestamp, "%Y-%m") AS "_Month", COUNT(*) FROM yourtable as t GROUP BY _Month; Group by Year: SELECT DATE_FORMAT(t.timestamp, "%Y") AS "_Year", COUNT(*) FROM yourtable as t GROUP BY

How to build group-by dynamically on a list of dictionaries

眉间皱痕 提交于 2019-12-19 11:18:49
问题 I am trying to perform a groupby on an IEnumerable. The problem is that I do not know at compile-time which fields i want to groupby. I have found another post on stack that explains how to do this when the class is known and has properties, but in my case i am dealing with a dictionary and the keys are also only known at run-time. My code would resemble something like this (i know this doesn't compile...): private object GetValuesGroupedBy(List<string> groupbyNames, List<string>

Resampling timeseries with a given timedelta

一曲冷凌霜 提交于 2019-12-19 11:04:55
问题 I am using Pandas to structure and process Data. This is my DataFrame: I want to do a resampling of time-series data, and have, for every ID (named here "3"), all bitrate scores, from beginning to end (beginning_time / end_time). For exemple, for the first row, I want to have all seconds, from 2016-07-08 02:17:42 to 2016-07-08 02:17:55, with the same bitrate score, and the same ID of course. Something like this : For example, given : df = pd.DataFrame( {'Id' : ['CODI126640013.ts',

SQL Server: How can I use the COUNT clause without GROUPing?

爷,独闯天下 提交于 2019-12-19 10:59:28
问题 I'm looking get two things from a query, given a set of contraints: The first match The total number of matches I can get the first match by: SELECT TOP 1 ID, ReportYear, Name, SignDate, ... FROM Table WHERE ... ORDER BY ... //I can put in here which one I want to take And then I can get the match count if I use SELECT MIN(ID), MIN(ReportYear), MIN(Name), MIN(SignDate), ... , COUNT(*) as MatchCount FROM Table WHERE ... GROUP BY ??? // I don't really want any grouping I really want to avoid

SQL Query to return 24 hour, hourly count even when no values exist?

不羁的心 提交于 2019-12-19 09:57:38
问题 I've written a query that groups the number of rows per hour, based on a given date range. SELECT CONVERT(VARCHAR(8),TransactionTime,101) + ' ' + CONVERT(VARCHAR(2),TransactionTime,108) as TDate, COUNT(TransactionID) AS TotalHourlyTransactions FROM MyTransactions WITH (NOLOCK) WHERE TransactionTime BETWEEN CAST(@StartDate AS SMALLDATETIME) AND CAST(@EndDate AS SMALLDATETIME) AND TerminalId = @TerminalID GROUP BY CONVERT(VARCHAR(8),TransactionTime,101) + ' ' + CONVERT(VARCHAR(2)

Is there a [straightforward] way to order results *first*, *then* group by another column, with SQL?

北城以北 提交于 2019-12-19 07:46:09
问题 I see that in SQL, the GROUP BY has to precede ORDER BY expression. Does this imply that ordering is done after grouping discards identical rows/columns? Because I seem to need to order rows by a timestamp column A first, THEN discarding rows with identical value in column A. Not sure how to accomplish this... I am using MySQL 5.1.41 create table ( A int, B timestamp ) The data could be: +-----+-----------------------+ | A | B | +-----+-----------------------+ | 1 | today | | 1 | yesterday |

How can I group an array of objects by month?

白昼怎懂夜的黑 提交于 2019-12-19 05:53:09
问题 I'm using JavaScript. I have an array that contains data in this format: [ {"USER_NAME":"User1","LAST_SUCCESSFUL_CONNECT":"1373978337642"}, {"USER_NAME":"User2","LAST_SUCCESSFUL_CONNECT":"1374515704026"}, {"USER_NAME":"User3","LAST_SUCCESSFUL_CONNECT":"1374749782479"} ] (the numbers above represent UTC date/time in milliseconds. I would like to group (count) the data by month. Something like this: [ {"Month":"January, 2014","User_Count": 2}, {"Month":"February, 2014","User_Count": 1}, ] I

GROUP BY return the first record

我是研究僧i 提交于 2019-12-19 05:23:13
问题 As far as I know mysql GROUP BY groups to the last record found. Is there any solution to GROUP BY the first record? I have setup the ORDER in SQL command and I need GROUP BY return the first record and not the last. EDIT Here is the Query SELECT DISTINCT(master.masterID), langData.*, master.* FROM master_table as master INNER JOIN lang_table as langData ON langData.masterID=master.masterID GROUP BY master.masterID ORDER BY CASE WHEN langData.lang='currentLang' THEN 1 ELSE 999 END , master

GROUP BY return the first record

南楼画角 提交于 2019-12-19 05:23:04
问题 As far as I know mysql GROUP BY groups to the last record found. Is there any solution to GROUP BY the first record? I have setup the ORDER in SQL command and I need GROUP BY return the first record and not the last. EDIT Here is the Query SELECT DISTINCT(master.masterID), langData.*, master.* FROM master_table as master INNER JOIN lang_table as langData ON langData.masterID=master.masterID GROUP BY master.masterID ORDER BY CASE WHEN langData.lang='currentLang' THEN 1 ELSE 999 END , master