group-by

selecting when an aggregate column has two or more items

Let's say I had a query like this: SELECT * FROM ( SELECT 'a' AS a, '1' AS b UNION SELECT 'a' AS a, '2' AS b UNION SELECT 'b' AS a, '1' AS b) AS a GROUP BY a.a In this case "a".b is an aggregate of 1,2 while "b".b is only an aggregate of 1. How can I select only "a"? Question updated to be a bit clearer: Let's take this ve...

Group mysql query by 15 min intervals

I've got a monitoring system that is collecting data every n seconds (n ~=10 but varies). I'd like to aggregate the collected data by 15 minute intervals. Is there a way to corral the timestamp column into 15 minute chunks to allow for grouping to work? ...

SQL GROUP BY with "default values"

I'm trying to create SELECT statement with a GROUP BY clause, which should return "default values". Imagine the following simple MySQL table: CREATE TABLE `tracker` ( `id` INTEGER PRIMARY KEY auto_increment, `date` DATETIME NOT NULL, `customer_id` INTEGER NOT NULL ); The table contains only one record: INSERT INTO `tracker` (`...

Reorganizing MySQL table to multiple rows by timestamp.

OK MySQL Wizards: I have a table of position data from multiple probes defined as follows: +----------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----------+------+-----+---------+-------+ | time | datetime | NO | | NULL | | | probe_id | char(3) | ...

ado sql update table with result of group by query

I am trying to update records in an .mdb table with the number of records containing the same value The sql below does not work but I think gives an indication of what I am trying to achieve. UPDATE table1 AS A INNER JOIN (SELECT PH_BSP , Count(PH_BSP) AS PHCOUNT FROM table1 GROUP BY PH_BSP) AS B ON A.PH_BSP=B.PH_BSP SET A.PH_SORT = ...

After grouping by, can I refer to the elements of the original IEnumerable in a LINQ query?

Example: from OriginalObject in ListOfOriginalObjects group new CustomObject { X = OriginalObject.A, Y = OriginalObject.B } by OriginalObject.Z into grouping select new GroupOfCustomObjects { Z = grouping.Key, C = OriginalObject.C, group = grouping } In the select part of the query, I'd like to add a property (Original...

How to find the latest row for each group of data

Hi All, I have a tricky problem that I'm trying to find the most effective method to solve. Here's a simplified version of my View structure. Table: Audits AuditID | PublicationID | AuditEndDate | AuditStartDate 1 | 3 | 13/05/2010 | 01/01/2010 2 | 1 | 31/12/2009 | 01/10/2009 3 | 3 ...

MySQL - how to retrieve columns in same row as the values returned by min/mx

I couldn't frame the question's title properly. Suppose a table of weekly movie earnings as below: MovieName <Varchar(450)> MovieGross <Decimal(18)> WeekofYear <Integer> Year <Integer> So how do I get the names of top grossers for each week of this year, if I do: select MovieName , Max(MovieGross) , WeekofYear from earni...

How can I do this Aggrigate, group by, in query in LINQ?

Please do not give me a full working example, I want to know how this is done rather than to get some code I can copy paste This is the query I need, and can't for the life of me create it in LINQ. SELECT * FROM dbo.Schedules s, dbo.Videos v WHERE s.VideoID = v.ID AND s.ID IN ( SELECT MAX(ID) FROM dbo.Schedules WHERE ChannelID...

Mysql : Count Posts and Group by date

Hello , I am not very good at sql , generally I use php to do my complicated tasks , But in this task , there are lots of data , so using php for counting posts is very slow. So I want a sql which counts post by date , but my date column in table is php's time stamp (int).I will crate post number x date chart ...

VB.NET GroupBy LINQ statement

I am having a bear of a time getting this to work. I have a List(Of MyItem) called Items that have a OrderId property on them. From those items, I want to create a list of Orders. Some items will have the same OrderId so I want to try to group by OrderId. I then want to sort by date. Here's what I have so far: Public ReadOnly Property A...

SQL Server problems when using ODER BY and DISTINCT together

I got two problems, the first problem is my two COUNTS that I start with. GroupID is a string that keep products together (Name_Year together), same product but different size. If I have three reviews in tblReview and they all have the same GroupID I want to return 3. My problem is that if I have three Products with different ProductID b...

GROUP BY a date, with ordering by date.

Take this simple query: SELECT DATE_FORMAT(someDate, '%y-%m-%d') as formattedDay FROM someTable GROUP BY formatterDay This will select rows from a table with only 1 row per date. How do I ensure that the row selected per date is the earliest for that date, without doing an ordered subquery in the FROM? Cheers ...

How to perform group by in LINQ and get a Iqueryable or a Custom Class Object?

Here is my query - var data = Goaldata.GroupBy(c => c.GoalId).ToList(); This returns a Igrouping object and I want an Iqueryable object which I can directly query to get the data while in this case I have to loop through using a foreach() and then get the data. Is there another way to group by in LINQ which returns directly as a list...

Simple SELECT query problem...

Hi, I have this MySql select query which seems to have a bug but I am quite "green" so I simply cannot see, so maybe you could help? Here is the query: SELECT node_id FROM rate WHERE node_id='".$cat_node_id_string."' LIMIT ".$node_count_star.",".$node_count_end." ORDER BY SUM(amount) GROUP BY node_id Thanks for help in ...

MySQL to fill in missing dates when using GROUP BY DATE(table.timestamp) without joining on temporary table

Hello, after reading through a couple similar Qs/As I haven't quite found the solution I'm looking for. The table data I have is GROUP BY DATE(timestamp) and returning a count, example result: [timestamp] => 2010-05-12 20:18:36 [count] => 10 [timestamp] => 2010-05-14 10:10:10 [count] => 8 Without using a temporary table, or calendar ...

SQL Server Update Group by

I'm trying to execute this on MS-SQL but returns me an error just at the Group by line update #temp Set Dos=Count(1) From Temp_Table2010 s where Id=s.Total and s.total in (Select Id from #temp) group by s.Total Do anyone knows how can I solve this problem having good performance. ...

SQL Server Select Distinct

I want to write a query like this: For a table that has these columns: ColA ColB ColC, ColD select first(ColA, ColB, ColC, ColD) distinct(ColB, ColC) from table order by ColD The query is supposed to order the table by ColD, then group the results by the combination of ColB and ColC (they may have different data types) and returns the...

Specify which row to return on SQLite Group By

I'm faced with a bit of a difficult problem. I store all the versions of all documents in a single table. Each document has a unique id, and the version is stored as an integer which is incremented everytime there is a new version. I need a query that will only select the latest version of each document from the database. While using GR...

How can I recreate a SQL statement using NHibernate that has an inner select case?

I am trying to recreate something like the following SQL using NHibernate criteria: select Range, count(*) from ( select case when ent.ID between 'A' and 'N' then 'A-M' else 'Other' end as Range from Subject ) tbl group by tbl.Range I am able to create the inner select as follows: session.CreateCri...