distinct

Ordering distinct column values by (first value of) other column in aggregate function

爷,独闯天下 提交于 2020-01-11 00:51:11
问题 I'm trying to order the output order of some distinct aggregated text based on the value of another column with something like: string_agg(DISTINCT sometext, ' ' ORDER BY numval) However, that results in the error: ERROR: in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list I do understand why this is, since the ordering would be "ill-defined" if the numval of two repeated values differs, with that of another lying in-between. Ideally, I would like to order them by

How do I SUM DISTINCT Rows?

只愿长相守 提交于 2020-01-10 03:57:28
问题 I'm struggling with a query where I need to SUM DISTINCT Rows. There has to be a way to do this... but I'm lost. Here's what I've got: SELECT DISTINCT Zipcodes.CountyID, us_co_est2005_allData.PopEstimate2005, us_co_est2005_allData.EstimatesBase2000, users_link_territory.userID FROM Zipcodes Inner Join Users_link_territory ON zipcodes.CountyID = Users_link_territory.CountyID Inner Join us_co_est2005_alldata ON zipcodes.FIPS = us_co_est2005_alldata.State AND zipcodes.code = us_co_est2005

distinct group by 去重查询

北城以北 提交于 2020-01-09 01:03:52
select * from dc_restaurants; 31   select DISTINCT (restaurant_name),id from dc_restaurants ; 31 (会按照id和 restaurant_name 联合 去重 ) select DISTINCT (restaurant_name),id from dc_restaurants group by restaurant_name; 17 select restaurant_name,id from dc_restaurants group by restaurant_name; 17 -- 按照某个字段 进行 去重的 方式: 1. group by(不用关注要查询的字段 ) -- 2. distinct 要关注要查询的字段,如果想要查询其他非 去重字段, -- 则需要group by 该去重字段,否则会按照多个字段进行联合去重 -- group by 分组 默认取 id 最小的那条记录 。。 -- distinct 去重 是按照 字段区分的。* 表示所有字段。单个字段是指 -- 去除该列 重复的数据。 来源: https://www.cnblogs.com/zhangchenglzhao/p/3918908.html

Index on Mysql column has lower cardinality than count distinct?

允我心安 提交于 2020-01-07 05:38:06
问题 I have a varchar(50) field named token. When I do count(distinct token) from table I get ~ 400k. However, if I do create index idx on table (token) The cardinality is only 200. What could be going on here? Shouldn't cardinality be the same as the number of distinct tokens? 回答1: The cardinality is an estimate of the number of unique values in the index. According to the documentation: Cardinality is counted based on statistics stored as integers, so the value is not necessarily exact even for

Get distinct record from table using zend functions?

狂风中的少年 提交于 2020-01-07 05:37:12
问题 I am getting all records like this in Zend: $table = new Model_Student_Object(); $select = $table->select(); echo $select->assemble();die(); Result of above code is following query: SELECT `student`.* FROM `student` Now I want to get distinct records by Zip code. So query should be: SELECT DISTINCT `student`.zip FROM `student` WHERE `student`.zip != '' How will I right above query with zend functions ?? Thanks 回答1: Try this $db = Zend_Db::factory( ...options... ); $select = new Zend_Db_Select

Super Slow Query - sped up, but not perfect… Please help

不打扰是莪最后的温柔 提交于 2020-01-06 19:32:34
问题 I posted a query yesterday (see here) that was horrible (took over a minute to run, resulting in 18,215 records): SELECT DISTINCT dbo.contacts_link_emails.Email, dbo.contacts.ContactID, dbo.contacts.First AS ContactFirstName, dbo.contacts.Last AS ContactLastName, dbo.contacts.InstitutionID, dbo.institutionswithzipcodesadditional.CountyID, dbo.institutionswithzipcodesadditional.StateID, dbo.institutionswithzipcodesadditional.DistrictID FROM dbo.contacts_def_jobfunctions AS contacts_def

How to fetch values of column 1 that contain only the given value of column 2?

别来无恙 提交于 2020-01-06 15:59:15
问题 I want to select names that only appear in Mexico and not in any other country. Country | Name -------------|------------ Mexico | Vallejo Mexico | Rachel United States| Rachel UK | Rachel Australia | Rachel Mexico | Amy Canada | Amy Mexico | Annette Mexico | Jennifer Swahili | Jennifer Mexico | Benedict The correct query would only return the following names. Name --------- Annette Benedict Vallejo Any ideas? I'm not sure if this might be a mix of DISTINCT and WHERE conditions. 回答1: I think

How to update distinct column in SQL Server 2005?

时光总嘲笑我的痴心妄想 提交于 2020-01-06 15:17:42
问题 In my table I have a column Segment_No which has duplicates. Now I want to update those duplicates. For example: the value 249X5601 is present in two rows. I want to change the second value to 249X5601R 回答1: The general form would be as follows: ;with AllRows as ( select Donation_ID,Registration_No,Segment_No, ROW_NUMBER() OVER ( PARTITION BY Segment_No order by <Suitable_Column> ) as rn from UnnamedTable ) update AllRows set Segment_no = <New_Value> where rn > 1 Where <Suitable_Column> gives

Get Distinct records from MongoDB using lithium

北慕城南 提交于 2020-01-06 04:16:48
问题 I want to find distinct records from a collected "pages". I tried: $Volume_numbers = Pages::find(array('fields'=>'DISTINCT volume_number')); I also tried: $params = array('conditions'=>array( 'distinct' => 'pages', 'key' => 'volume_number', )); $pageVolumes = Pages::all($params); as suggested in MongoDB documentation and also in one of the answers. When I try to execute this through Mongo, I get correct results > db.runCommand({distinct:'pages',key:'volume_number'}) { "values" : [ 22, 38 ],

Select Distinct * as query method?

大兔子大兔子 提交于 2020-01-05 05:39:16
问题 In my Spring project, I'm currently using only query methods. Now, when calling findAll(Pageable) with a pageable that contains a sort of a collection property, I'm experiencing a known and expected issue: DATAJPA-744: duplicate results when sorting by collection property An easy way and also suggested way to solve this is by using the DISTINCT keyword to filter the result. My problem is that when I create the repository method findDistinct , spring throws an exception on initializing telling