I have one database table called ms_message: with 6 columns (id, senderid, receiverid, content, isRead, receivedTime). I want to get the latest received message group by sender in Doctrine but I cannot run the sub query or use order by and group by as well.
The query in SQL looks like this:
SELECT * FROM (SELECT * FROM ms_message WHERE receiverId = :receiver ORDER BY receivedTime) GROUP BY senderId;
I don't think that Doctrine supports queries like SELECT * FROM (SELECT ...)
Related
Would love to get some help here understanding an unexpected behavior with an Oracle SELECT query. And maybe there is a nicer way to construct my query.
Basically, this is the query that gets me the distinct set of IDs that I'm after:
select distinct(c.LOCATIONID) from COURSESESSION c where c.MARKETID=280 and c.STATUSID in(1, 2)
and c.COURSESESSIONID not in (SELECT distinct(REGISTRATION.COURSESESSIONID) FROM REGISTRATION)
But I also want all the data for these distinct IDs form COURSESESSION table, so I'm wrapping the above query in yet another select statement, so it looks like this:
select * from COURSESESSION cs where cs.COURSESESSIONID in
(select distinct(c.LOCATIONID) from COURSESESSION c where c.MARKETID=280 and c.STATUSID in(1, 2)
and c.COURSESESSIONID not in (SELECT distinct(REGISTRATION.COURSESESSIONID) FROM REGISTRATION));
The first query returns a set of 21 records, but the second set returns 19, so I'm pretty sure I'm not doing this right. The triple-select query is basically for these 2 steps:
Set idsSet = select distinct(c.LOCATIONID) from COURSESESSION c where c.MARKETID=280 and c.STATUSID in(1, 2)
and c.COURSESESSIONID not in (SELECT distinct(REGISTRATION.COURSESESSIONID) FROM REGISTRATION)
select * from COURSESESSION cs where cs.COURSESESSIONID in(idsSet);
Anyone knows how to fix my triple-select query?
Let me explain the question.
I have two tables, which have 3 columns with same data tpyes. The 3 columns create a key/ID if you like, but the name of the columns are different in the tables.
Now I am creating queries with these 3 columns for both tables. I've managed to independently get these results
For example:
SELECT ID, FirstColumn, sum(SecondColumn)
FROM (SELECT ABC||DEF||GHI AS ID, FirstTable.*
FROM FirstTable
WHERE ThirdColumn = *1st condition*)
GROUP BY ID, FirstColumn
;
SELECT ID, SomeColumn, sum(AnotherColumn)
FROM (SELECT JKM||OPQ||RST AS ID, SecondTable.*
FROM SecondTable
WHERE AlsoSomeColumn = *2nd condition*)
GROUP BY ID, SomeColumn
;
So I make a very similar queries for two different tables. I know the results have a certain number of same rows with the ID attribute, the one I've just created in the queries. I need to check which rows in the result are not in the other query's result and vice versa.
Do I have to make temporary tables or views from the queries? Maybe join the two tables in a specific way and only run one query on them?
As a beginner I don't have any experience how to use results as an input for the next query. I'm interested what is the cleanest, most elegant way to do this.
No, you most probably don't need any "temporary" tables. WITH factoring clause would help.
Here's an example:
with
first_query as
(select id, first_column, ...
from (select ABC||DEF||GHI as id, ...)
),
second_query as
(select id, some_column, ...
from (select JKM||OPQ||RST as id, ...)
)
select id from first_query
minus
select id from second_query;
For another result you'd just switch the tables, e.g.
with ... <the same as above>
select id from second_query
minus
select id from first_query
I have an oracle query that uses a created table as part of the code. Every time I need to run a report I delete current data and import the new data I receive. This is one column of id's. I need to create a report on SSRS in which the user can input this data into said table as a parameter. I have designed a simple report that they can enter some of the id's into a parameter, but there may be times when they need to enter in a few thousand id's, and the report already runs long. Here is what the SSRS code currently says:
select distinct n.id, n.notes
from notes n
join (
select max(seq_num) as seqnum, id from notes group by id) maxresults
on n.id = maxresults.ID
where n.seq_num = maxresults.seqnum
and n.id in (#MyParam)
Is there a way to have MyParam insert data into a table I would join called My_ID, joining as Join My_Id id on n.id = id.id
I do not have permissions to create functions or procedures in the database.
Thank you
You may try the trick with MATERIALIZE hint which normally forces Oracle to create a temporary table :
WITH cte1 AS
( SELECT /*+ MATERIALIZE */ 1 as id FROM DUAL
UNION ALL
SELECT 2 DUAL
)
SELECT a.*
FROM table1 a
INNER JOIN cte1 b ON b.id = a.id
How to do this query: SELECT * FROM (SELECT * FROM some_table ORDER BY id DESC LIMIT 3) a ORDER BY id with Eloquent in Laravel4?
Thanks in advance.
I just looked it up but couldn't find a way to do it.
What you can is to just create a normal Eloquent query and then sort it by PHP.
$query = MyModel::orderBy('id', 'desc')->limit(3)->get();
$query->sortBy(function($object) {
return $object->id;
});
dd($query->toArray());
Raw: DB::select('SELECT * FROM (SELECT * FROM some_table ORDER BY id DESC LIMIT 3) a ORDER BY id');
Why using Eloquent all the time? It has so many limitations. You have 3 options when querying a database. Eloquent, Query Builder and Raw.
I have an issue with using Oracle's union and order by clauses together.
I have two complex queries (with sub queries in them) having an order by clause for each of them. I need to union the output of both and return the result. When I run it, I am getting the error ORA-00933: SQL command not properly ended.
But it works when I comment out the order by clauses in both of them.
To test this, I created a simple query as simple as shown below
select * from employee where employee_id=2 order by name
union
select * from employee where employee_id=3 order by name;
Even this gave the same error when ran with order by clauses but runs well when I commentout the order by clauses.
I tried searching forums, but I could not get solution for the exact problem. I found one at ORACLE Query with ORDER BY and UNION but As my queries are already too complecated because of subqueries and joins between too many tables, I dont want to implement this.
Can someone help me on fixing the root cause of the issue.
try this code:
select e1.name name /* e1.* */
from employee e1
where employee_id = 2
union
select
e2.name name /* e2.* */
from employee e2
where employee_id = 3
order by name;
if you want to order the result of first query then to order the result the second query so you can do like this:
select 1 query, e1.name name /* e1.* */
from employee e1
where employee_id = 2
union
select
2 query, e2.name name /* e2.* */
from employee e2
where employee_id = 3
order by query, name;
You can have only one ORDER BY when combining multiple queries, on the last statement. The ORDER BY clause acts on the entire set.
See the Oracle Documentation:
You cannot specify the order_by_clause in the subquery of these operators.
If you want order by in each query you must wrap it in other select as a subquery:
select * from (select * from employee where employee_id=2 order by name)
union
select * from (select * from employee where employee_id=3 order by name);