I want to sort a table having 3 columns (time, source , recipient) by the order by which communication is being made. If the source and recipient are conversing together then it will list them by the time. The goal is to see the communication happening between similar people ordered by time.An example is as:
time|source|recipient
1 paul amy
2 amy paul
3 amy paul
5 paul jane
8 amy paul
9 jane paul
10 paul amy
11 paul jane
the end result would be like
1 paul amy
2 amy paul
3 amy paul
8 amy paul
10 paul amy
5 paul jane
9 jane paul
11 paul jane
Your question is a bit vague. My educated guess is you want this:
SELECT *
FROM tbl
ORDER BY (GREATEST(source, recipient), LEAST(source, recipient), "time";
The manual about GREATEST and LEAST.
Related
I need to create column with name(s) (Supervisors - can be multiple supervisors at the same time, but also there might not be supervisor at all) from JSON format column, that not in 2 other column with names (Employee and Client).
Id
Employee
Client
AllParticipants
1
Justin Bieber
Ariana Grande
[{"ParticipantName":"Justin Bieber"},{"ParticipantName":"Ariana Grande"}]
2
Lionel Messi
Christiano Ronaldo
[{"ParticipantName":"Christiano Ronaldo"},{"ParticipantName":"Lionel Messi"}]
3
Nicolas Cage
Robert De Niro
[{"ParticipantName":"Robert De Niro"},{"ParticipantName":"Nicolas Cage"},{"ParticipantName":"Brad Pitt"}]
4
Harry Potter
Ron Weasley
[{"ParticipantName":"Ron Weasley"},{"ParticipantName":"Albus Dumbldor"},{"ParticipantName":"Harry Potter"},{"ParticipantName":"Lord Voldemort"}]
5
Tom Holland
Henry Cavill
[{"ParticipantName":"Henry Cavill"},{"ParticipantName":"Tom Holland"}]
6
Spider Man
Venom
[{"ParticipantName":"Venom"},{"ParticipantName":"Iron Man"},{"ParticipantName":"Superman"},{"ParticipantName":"Spider Man"}]
7
Andrew Garfield
Leonardo DiCaprio
[{"ParticipantName":"Tom Cruise"},{"ParticipantName":"Andrew Garfield"},{"ParticipantName":"Leonardo DiCaprio"}]
8
Dwayne Johnson
Jennifer Lawrence
[{"ParticipantName":"Jennifer Lawrence"},{"ParticipantName":"Dwayne Johnson"}]
The output column I need:
Supervisors
NULL
NULL
Brad Pitt
Albus Dumbldor, Lord Voldemort
NULL
Iron Man, Superman
Tom Cruise
NULL
I've tried to create extra columns to use Case expression after that, but it seems too complex.
SELECT *,
JSON_VALUE(w.AllParticipants,'$[0].ParticipantName') AS ParticipantName1,
JSON_VALUE(w.AllParticipants,'$[1].ParticipantName') AS ParticipantName2,
JSON_VALUE(w.AllParticipants,'$[2].ParticipantName') AS ParticipantName3,
JSON_VALUE(w.AllParticipants,'$[3].ParticipantName') AS ParticipantName4
FROM Work AS w
I'm wondering if there is an easy way to compare values and extract only unique ones.
The goal is to try and obtain two random sample cases per handler ID.
The data for this project is below.
ID Complaint Handler Handler ID Reference Outcome Handler Notes
1 John Doe h384 R38423 Uphold Not Applicable
2 Ryan Jones h632 R38482 Uphold Not Applicable
3 Chris Smith h238 R84823 Defend Not Applicable
4 Emily Surry h634 R48384 Reject Not Applicable
5 Elle Smith h123 R48823 Uphold Not Applicable
6 Jane Doe h324 R48282 Uphold Not Applicable
7 Joe Bloggs h538 R83322 Reject Not Applicable
8 Ryan Jones h632 R38299 Defend Not Applicable
9 Chris Smith h238 R83482 Reject Not Applicable
10 Chris Smith h238 R91823 Reject Not Applicable
11 Joe Bloggs h538 R18291 Uphold Not Applicable
I have used the following query to select all the unique case handler references.
SELECT Cases.[Handler ID]
FROM Cases
GROUP BY Cases.[Handler ID];
I then need to loop through all these unique references and execute the following query
SELECT TOP 2 *
FROM Cases
WHERE Cases.[Handler ID] = 'XXXXXX'
ORDER BY Rnd(ID)
An example of the result would be
ID Complaint Handler Handler ID Reference Outcome Handler Notes
1 John Doe h384 R38423 Uphold Not Applicable
2 Ryan Jones h632 R38482 Uphold Not Applicable
3 Chris Smith h238 R84823 Defend Not Applicable
4 Emily Surry h634 R48384 Reject Not Applicable
5 Elle Smith h123 R48823 Uphold Not Applicable
6 Jane Doe h324 R48282 Uphold Not Applicable
7 Joe Bloggs h538 R83322 Reject Not Applicable
8 Ryan Jones h632 R38299 Defend Not Applicable
10 Chris Smith h238 R91823 Reject Not Applicable
11 Joe Bloggs h538 R18291 Uphold Not Applicable
Result example: Row 9 randomly removed as there was three Chris Smith's.
No other rows affected as there is 2 or less results.
I think this will work in MS Access:
select c.*
from cases as c
where c.id in (select top 2 c2.id
from cases as c2
where c2.handler = c.handler
order by rnd(-Timer() * c2.id)
);
Does anyone know how can I auto-number column "B" based on column "A"?
Let me give you an example on how column A looks like:
John
John
Mike
Mike
Mike
Mike
Andy
Andy
Andy
Andy
Andy
Andy
Andy
Now I want to add numbering in column "B" based on column A, like this:
John 1
John 2
Mike 1
Mike 2
Mike 3
Mike 4
Andy 1
Andy 2
Andy 3
Andy 4
Andy 5
Andy 6
Andy 7
I need to do this for a list of 20k records so I need to find an automatic solution.
You can use COUNTIF().
In B1 and drag down:
=COUNTIF($A$1:A1,A1)
I am trying to create a view that records the selected attributes for all Computer Science majors.
This is my query to create a view:
DROP VIEW CS_grade_report;
CREATE VIEW CS_grade_report AS
SELECT Student.student_id AS "ID",
student_name AS "Name",
course_number AS "Course #",
credit AS "Credit",
grade AS Grade
FROM Student, Class, Enrolls
WHERE major = 'CSCI'
AND Student.student_id = Enrolls.student_id
AND Class.schedule_num = Enrolls.schedule_num;
SELECT *
FROM CS_grade_report;
And this is what is generated:
ID Name Course # Credit GR
------ ------------------------- -------- ---------- --
600000 John Smith CSCI3200 4 B+
600000 John Smith CSCI3700 3 C
600000 John Smith SPAN1004 3 A-
600000 John Smith CSCI4300 3 A+
600001 Andrew Tram MUSC2406 2 A+
600001 Andrew Tram SPAN1004 3 A
600001 Andrew Tram CSCI3700 3 B-
600002 Jane Doe CSCI4200 3 D+
600003 Michael Jordan CSCI4300 3 A+
600004 Tiger Woods MUSC1000 1 A
600007 Dominique Davis CSCI4300 3 F
ID Name Course # Credit GR
------ ------------------------- -------- ---------- --
600009 Will Smith CSCI3200 4 A
600010 Papa Johns CSCI3200 4 B
600011 John Doe CSCI3200 4 C
600012 Jackie Chan CSCI3200 4 D
600013 Some Guy CSCI3200 4 E
16 rows selected.
I am assuming this is output from sqlplus. There is a "pagesize" option to define when breaks are added. If you only want to see one heading, set the size to a large enough value prior to running your SELECT statement as such:
set pagesize 500
(or whatever size you want)
There are many command options for sqlplus. This link is a good cheat-sheet.
Can someone please lend a hand with this query? I've been fooling with LIMIT or TOP, but I think I'm off track. I want to return all fields from a table, but with a max of 3 duplicate id's in the new table.
Table
id first last
===================
1 John Doe
1 John Doe
1 John Doe
1 John Doe
2 Mary Green
2 Mary Green
3 Stacy Kirk
3 Stacy Kirk
3 Stacy Kirk
3 Stacy Kirk
3 Stacy Kirk
Desired Results (up to 3 ids)
id first last
====================
1 John Doe
1 John Doe
1 John Doe
2 Mary Green
2 Mary Green
3 Stacy Kirk
3 Stacy Kirk
3 Stacy Kirk
Thanks!
since you mentioned TOP, this is for SQL SERVER
SELECT id, first, last
FROM
(
SELECT id, first, last,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY LAST) rn
FROM TABLE1
) s
WHERE s.rn <= 3
SQLFiddle Demo (SQL Server)