Home > Software engineering >  How to sort a table by two columns crosswise
How to sort a table by two columns crosswise

Time:10-17

I have a table that belongs to a person's cell phone texting called 'Ali'. He received text messages from people and sent his text message reply to those people.

MessageTable looks like this:

id sendDate sendTime sender receiver
1 1397/01/02 10:02 Karim Ali
2 1398/05/09 05:30 Hamed Ali
3 1398/06/07 05:10 Majid Ali
4 1398/08/09 06:12 Ali Karim
5 1399/02/01 07:15 Ali Saeed
6 1399/07/02 08:51 Parsa Ali
7 1399/08/06 12:20 Ali Karim
8 1399/09/04 20:01 Ali Hamed
9 1399/12/08 22:05 Asgar Ali
10 1400/01/01 23:11 Majid Ali
11 1400/02/13 14:10 Karim Ali
12 1400/04/05 16:25 Ali Hamed
12 1400/06/12 22:25 Ali Majid
12 1400/07/24 08:25 Saeed Ali

I want the table to be arranged in such a way that, firstly, all the text messages that Ali had with each person are arranged separately, and secondly, the date and time are displayed in order. something like this:

id sendDate sendTime sender receiver
1 1397/01/02 10:02 Karim Ali
4 1398/08/09 06:12 Ali Karim
7 1399/08/06 12:20 Ali Karim
11 1400/02/13 14:10 Karim Ali
2 1398/05/09 05:30 Hamed Ali
8 1399/09/04 20:01 Ali Hamed
12 1400/04/05 16:25 Ali Hamed
3 1398/06/07 05:10 Majid Ali
10 1400/01/01 23:11 Majid Ali
12 1400/06/12 22:25 Ali Majid
5 1399/02/01 07:15 Ali Saeed
12 1400/07/24 08:25 Saeed Ali
6 1399/07/02 08:51 Parsa Ali
9 1399/12/08 22:05 Asgar Ali

I need SQL code, Can every body help me?

CodePudding user response:

Create a calculated column with a CASE statement that sets its value to the sender if the receiver is Ali and to the receiver if the sender is Ali.

Sort by this column and the date

CodePudding user response:

I come from Java. I do not use SQL. But I had a similar problem a while ago and I will just explain how I solved it: I created an ArrayList of Objects of the type of Class. In Your case these would the the calls, or messages. These objects have specific data. I then would use a model to add data in the list and change the model, not the list. I would then create an 2D array of Strings that contain the Data. When you change the data, you just rearrange the way the 2D Array is created. Also use a loop to create the 2D Array so your list can be infinitly big. I used all the trigger words. You may want to do the research on how to do it with SQL yourself. If it does help, let me know.

  •  Tags:  
  • sql
  • Related