Maybe I am trying to do something that can't be done, as my searches don't seem to find any similar examples.
I am trying to write a query in MariaDB that lists the lineitems (detail) as well as the subtotals, totals, and grand totals.
So for instance if I have the following data:
DonationDate
UserContactID
DonationAmount
2021-05-20
613
5.00
2021-05-20
613
40.00
2021-06-22
613
6.00
2021-06-22
613
10.00
2021-06-22
777
10.00
2021-06-22
777
30.00
DonationDate
UserContactID
DonationAmount
2021-05-20
613
5.00
2021-05-20
613
40.00
sub for 613 on 2021-05-20
45.00
tot for 2021-05-20
45.00
2021-06-22
613
6.00
2021-06-22
613
10.00
Sub for 613 on 2021-06-22
16.00
2021-06-22
777
10.00
2021-06-22
777
30.00
Sub for 777 on 2021-06-22
40.00
tot for 2021-06-22
56.00
When I try to use rollups I lose the individual lines. For example, 2021-05-20 shows 45, not one line for 40 and the other for 5.
for example:
SELECT
DonationDate,UserContactID,DonationID, sum(DonationAmount)
FROM
donations
GROUP BY
DonationDate, UserContactID with Rollup`
The first two rows show up like this:
DonationDate
UserContactID
DonationAmount
2021-05-20
613
45
2021-05-20
45
Note that what we want is for that first 45 to show up as the detail items, 40 & 45, before showing up as as subtotal for those useids for that date.
Any help greatly appreciated. (P.S. The initial table with the data looks right on the preview, but I see it gets messed up when it actually posts. Sorry about that.)
Related
I am trying to parse 1st table located here using BeautifulSoup in Python. It parsed my First column but for some reason It didn't parsed entire table. Any help is appreciated!
Note: I am trying to parse entire table and convert into pandas dataframe
My Code:
import requests
from bs4 import BeautifulSoup
WIKI_URL = requests.get("https://en.wikipedia.org/wiki/NCAA_Division_I_FBS_football_win-loss_records").text
soup = BeautifulSoup(WIKI_URL, features="lxml")
print(soup.prettify())
my_table = soup.find('table',{'class':'wikitable sortable'})
links=my_table.findAll('a')
print(links)
It only parsed one column because you did a findall for only the items in the first column. To parse the entire table you'd have to do a findall for the table rows <tr> and then a findall within each row for the table divides <td>. Right now you are just doing a findall for the links and then printing the links.
my_table = soup.find('table',{'class':'wikitable sortable'})
for row in mytable.findAll('tr'):
print(','.join([td.get_text(strip=True) for td in row.findAll('td')]))
NOTE: Accept B.Adler's solution as it is good work and sound advice. This solution is simply so you can see some alternatives as you are learning.
Whenever I see <table> tags, I'll usually check out pandas first to see if I can find what I need from the tables that way. pd.read_html() will return a list of dataframes, and you can work/manipulate those to extract what you need.
import pandas as pd
WIKI_URL = "https://en.wikipedia.org/wiki/NCAA_Division_I_FBS_football_win-loss_records"
tables = pd.read_html(WIKI_URL)
You can also look through the dataframes to see which has the data you want.
I just used dataframe in index position 2 for this one, which is the first table you were looking for
table = tables[2]
Output:
print (table)
0 1 ... 6 7
0 Team Won ... Total Games Conference
1 Michigan 953 ... 1331 Big Ten
2 Ohio State 1 911 ... 1289 Big Ten
3 Notre Dame 2 897 ... 1263 Independent
4 Boise State 448 ... 618 Mountain West
5 Alabama 3 905 ... 1277 SEC
6 Oklahoma 896 ... 1274 Big 12
7 Texas 908 ... 1311 Big 12
8 USC 4 839 ... 1239 Pac-12
9 Nebraska 897 ... 1325 Big Ten
10 Penn State 887 ... 1319 Big Ten
11 Tennessee 838 ... 1281 SEC
12 Florida State 5 544 ... 818 ACC
13 Georgia 819 ... 1296 SEC
14 LSU 797 ... 1259 SEC
15 Appalachian State 617 ... 981 Sun Belt
16 Georgia Southern 387 ... 616 Sun Belt
17 Miami (FL) 630 ... 1009 ACC
18 Auburn 759 ... 1242 SEC
19 Florida 724 ... 1182 SEC
20 Old Dominion 76 ... 121 C-USA
21 Coastal Carolina 112 ... 180 Sun Belt
22 Washington 735 ... 1234 Pac-12
23 Clemson 744 ... 1248 ACC
24 Virginia Tech 743 ... 1262 ACC
25 Arizona State 614 ... 1032 Pac-12
26 Texas A&M 741 ... 1270 SEC
27 Michigan State 701 ... 1204 Big Ten
28 West Virginia 750 ... 1292 Big 12
29 Miami (OH) 690 ... 1195 MAC
.. ... ... ... ... ...
101 Memphis 482 ... 1026 The American
102 Kansas 582 ... 1271 Big 12
103 Wyoming 526 ... 1122 Mountain West
104 Louisiana 510 ... 1098 Sun Belt
105 Colorado State 520 ... 1124 Mountain West
106 Connecticut 508 ... 1107 The American
107 SMU 489 ... 1083 The American
108 Oregon State 530 ... 1173 Pac-12
109 UTSA 38 ... 82 C-USA
110 Kansas State 526 ... 1207 Big 12
111 New Mexico 483 ... 1103 Mountain West
112 Temple 468 ... 1094 The American
113 Iowa State 524 ... 1214 Big 12
114 Tulane 520 ... 1197 The American
115 Northwestern 535 ... 1240 Big Ten
116 UAB 126 ... 284 C-USA
117 Rice 470 ... 1108 C-USA
118 Eastern Michigan 453 ... 1089 MAC
119 Louisiana-Monroe 304 ... 727 Sun Belt
120 Florida Atlantic 87 ... 205 C-USA
121 Indiana 479 ... 1195 Big Ten
122 Buffalo 370 ... 922 MAC
123 Wake Forest 450 ... 1136 ACC
124 New Mexico State 430 ... 1090 Independent
125 UTEP 390 ... 1005 C-USA
126 UNLV11 228 ... 574 Mountain West
127 Kent State 341 ... 922 MAC
128 FIU 64 ... 191 C-USA
129 Charlotte 20 ... 65 C-USA
130 Georgia State 27 ... 94 Sun Belt
[131 rows x 8 columns]
I am using MySQL 5.6, and creating a view. It seems MySQL 5.6 view does not support subquery inside a view. I have two tables like the following:
table 1
account balance date time in out
408 100.00 2018-03-09 11:36:47 100.00 0.00
408 86.00 2018-03-09 11:54:48 0.00 14.00
408 74.00 2018-03-09 11:54:48 0.00 12.00
408 21.00 2018-03-09 11:54:48 0.00 13.00
408 11.00 2018-03-09 11:54:48 0.00 10.00
408 0.00 2018-03-09 11:54:48 0.00 11.00
408 13000000.00 2018-03-09 19:15:04 13000000.00 0.00
408 12999880.00 2018-03-10 00:51:37 0.00 120.00
408 12999640.00 2018-03-10 00:51:48 0.00 240.00
table2
account name
999 bank1
408 bank2
The view is created like this:
create view test
select table1.account, table2.name, table1.balance, table1.date from (table1 join table2) where table1.account=table2.account group by table1.account, table1.date
The problem is that the view picked out the first time, showed the first balance. But I want the max time on that day. For example, the view results are like:
408 bank1 100 2018-03-09
408 bank1 12999880.00 2018-03-10
The MySQL 5.6 does not allow me to use a subquery. So is there another way to show the correct balance like this:
408 bank1 13000000.00 2018-03-09
408 bank1 12999640.00 2018-03-10
which shows the lastest balance of the last record on that day.
This is what i have got in my SQL SERVER database table, where I am trying to calculate Balance Leave of an employee.
My actual data is:
EmpId EmpName EvalDate OpeningEL EnjoyedEL BalanceEL
12 CHANDRA 2014-04-01 18:30:00.000 0.95 0.00 0.95
12 CHANDRA 2014-05-01 18:30:00.000 1.30 0.00 1.30
12 CHANDRA 2014-06-01 18:30:00.000 1.20 1.00 1.20
12 CHANDRA 2014-07-01 18:30:00.000 1.25 0.00 1.25
12 CHANDRA 2014-08-01 18:30:00.000 1.25 1.00 1.25
But i need the data in below way
EmpId EmpName EvalDate OpeningEL EnjoyedEL BalanceEL
12 CHANDRA 2014-04-01 18:30:00.000 0.95 0.00 0.95
12 CHANDRA 2014-05-01 18:30:00.000 2.25 0.00 2.25
12 CHANDRA 2014-06-01 18:30:00.000 3.45 1.00 2.45
12 CHANDRA 2014-07-01 18:30:00.000 3.70 0.00 3.70
12 CHANDRA 2014-08-01 18:30:00.000 4.95 1.00 3.95
Previous BalanceELs are added with next OpeningELs.
So, how to achieve this....Please suggest something.
You can use CROSS APPLY and GROUP BY to achieve this
OpeningEL and BalanceEL from CROSS APPLY will get the sum of current and previous records for an employee.
SELECT
EL1.EmpId,
EL1.EmpName,
EL1.EvalDate,
Temp.OpeningEL,
EL1.EnjoyedEL,
Temp.BalanceEL
FROM EmployeeLeave EL1
CROSS APPLY
(
SELECT
SUM(OpeningEL) as OpeningEL,
SUM(BalanceEL) - SUM(EnjoyedEL) as BalanceEL
FROM EmployeeLeave EL2
WHERE EL2.EmpId = EL1.EmpId
AND EL2.EvalDate <= EL1.EvalDate
)Temp;
I am trying to create a query from sales-order-details and invoice for an income statement sales revenues.
when I put this into the sql to look up the query
SELECT salesno, quantity, price, price* quantity
FROM inventory, sales_order_details
LIMIT 0, 5
it keeps giving the same quantity and sales number for all of them. the prices are right and so is the price.. what am I doing wrong?
salesno quantity price and quantity*price
5455 153 2.00 306.00
5455 153 6.00 918.00
5455 153 5.00 765.00
5455 153 4.00 612.00
5455 153 5.00 765.00
My sales-order-details table looks like this
SQL query:
SELECT * FROM `sales_order_details` ORDER BY `sales_order_details`.`quantity` DESC LIMIT 0, 5 ;
Rows: 5
salesno inno quantity
5459 6319 290
5458 6318 220
5455 6315 153
5457 6317 110
5456 6316 101
and my invoice table looks like this
SQL query:
SELECT * FROM `inventory` LIMIT 0, 30 ;
Rows: 5
inno pname qoh price cost
6315 roses 500 2.00 1.00
6316 sunflowers 200 6.00
6317 dandy lions 270 5.00
6318 violets 300 4.00 2.00
6319 tulips 130 5.00 2.50
I need inno, pname, price, quantity and the price*quantity
I have a matrix report that has four columns and is sorted descending on the last columns values. The problem I have is when there is a tie I would like to use the value in the prior column or the one prior to that if there is still a tie. Below is a sample of my output and what I'm after is for Nissan and Renault to be switched. This is the expression I'm currently using in my group sort
=IIF(Fields!YearSold.Value = MAX(Fields!YearSold.Value),0, Fields!UnitSold.Value)
2009 2010 2011 2012
Make Units Units Units Units
Chevy 1,104 842 811 927
Volvo 1,054 905 792 879
Ford 1,638 923 718 809
Nissan 918 794 725 791
Renault 840 698 759 791
Mazda 722 535 460 621
Lexus 786 590 551 563
You can sort a tablix on multiple columns. Edit the tablix Sort properties, adding the additional columns in order - the tablix will be sorted that order, starting with the top column.