having

midterm solution around query in SQL

匆匆过客 提交于 2019-12-20 07:29:53
问题 This is the database: EMPLOYEE (fmane, minit, lname, ssn, birthdate, address, sex, salary, superssn, dno) KEY: ssn DEPARTMENT (dname, dnumber, mgrssn, mgrstartdate) KEY: dnumber. PROJECT (pname, pnumber, plocation, dnum) KEY: pnumber. WORKS_ON (essn, pno, hours) KEY: (essn, pno) DEPENDENT (essn, dependent-name, sex, bdate, relationship) KEY: (essn, dependent-name) The question asked is... Give the last name and SSN of the unmarried employees who work on two or more projects. SELECT e.Lname, e

sql having get only the first recorded row in table and i want all

こ雲淡風輕ζ 提交于 2019-12-20 04:26:06
问题 Somebody already helped me with this query but I made an adaptation and I get a problem : SELECT AVG(tyd.price) AS avg_price, COUNT(tyd.id_product) AS cnt, tyd.id_marchand, tyd.id_product, catalog.price AS c_price, tyd.price AS t_price, tyd.amount AS t_am, pro_tyd.amount AS p_am, pro_tyd.price AS p_price, catalog.img_src, tyd.step, tyd.login AS tyd_l FROM catalog INNER JOIN tyd ON catalog.id_marchand = tyd.id_marchand AND catalog.id_product = tyd.id_product AND tyd.step = "1" INNER JOIN pro

What is the semantic difference between WHERE and HAVING?

元气小坏坏 提交于 2019-12-20 04:11:45
问题 Let's put GROUP BY aside for a second. In normal queries (without GROUP BY ), what is the semantic difference? Why does this answer work? (put an alias in a HAVING clause instead of WHERE ) 回答1: HAVING operates on the summarized row - WHERE is operating on the entire table before the GROUP BY is applied. (You can't put GROUP BY aside, HAVING is a clause reserved for use with GROUP BY - leaving out the GROUP BY doesn't change the implicit action that is occurring behind the scenes). It's also

Difference between WHERE and HAVING in SQL [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-19 03:24:32
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: SQL: What's the difference between HAVING and WHERE? I have seen various discussions on WHERE and HAVING . I still have a question: is HAVING used only when considering aggregates, or can it be used in more general terms: whenever you have created or aliased a field in your query? I know that WHERE should always be used when possible. 回答1: HAVING specifies a search for something used in the SELECT statement. In

Using the 'case…when…then…else…end' construct in the 'having' clause in JPA criteria query

时间秒杀一切 提交于 2019-12-18 07:03:18
问题 The following criteria query calculates the average of rating of different groups of products. CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple>criteriaQuery=criteriaBuilder.createQuery(Tuple.class); Metamodel metamodel=entityManager.getMetamodel(); EntityType<Product>entityType=metamodel.entity(Product.class); Root<Product>root=criteriaQuery.from(entityType); SetJoin<Product, Rating> join = root.join(Product_.ratingSet, JoinType.LEFT); Expression<Number

What is the correct way to do a HAVING in a MongoDB GROUP BY?

≡放荡痞女 提交于 2019-12-17 10:53:46
问题 For what would be this query in SQL (to find duplicates): SELECT userId, name FROM col GROUP BY userId, name HAVING COUNT(*)>1 I performed this simple query in MongoDB: res = db.col.group({key:{userId:true,name:true}, reduce: function(obj,prev) {prev.count++;}, initial: {count:0}}) I've added a simple Javascript loop to go over the result set, and performed a filter to find all the fields with a count > 1 there, like so: for (i in res) {if (res[i].count>1) printjson(res[i])}; Is there a

How to group by in SQL by largest date (Order By a Group By)

五迷三道 提交于 2019-12-13 17:40:16
问题 I have the following database table Here is my sample data I have for it. What I am trying to figure out how to do is how to write a query to select all apntoken for userid='20' grouping by deviceid and then by apntoken as well (except that it should show the most recent apntoken). Some queries I have tried are this. SELECT DISTINCT apntoken,deviceid,created FROM `distribution_mobiletokens` WHERE userid='20' GROUP BY deviceid This returns the following result. Notice the date is not the

mysql how to find if at least one row from cross reference table is null or criteria

冷暖自知 提交于 2019-12-13 08:29:21
问题 i have trouble with mysql, i dont find the way to do it maybe i dont know the good mysql keyword mysql5 +----------+------------+----------+ | ID | FOREIGNKEY | TRAINER | +----------+------------+----------+ | ... | ... | ... | | 475 | 254 | NULL | | 476 | 254 | NULL | | 477 | 254 | NULL | | 478 | 286 | NULL | | 479 | 286 | FREE | | 480 | 286 | FREE | | 481 | 401 | FREE | | 482 | 401 | 1 | | 483 | 401 | FREE | | 484 | 405 | NULL | | 485 | 405 | 1 | | 486 | 405 | 5 | | 487 | 405 | FREE | | 488

mysql force order of results found to match order of IN clause

被刻印的时光 ゝ 提交于 2019-12-13 07:41:53
问题 This question is different to a commonly asked question about ordering the final results by the IN clause. I would like to force the results returned by the query that contains the IN clause, to match the order of the IN clause. This is the original question that I am working from. I'd like to alter the query below so that a row containing progress=2 occurs before progress=4 and progress=7 for each session_id when ordering the formation_page_hits table by datetime . Here is the current query:

SQL Counting Records with Count and Having

泄露秘密 提交于 2019-12-13 02:35:44
问题 I'm having problems with what I thought was a simple query to count records: SELECT req_ownerid, count(req_status_lender) AS total6 FROM bor_requests WHERE (req_status_lender = 0 AND req_status_borrower = 0) OR (req_status_lender = 1 AND req_status_borrower = 1) GROUP BY req_ownerid HAVING req_ownerid = 70 I thought this would count all the records where (req_status_lender = 0 AND req_status_borrower = 0) and (req_status_lender = 1 AND req_status_borrower = 1) and then give me the total but