How to sort the sender's names in inbox? - message

I want to sort an inbox by the sender's name.
I have the following MySQL query I am using to sort:
SELECT *
FROM (
SELECT *
FROM Msg_Tab
ORDER BY uid DESC
) AS searchmsg
GROUP BY sender;
But, here it's not sorting if I send any message.
It's only sorting while I'm receiving messages.
MySQL table : Msg_Tab
Columns: sender, receiver, time, message.

You must sort by the sender.
SELECT *
FROM (
SELECT *
FROM Msg_Tab
ORDER BY uid DESC
) AS searchmsg
GROUP BY sender
ORDER BY sender;

Related

Can anyone help me in this dbms query?

The Question-
Write a query to display the name(s) of the students who have secured the maximum marks in each subject, ordered by subject name in ascending order. If there are multiple toppers, display their names in alphabetical order.
Display it as subject_name and student_name.
O/P: first column - subject_name
second column - student_name
My answer-
select su.subject_name,st.student_name from subject su,student st,mark m
where m.student_id = st.student_id and m.subject_id = su.subject_id
and m.value = (select max(value) from mark group by subject_id);
Error-
and m.value = (select max(value) from mark group by subject_id)
*
ERROR at line 3:
ORA-01427: single-row subquery returns more than one row
What i know is I will have to make another subquery something like
and m.value =(select..... (select max(value) from mark group by subject_id));
But I am not getting it.
Nevermind got the answer, we will have to use IN operator and club value, subject_id as they are a set.
SELECT su.subject_name, st.student_name
FROM student s
JOIN mark m ON st.student_id = m.student_id
JOIN subject su ON m.subject_id = su.subject_id
WHERE (m.value, su.subject_id) IN (
SELECT max(value), subject_id FROM mark GROUP BY subject_id
)
ORDER BY su.subject_name, st.student_name;

bigQuery - Join

I'm trying to join two databases on the ID. The first database on price quotes does not have the data on websites, so I want to join it in from the logs database. However, in the logs database the ID is not unique, but the first chronological appearance of the ID - this is the right website.
When I run the query below, I get:
Resources exceeded during query execution.
Hence I don't know whether the problem is the code or something else.
Thanks
SELECT ID, user,busWeek, count(*) as num FROM [datastore.quotes]
Join (
select objectID, first(website) from (
select objectID, website, date from [datastore.allLogs]
order by date) group by objectID)
as Logs
on ID = objectID
group by ID,user,busWeek
Can you try:
SELECT ID, user,busWeek, count(*) as num FROM [datastore.quotes]
Join EACH (
select objectID, first(website) from (
select objectID, website, date from [datastore.allLogs]
order by date) group EACH by objectID)
as Logs
on ID = objectID
group by ID,user,busWeek
Note the 'EACH' - that keyword won't be needed in the future, but it's still useful today.
I think the issue is in ORDER BY. This brings all calculation to one node which causes "Resources Exceeded" message. I understand you need it to bring first (by date) website for each object.
Try to rewrite this select (inside join) to be partitioned.
For example using window functions with OVER(PARTITION BY ... ORDER BY)
In this case, I think, you have chance to make this in parallel
See below for reference
Window Functions

Sqlite Group By reverses the order

This is my query :
SELECT * FROM Message WHERE ParentMessage = ? GROUP BY MessageId
This reverses the order of the results.
Not sure why
Records on screen before Group By :
A
B
C
D
Records on screen after Group By :
D
C
B
A
In SQL, the results of a query do not have any guaranteed order unless you are using ORDER BY.
(In this case, it's likely that the query optimizer has estimated that using an index in a certain way would make the execution faster.)
If your tableName is Message and Column name is ParentMessage finally your row name is Message ID,the result is below like this
select * from Message WHERE ParentMessage in (MessageId) order by ParentMessage DESC
Otherwise
select * from Message WHERE ParentMessage order by MessageId DESC

Transform SQL JOIN SELECT to Esper EPL syntax

Let's consider a simple object with the same representation in a SQL database with properties(columns¨): Id, UserId,Ip.
I would like to prepare a query that would generate event in case that one user logs in from 2 IP adresses (or more) within 1 hour period.
My SQL looks like:
SELECT id,user_id,ip FROM w_log log
LEFT JOIN
(SELECT user_id, count(distinct ip) AS ip_count FROM w_log GROUP BY user_id) ips
ON log.user_id = ips.user_id
WHERE ips.ip_count > 1
Transformation to EPL:
SELECT * FROM LogEntry.win:time(1 hour) logs LEFT INNER join
(select UserId,count(distinct Ip) as IpCount FROM LogEntry.win:time(1 hour)) ips
ON logs.UserId = ips.UserId where ips.IpCount>1
Exception:
Additional information: Incorrect syntax near '(' at line 1 column 100,
please check the outer join within the from clause near reserved keyword 'select'
UPDATE:
I was successfuly able to create a schema, named window and insert data into it (or update it). I would like to increase the counter when a new LogEvent arrives in the .win:time(10 seconds) and decrease it when the event is leaving the 10 seconds window. Unfortunately the istream() doesn't seem to provide the true/false when the event is in remove stream.
create schema IpCountRec as (ip string, hitCount int)
create window IpCountWindow.win:time(10 seconds) as IpCountRec
on LogEvent.win:time(10 seconds) log
merge IpCountWindow ipc
where ipc.ip = log.ip
when matched and istream()
then update set hitCount = hitCount + 1
when matched and not istream()
then update set hitCount = hitCount - 1
when not matched
then insert select ip, 1 as hitCount
Is there something I missed?
In EPL I don't think it is possible to put a query into the from-part. You can change using "insert into". An EPL alternative is also a named window or table.

Row position in 100k+ records

i have this code to get the position of each record:
Message.all.each_with_index do |msg, i|
message_order[msg.id] = i
end
But now i have 100k+ messages and it takes to long to iterate over all the records.
Can anyone tell me how to do this more performant? (I'm using oracle)
I thought about rownum but didn't come to a solution.
A solution which returns the position for just one message would be great.
I don't know if I have understood your problem.
If you need the message with a specified order id (for example: 5), you can execute something like:
SELECT message
FROM (SELECT message, ROWNUM AS ID
FROM (SELECT message
FROM tab1
ORDER BY some_date))
WHERE ID = 5;
Or, using analytical functions:
SELECT message
FROM (SELECT message,
ROW_NUMBER() OVER(ORDER BY some_date) AS ID
FROM tab1)
WHERE ID = 5;
This should work:
Message.where("id < :id", id: msg.id).count

Resources