having

Find duplicate concat values

点点圈 提交于 2019-12-11 07:23:54
问题 I'm trying to find each row with duplicates in a MySQL database. 1 alex smith 2 bob smith 3 alex smith I want to return: 1 alex smith 3 alex smith This code will find duplicates, but it doesn't list each row that is a duplicate. SELECT *, CONCAT(`firstName`, ' ', `lastName`) as full_name, COUNT(*) d FROM users GROUP BY full_name HAVING d > 1; The code below is what I'm trying to get each row that is a dupliate, but I get the error "#1054 - Unknown column 'full_name' in 'IN/ALL/ANY subquery'"

Is there a performance difference between HAVING on alias, vs not using HAVING

[亡魂溺海] 提交于 2019-12-11 07:09:56
问题 Ok, I'm learning, bit by bit, about what HAVING means. Now, my question is if these two queries have difference performance characteristics: Without HAVING SELECT x + y AS z, t.* FROM t WHERE x = 1 and x+y = 2 With HAVING SELECT x + y AS z, t.* FROM t WHERE x = 1 HAVING z = 2 回答1: Yes it should be different - (1) is expected to be faster. Having will ensure that first the main query is run and then the having filter is applied - so it basically works on a the dataset returned by the (query

SQL Inner Join customers with orders

落花浮王杯 提交于 2019-12-11 06:24:28
问题 if I have a two tables (one of customers, with their information including address, name, emails etc) and another of orders (with order number, shipping date, customer name who ordered that item), how could I show the email of the customers who have less than 3 orders? I know I have to use an inner join and some alias's, but i'm not sure how to proceed. Thanks! what I have so far: SELECT customer.email FROM customer as cust INNER JOIN (select customer_id, sum(line_qty) AS total from orders as

SQL statement conversion to hibernate criteria (having count clause)

别来无恙 提交于 2019-12-11 05:59:15
问题 I have an SQL query that I would like to convert into a criteria (Hibernate 3.3.2) to use it into my persistancy layer. That's the query : select last_name from member where member.end_date > SYSDATE group by member.last_name having count(member.last_name) >1; I already try a lot a combinations, but there is no result that would be good for me ... This is my code : Criteria criteria = getSession(false).createCriteria(Member.ENTITY_NAME); criteria.setProjection(Projections.projectionList()

2 count(*)+group by+having+join

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 02:34:44
问题 I have tables: CREATE TABLE IF NOT EXISTS `un_pl` ( `coo` double DEFAULT NULL, `pl` varchar(255) DEFAULT NULL, `gal` varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `un_pl` (`coo`, `pl`, `gal`) VALUES (21.33333, 'One', 'Zero'), (22.33333, 'Mars', 'Sofar'); CREATE TABLE IF NOT EXISTS `un_tr` ( `ut_id` int(11) NOT NULL AUTO_INCREMENT, `date` date DEFAULT NULL, `s_p_c` double NOT NULL, `d_c` double NOT NULL, `tr_id` int(11) DEFAULT NULL, `ev_t` varchar(50) DEFAULT

Trouble with SQLite SQL Query [duplicate]

牧云@^-^@ 提交于 2019-12-11 00:57:33
问题 This question already has answers here : SQLite - WHERE Clause & UDFs (4 answers) Closed 6 years ago . I'm trying to run the following query in SQLite 3: SELECT *, DISTANCE(latitude, longitude, ?, ?) AS "distance" FROM "country" WHERE "id" NOT LIKE ? HAVING "distance" <= ? ORDER BY "distance" ASC; But I get the following error: SQLSTATE[HY000]: General error: 1 a GROUP BY clause is required before HAVING I don't understand why SQLite wants me to group results, but still I tried the following:

How to select top x records for every group

穿精又带淫゛_ 提交于 2019-12-10 22:53:43
问题 I tried something like this select Id,UserId from myTable group by Id,UserId having COUNT(UserId)<7 Now what i want to do is selecting 6 records for each userid. But my approach failed. So what is the correct syntax ? Id is primary key clustered index 回答1: This should get you pretty close WITH r ( userid, rnk ) AS ( SELECT userid, RANK() OVER ( PARTITION BY id ) AS rnk FROM MyTable GROUP BY userid) SELECT r.* FROM r WHERE r.Rank <= 6 来源: https://stackoverflow.com/questions/14025255/how-to

'invalid column name' while using the HAVING

主宰稳场 提交于 2019-12-10 20:18:17
问题 I am using Microsoft SQL SERVER 2014. The following is my query SELECT type, SUM(calories) AS total_calories FROM exercise_logs GROUP BY type HAVING total_calories > 150; and I get the error Msg 207, Level 16, State 1, Line 2 Invalid column name 'total_calories'. it is a very simple table (I am new to sql and learning it). can somebody point out what am i doing wrong? Thanks. 回答1: Aggregation is required , as you have no access to alias total_calories SELECT type,SUM(calories) AS total

Does MySQL use indexes on Having?

纵然是瞬间 提交于 2019-12-10 16:12:49
问题 A portion of my query looks like: HAVING date > '2011-04-13 04:28:03' The date variable is indexed, does this have any effect on the query? EXPLAIN EXTENDED doesn't seem to be using the index, but I don't know if that's only because I have 4 rows in the database that I'm testing with. My query: SELECT AVG(slen) FROM ( SELECT date, COUNT(id) as slen FROM table WHERE product_id = 2830 GROUP BY id HAVING date > '2011-04-13 04:28:02' ) as T There are a few rows that have different date values. I

SQL Count where clause

試著忘記壹切 提交于 2019-12-10 14:32:42
问题 I have the the following SQL statement: SELECT [l.LeagueId] AS LeagueId, [l.LeagueName] AS NAME, [lp.PositionId] FROM (Leagues l INNER JOIN Lineups lp ON l.LeagueId = lp.LeagueId) WHERE (lp.PositionId = 1) OR (lp.PositionId = 3) OR (lp.PositionId = 2) What I really need is to get the rows where the count of the position is greater than a number. Something like: SELECT [l.LeagueId] AS LeagueId, [l.LeagueName] AS NAME, [lp.PositionId] FROM (Leagues l INNER JOIN Lineups lp ON l.LeagueId = lp