I have table with ID DOB AMOUNT RECEIVER_NAME SENDER_NAME SETTLE_FEE columns.
Sample Data
ID DOB AMOUNT RECEIVER_NAME SENDER_NAME SETTLE_FEE
-------------------------------------------------------------------
1 10-06-1990 100 Jose Benn 12
2 12-06-1990 200 Jim Mike 12
3 10-06-1990 300 Kate Benn 12
4 12-06-1990 100 Amy Mike 12
5 10-06-1990 200 Alison Benn 12
6 12-06-1990 300 Mary Mike 12
Expected result
ID DOB SENDER_NAME
---------------------------
1 10-06-1990 Benn
|--------AMOUNT RECEIVER_NAME SETTLE_FEE
100 Jose 12
300 Kate 12
200 Alison 12
2 12-06-1990 Mike
|--------AMOUNT RECEIVER_NAME SETTLE_FEE
200 Jim 12
100 Amy 12
300 Mary 12
I need to get all the data of each sender name.
I tried using group_concat(), But, it can take only 1024 characters.
So, what is the efficient way to achieve this scenario.
I can't use PL/SQL and no session related values allowed.
I need all the receiver's name, amount, fee for all the sender_name. What is the efficient way to do it.
Thanks,
Jose
There is a solution:
Just use an ORDER BY:
SELECT ID, DOB,SENDER, AMOUNT, RECEIVER_NAME, SETTLE_FEE
FROM inputTable
ORDER BY SENDER,DOB;
In displaying as you want the data, you just use code like (dummy code, javscriptish):
var previousSender="";
while(var row=db.fetch()) {
if(row.sender!=previousSender) {
console.log(id+" "+row.dob+" "+row.sender);
previousSend=row.sender();
} else {
console.log(" |-------"+row.amount+" "+row.receiver_name+" "+row.settle_fee);
}
}
As you can see: id is random since it has no relation to the dob or sender in your model. It is just a row counter.
Related
I have two tables as shown below: Table_Report needs to be updated from Daily_Records under these conditions:
Sold_Summary column needs to be updated by concatenating all Sold_Item where PIN is the same. Excluding special sold { Laptop, Smartphone, Tablets} which needs to be recorded in the Special_sold column.
Dest_Summary column needs to be updated by concatenating all Dest when PIN is the same.
Special_sold column needs to be updated the same way when PIN is the same.
Special_sold column only looks in the Sold_Item column if It finds any special sold { Laptop, Smartphone, Tablets} it records that under Special_sold when PIN is the same.
Table1: Daily_Records
**ID
Date
PIN
Sold_Item
Dest**
1
2021-04-20
110
Bag
Dawa
2
2021-04-21
131
Table
Harare
3
2021-04-22
110
Chair
Gondar
4
2021-04-23
120
Smartphone
Dawa
5
2021-04-24
111
Laptop
Adama
6
2021-04-25
120
Chair
Dawa
7
2021-04-26
111
Book
Harare
Table2: Table_Report
**ID
PIN
Sold_Summary
Dest_Summary
Special_sold**
1
110
Bag, Chair
Dawa, Gondar
null
2
111
Book
Adama, Harare
Laptop
3
120
Chair
Dawa
Smartphone
4
131
Table
Harare
null
I wouldn't use a trigger for this. Just run a query:
select pin,
group_concat(distinct case when sold_item not in ('Laptop', 'Smartphone', 'Tablet') then sold_item end) as sold_items,
group_concat(distinct case when sold_item in ('Laptop', 'Smartphone', 'Tablet') then sold_item end) as sold_items_special,
group_concat(distinct dest) as dest
from daily_records
group by pin;
You don't explain what id is. You can add such a column as:
row_number() over (order by pin) as id
Sounds simple but I couldn't find the solution for it.
I have a table with 3 columns. Account, Amount, Date.
I want to get all entries except the ones of one specific account with negative amount. But I still want to get the entries of this account if amount value is positive.
So with this query I'm also not getting the entries from account1 with a positive amount.
select * from table where (account!='account1' AND amount<='0') AND date='2020-05-01'
You can do this using WHERE NOT in your statement.
Example schema:
Account Amount Date
=====================================
1 Ben 200 2020-10-10
2 Frank 200 2020-10-10
3 Ben -300 2020-10-12
4 Ben 10 2020-10-16
5 Mary 2000 2020-10-16
6 Frank -200 2020-10-18
7 Ben -10 2020-10-18
8 Ben 0 2020-10-20
Now if you build your query like this
SELECT * FROM t1 WHERE NOT (account='Ben' AND amount<0);
you should get what you want (all records except the 3rd and 7th).
Edit: if you really only want to exclude records with negative amounts, you need to do < rather than <= as you did in your example above. Depends on whether you want row 8 to be included in the result or not.
This is the first question i post in stackoverflow. Hope that you guys can help me resolve this problem. I have been stuck for 2 days.
I have 6 tables. It's all below:
students:
id name lastname
1 John Snow
2 Sansa Stark
3 T-Bag Bagwell
student_course:
id student_id course_id course_start course_end
1 1 1 2015-06-19 2015-08-20
2 2 3 2015-07-09 2015-09-15
3 3 1 2015-05-15 2015-08-22
payment:
payment_id student_id course_id
1 1 1
2 2 3
3 3 1
payment_initial:
payment_id payment_due
1 2015-06-12
3 2015-05-08
payment_installment:
payment_id payment_due int_payment_due
2 2015-07-02 2015-07-15
passport_visa:
student_id passport_expiry_date visa_expiry_date
1 2015-09-10 2015-10-12
2 2015-09-12 2015-09-15
3 2015-10-11 2015-9-28
And the result i want is: result will be sort by date combined form 3 tables which have "Date" field. "Date" field after sorting include only date after present.
How can I make query string that bring me the result like this:
student_id(1) course_id(1) course_start(2015-06-19)
student_id(2) course_id(3) payment_id(2) payment_due(2015-07-02)
student_id(2) course_id(3) course_start(2015-07-09)
student_id(2) course_id(3) payment_id(2) int_payment_due(2015-07-15)
student_id(1) course_id(1) course_end(2015-08-20)
student_id(3) course_id(1) course_end(2015-08-22)
student_id(3) passport_expiry_date(2015-09-12)
.....
I want to add Name and Lastname at the result but it show too long. So I just write like that.
Last result i want to get is the date field (sorted), and which event of date will happen (course start, course end, payment due...)
Sorry if my English grammar is bad. Please help me. Thank you all.
I just collect all result after insert data (which has date field) into one table, and sort them in that table. That makes more insert query but easy to sort.
So I do it on that way.
Here is my sample data:
id name age location staff score
-------------------------------------------------
1 Joe 21 uk 0 100
2 Sam 27 uk 0 200
3 Luke 21 uk 1 3000
4 Kim 25 usa 0 60
I want to reset the score back to say 10, for all, except Luke on id 3, without resetting Luke back to 0.
I'd code it like this:
UPDATE users_stats SET score = '10' WHERE staff = '0' AND not(id = '3')
But that resets id 3 to 0. I don't want it touching the id 3.
One way to do that is like this:
UPDATE users_stats
SET score = 10
WHERE staff = 0 AND
id <> 3
A more common way would be:
UPDATE users_stats
SET score = 10
WHERE staff = 0 AND
id != 3
Live DEMO
In MySQL they both mean the same thing:
If id is different than 3
Also just as a side note, since Luke is a staff 1 you don't even need the id check as it will only change the score of those with staff 0 to score 10.
This is my first post, so please forgive me for any formatting errors.
I need to find the time between each row of data for a large number of users, and then take the average of those times as a function of time. I know how to to do the latter part, I just don't know how to find the time differences between rows.
For example, I can generate this output very easily (for all users sequentially, just using 'John' as an example):
User order time
John 13 2013-10-31 22:35:19
John 12 2013-10-18 23:16:50
John 11 2012-12-07 00:38:34
John 10 2012-06-10 02:19:14
John 9 2010-07-02 00:55:54
John 8 2009-12-20 23:43:41
John 7 2009-12-20 01:14:32
John 6 2009-12-18 15:12:40
John 5 2009-12-13 00:38:38
John 4 2009-12-12 16:00:13
John 3 2009-12-12 14:50:18
John 2 2009-12-11 18:41:15
John 1 2009-12-04 03:12:06
But, what I need to find out how to do is create this:
User order time timeDiff
John 13 2013-10-31 22:35:19 13
John 12 2013-10-18 23:16:50 300
John 11 2012-12-07 00:38:34 170
John 10 2012-06-10 02:19:14 ...
John 9 2010-07-02 00:55:54 ...
John 8 2009-12-20 23:43:41 ...
John 7 2009-12-20 01:14:32 ...
John 6 2009-12-18 15:12:40 ...
John 5 2009-12-13 00:38:38 ...
John 4 2009-12-12 16:00:13 ...
John 3 2009-12-12 14:50:18 ...
John 2 2009-12-11 18:41:15 ...
John 1 2009-12-04 03:12:06 NULL
This is easily done in excel, but I need the grouping properties for users + dates in order to do some analysis.
All help appreciated!!
Try this
Select current.time - previous.time As TimeDiff
From UserTable current Join
UserTable previous On current.User = pervious.User And current.Order - 1 = previous.Order
Where current.User = 'John'
That should get you the time differences.
You said you knew how to get the average from there.
Like you said, it's easy in Excel. Referring to relative rows in SQL is pretty hard, especially in MySQL.
You have to use server-side variables to establish "state", so you can remember previous values. e.g.
SET prev := null;
SELECT User, Order, time, IF(#prev IS NOT NULL, time - #prev, null), #prev := time;
It gets even harder since you're doing this for multiple users, and will have to add extra code to detect when the user changes.
For this kind of thing, you're better off doing the calcs in client-side code. Which means just do:
SELECT User, Order, UNIX_TIMESTAMP(time)
...
ORDER BY User, Order
and then you can feed those timestamps directly to PHP's time system for formatting/calculations.