distinct

Group by doesn't give me the newest group

ⅰ亾dé卋堺 提交于 2019-12-13 04:03:10
问题 I was trying to get the distinct result from my table, and people said I should use the group by. This worked half way... I now get the distinct result but the result is not the newest thread... my table contains status on apartments from several buildings. The apartments can be found many times since it's a history table... I need to make a select that retrieves the distinct apartments with the current status. ID Building Apartment_id Status 1 1 1 1 2 1 1 2 3 2 2 3 4 2 4 2 5 2 3 2 6 2 5 1 7

php mysql sort by number of rows descending

情到浓时终转凉″ 提交于 2019-12-13 03:58:28
问题 can you help me $sql="select * from table1 where id='1,2,3,4'"; { $sql2="select distinct column1 from table2 where column2='".$row['id']."' and left(date,10) BETWEEN '".$date_from."' AND '".$date_to."'"; } I need to sort $sql by number of rows descending for $sql2 by id 回答1: If I understand you correctly, you want the results from $sql ordered by number of rows found by $sql2 for each row in $sql . This join should do that, it joins table2 and orders by the count, descending. SELECT t1.id

SQL - how to get unique values in a column (distinct does not help here)

元气小坏坏 提交于 2019-12-13 03:55:52
问题 I have a production case where we track devices that move around warehouses. I have a table that shows these previous locations like this: +--------+------------+-----------+------+ | device | current_WH | date | rank | +--------+------------+-----------+------+ | 1 | AB | 3/15/2018 | 7 | | 1 | CC | 3/19/2018 | 6 | | 1 | CC | 3/22/2018 | 5 | | 1 | CC | 3/22/2018 | 5 | | 1 | DD | 4/23/2018 | 4 | | 1 | DD | 5/11/2018 | 3 | | 1 | DD | 5/15/2018 | 2 | | 1 | DD | 5/15/2018 | 2 | | 1 | AA | 6/6

MSSQL - Making multiple count distinct calls in a query runs slowly

蓝咒 提交于 2019-12-13 01:27:59
问题 I have tables with the following schema: Device DeviceId Name Service ServiceId Name Software SoftwareId Name Device_Software DeviceId SoftwareId DiscoveryDate Device_Service DeviceId ServiceId DiscoveryDate Now, I'm trying to write a query that gives the a Device, and the number of distinct software and services that device has. If I run the following query I get a result back within 5 seconds (device has 50,000 rows, software and service both have 200 and the link tables include a link for

Distinct a list of records based on two column using groupby

你离开我真会死。 提交于 2019-12-13 01:27:39
问题 I am trying to fetch a list using EF6 .I have a class like this: public class Province { public string province { set; get; } public string provinceCode { set; get; } } Zone class namespace InnoviceDomainClass { using System; using System.Collections.Generic; public partial class Zone { public string CityCode { get; set; } public string City { get; set; } public string Province { get; set; } public Nullable<int> ProvinceCode { get; set; } } } I fetch my data using this : List<Zone> ListZone =

Retrieving one element and filter by another

删除回忆录丶 提交于 2019-12-12 23:12:59
问题 Having tried and failed to modify the XSL from here: Distinct elements and grouping I'm posting here to ask if anyone could please help. I've basically got the same data structure (mine is actually an RSS feed of products) as in the above post, but I want to list the Description elements uniquely, in sorted order where the File element contains a particular value. I believe the XPath for the File select would be: */File[text()='file1'] for example, to take only the File elements containing

How do you SELECT several columns with one distinct column

半城伤御伤魂 提交于 2019-12-12 21:32:04
问题 Objective: using SqlServer 2005, Select multiple columns, but ensure that 1 specific column is not a duplicate Issue: The following code does not remove the duplicates. The field that has duplicates is email. SELECT DISTINCT email, name, phone FROM database.dbo.table WHERE status = 'active' GROUP BY email, name, phone Thank you in advance for any comments, suggestions or recommendations. 回答1: It removes email duplicates but you have to decide which name, phone you need. The result is based on

Sybase SQL Select Distinct Based on Multiple Columns with an ID

≯℡__Kan透↙ 提交于 2019-12-12 15:36:02
问题 I'm trying to query a sybase server to get examples of different types of data we hold for testing purposes. I have a table that looks like the below (abstracted) Animals table: id | type | breed | name ------------------------------------ 1 | dog | german shepard | Bernie 2 | dog | german shepard | James 3 | dog | husky | Laura 4 | cat | british blue | Mr Fluffles 5 | cat | other | Laserchild 6 | cat | british blue | Sleepy head 7 | fish | goldfish | Goldie As I mentioned I want an example

Selecting most recent MySQL rows by MAX(time) WHERE time <= x

戏子无情 提交于 2019-12-12 14:43:26
问题 I am selecting the most recent entries of a MySQL table with: SELECT MAX(time) as most_recent, userID FROM TableName GROUP BY userID ORDER BY most_recent DESC My problem is, if I want to limit the maximum time with: WHERE time <= nnn The query does not work anymore. Is there a solution for with without a subquery? Thanks in advance! 回答1: you can do it with subquery : select t.userID, max(t.time) from ( select userID, time from tableName where time <= nnn ) t group by t.userID or simply :

How to count the number of grouped rows in mysql when I already count the total rows

只谈情不闲聊 提交于 2019-12-12 14:12:52
问题 I have the below table and I want to do the following: Count the number of times each item appears in the table Count the DISTINCT number of items Group the items by name +-------+---------+ | id | names | +-------+---------+ | 1 | Apple | | 2 | Orange | | 3 | Grape | | 4 | Apple | | 5 | Apple | | 6 | Orange | | 7 | Apple | | 8 | Grape | +-------+---------+ For the 1. and 3. points I have the following query which works quite well: SELECT * , COUNT(names) as count_name, FROM tbl_products