subquery

Select last 30 items per group by

≯℡__Kan透↙ 提交于 2019-12-08 05:44:20
问题 Hopefully the title makes any sense. For this example I'll have the next table in my database measurements ================================== stn | date | temp | time = 1 | 01-12-2001 | 2.0 | 14:30 = 1 | 01-12-2001 | 2.1 | 14:31 = 1 | 03-12-2001 | 1.9 | 21:34 = 2 | 01-12-2001 | 4.5 | 12:48 = 2 | 01-12-2001 | 4.7 | 12:49 = 2 | 03-12-2001 | 4.9 | 11:01 = ================================== And so on and so forth. Each station (stn) has many measurements, one per day second . Now I want to select

How to create a join that uses a subquery?

牧云@^-^@ 提交于 2019-12-08 05:27:02
问题 How do you convert the following SQL Query to a CakePhp Find Query? SELECT a.id, a.rev, a.contents FROM YourTable a INNER JOIN ( SELECT id, MAX(rev) rev FROM YourTable GROUP BY id ) b ON a.id = b.id AND a.rev = b.rev I have tried the code below: return $model->find('all', [ 'fields' => $fields, 'joins' => [ [ 'table' => $model->useTable, 'fields' => ['id','MAX(rev) as rev'], 'alias' => 'max_rev_table', 'type' => 'INNER', 'group' => ['id'], 'conditions' => [ $model->name.'.id= max_rev_table.id

Sequelize Top level where with eagerly loaded models creates sub query

放肆的年华 提交于 2019-12-08 05:14:01
问题 I'm running an into issue where Sequelize creates a subquery of the primary model and then joins the includes with that subquery instead of directly with the primary model table. The query conditions for the include(s) ends up inside the subquery's WHERE clause which makes it invalid. I have shortened names down trying to keep this compact hopefully without losing any relevant info. Environment: Nodejs: 6.11.3 Sequelize: 3.23.6 => Updated to 4.38.1 and problem persists MySql: 5.7.23 Code snip

Can we make a DISTINCT of a group_concat(distinct somefield)?

我只是一个虾纸丫 提交于 2019-12-08 05:11:40
问题 http://sqlfiddle.com/#!2/37dd94/17 If I do SELECT DISTINCT I get the same results as doing just SELECT . On the query results, you will see two activities that contains the District "Evora". Only one should appear. Any clue? 回答1: How about the following query (SQL FIDDLE): SELECT GROUP_CONCAT(APA_T.district), t.name FROM tbl_activity AS t JOIN tbl_activity_package AS ap ON t.id = ap.id_activity JOIN ( SELECT DISTINCT apa.district AS district, ( SELECT s1.id_activity_package FROM tbl_activity

How to set parameters in nested query in DQL

核能气质少年 提交于 2019-12-08 04:35:50
问题 Hi , I'd like to get number of created articles by a user, the query used to work until I added some parameters filtering the query by "fromDate" and "toDate" dates, here's my query : // query String $dql = 'SELECT u.idUser, u.lastName, u.email, u.mobile, (SELECT AVG(n.note) FROM MyBundle:Note n WHERE n.noteFor = u.idUser) AS note, (SELECT COUNT(a) FROM MyBundle:Article a WHERE (a.createdBy = u.idUser) AND (a.createdAt BETWEEN :fromDate AND :toDate)) AS articles FROM MyBundle:User u'; //

MySQL 'WHERE' clause which excludes results in subquery

霸气de小男生 提交于 2019-12-08 04:09:24
问题 I have the following SQL query: SELECT members.member_ID, members.nick_name FROM orgs INNER JOIN assets ON assets.org_ID = orgs.org_ID INNER JOIN orgs_to_members ON orgs_to_members.org_ID = orgs.org_ID INNER JOIN members ON members.member_ID = orgs_to_members.member_ID where orgs.org_ID = '7' AND NOT EXISTS (select shares.member_ID from shares where shares.asset_ID = '224') There are 3 members in org 7: - member_ID 1 - member_ID 4 - member_ID 6 In the subquery, member IDs 1 and 4 result. I am

Not able to execute sql query

↘锁芯ラ 提交于 2019-12-08 03:22:11
问题 select tt.threshold_id from (select sum(amount) over (partition by tt.threshold_type order by tt.threshold_type ) amt from cash_transactions) cash, thresholds tt where tt.threshold_amount < cash.amt the rdms involved is oracle the error is " ORA-00904: "TT"."THRESHOLD_TYPE": invalid identifier" what i want to do with this query is : threshold table contains a column threshold type which contain column name of the cash transactions table And for each record from the threshold table we need to

TSQL Counting number of Consecutive Absence in a row within time period

末鹿安然 提交于 2019-12-08 03:15:28
问题 Problem: I'm trying to calculate the number of consecutive absence each student have for a particular class within a week period. e.g. If class MATH1234 has classes on Day 1 Period 4 and Day 4 Period 3 , and student 0012345 was absent for Day 1 Period 4 and Day 4 Period 3 in Week 1 , it is the same as if that student was absent for Day 4 Period 3 in Week 1 and Day 1 Period 4 in Week 2 . I have a table called Lessons that contain a running list of all the students and the classes they are

Help me turn a SUBQUERY into a JOIN

可紊 提交于 2019-12-08 02:41:51
问题 Two tables. emails id (int10) | ownership (int10) messages emailid (int10) indexed | message (mediumtext) Subquery (which is terrible in mysql). SELECT COUNT(*) FROM messages WHERE message LIKE '%word%' AND emailid IN (SELECT id FROM emails WHERE ownership = 32) The usage here is that I run a search on emails (which is obviously simplified in the sample above), that generates a list of say 3,000 email id's. I then want to do a search against messages because i need to do a text match - from

Laravel - named subquery using Query Builder

大兔子大兔子 提交于 2019-12-08 02:12:01
问题 In MySQL subqueries documentation there's an exmaple of subquery: SELECT ... FROM (subquery) [AS] name ... Here's the raw query which I want to transform: select SUBQUERY_NAME.* from (select id, name from items) AS SUBQUERY_NAME Is there any way to do this in Laravel Query Builder without using DB::raw() ? 回答1: Unfortunatelly no. The Query Builder has its limitations and more complex queries are outside its scope, that's why DB::raw() is there. But, if you want to make it a little more