Summing data in google sheets based on multiple columns - google-sheets-query

I have an export from a time sheet program of hours worked on projects in the following format.
Existing Data
Resource | Project | Subtask | Hours
Alice | Artemis | Dev | 10
Alice | Artemis | Dev | 20
Alice | Artemis | Dev | 30
Alice | Boreas | Dev | 10
Beth | Artemis | Test | 10
Beth | Boreas | Test | 20
Beth | Boreas | Test | 30
Beth | Boreas | Test | 10
Claire | Boreas | Dev | 10
Claire | Boreas | Dev | 20
Claire | Boreas | Dev | 30
Claire | Boreas | Dev | 10
Can anyone tell me how to generate the following summary/ aggregation report? I assume the answer will involve Query, but I haven't been able to get the summing of the hours to work, and I wonder if the answer will require a combination of query and other functions.
Desired Output
Project | Subtask | Resource | Total Hours
Artemis | Dev | Alice | 60
Artemis | Test | Beth | 10
Boreas | Dev | Alice | 10
Boreas | Dev | Claire | 70
Boreas | Test | Beth | 60
Thanks in advance for any suggestions that move me forward.

Looks similar to https://webapps.stackexchange.com/questions/100274/google-sheets-query-group-by-multiple
Does something like this work?:
QUERY(ARRAYFORMULA(IMPORTRANGE(
<spreadsheet_url>,
<range>")),
"Select Project, Subtask, Resource, sum(Hours)
group by Project, Subtask, Resource label sum(Hours) 'Total Hours'")

Related

MySQL 5.5 How to do know the number of days when the user has login and the number of days he has never login

I have this table on MySQL database version 5.5.62 (remote hosting) for 2022 year.
------------------------------------------------------------------------------------
| number_access_total_year | number_access_month | number_access_day | access_user |
------------------------------------------------------------------------------------
| 16350 | 1363 | 45 | D1 |
| 14870 | 1239 | 41 | D2 |
| 13591 | 1133 | 37 | D3 |
| 13364 | 1114 | 37 | D4 |
| 12324 | 1027 | 34 | D5 |
------------------------------------------------------------------------------------
This table is the summary of the accesses registered for each user.
Each user access is stored in database table even if it is multiple times a day.
In the access table are stored:
user name (dt_user),
access day (dt_access),
access time (tm_access)
i. e.
---------------------------------------------
| dt_user | dt_access | tm_access | dt_ID |
---------------------------------------------
| D1 | 2022-08-19 | 11:18:36 | 120716 |
| D5 | 2022-03-21 | 23:18:36 | 120715 |
| D5 | 2022-03-21 | 12:24:13 | 120714 |
| D5 | 2022-03-21 | 08:46:55 | 120713 |
| D3 | 2022-01-16 | 04:41:11 | 120712 |
---------------------------------------------
5 rows in set (0.11 sec)
I would need to convert this data into days, because I need to know the number of days when the user has login and the number of days he has never login.
Any suggestion?
Any help really appreciated.
1.You have to find the day differences between 2 dates (todaydate, date that you want to start tracking the days)
example: SELECT DATEDIFF(CURDATE(),"2017-06-25");
Find the distinct dates from dt_access and get their count.
The number of days that user has loged in is the count of point no.2, the number of days that user has not logged in is: totalDays (of point no.1) - count of logged in users (point no.2)

Mysql syntax to get values by percentage

Im a beginner and I would like to ask how to get the 20% of the Rates Column Values (The Mysql Syntax for this)
.
How can I make another column using SELECT statement where the values are 20% of the Rates.
+-------------------+----------------------+------+
| Fullname | position | rate |
+-------------------+----------------------+------+
| terry, sanchez | Web Developer | 1500 |
| bernie, nahoma | Web Developer | 1500 |
| marie, viray | Web Developer | 1500 |
| lucy, dante | Web Developer | 1500 |
| benedict, cruz | Web Developer | 1500 |
| jhon paul, arroyo | Web Designer | 1200 |
| lewis, abante | Web Designer | 1200 |
| lito, lapid | Database Administrat | 1350 |
| jerwin, aton | Project Manager | 2000 |
| benjie, cruz | Dev Ops | 1250 |
+-------------------+----------------------+------+
select (0.2 * rate) as 20_percent_rate, rate from table
This should work for you.
select rates, (select (rates * 20) / 100) as percentage from table
This will give you desried result. selecting rates as the first column is totaly optional.

How do I get information from multiple MySQL tables?

I'm working on a way to keep track of physical connections between servers/switches in multiple racks. I set up my database so it is like this:
mysql> show tables;
+----------------------+
| Tables_in_System |
+----------------------+
| connections |
| racks |
| servers |
+----------------------+
mysql> select * from racks;
+----+-------------+-------------+----------+
| id | rack_number | rack_height | location |
+----+-------------+-------------+----------+
| 2 | 7430 | 43 | xxx |
| 3 | 7431 | 43 | xxx |
| 4 | 7432 | 43 | xxx |
+----+-------------+-------------+----------+
mysql> select * from servers;
+----+-------------+---------------+-----------------+---------------+
| id | server_type | rack_location | server_unit_loc | server_height |
+----+-------------+---------------+-----------------+---------------+
| 1 | Server 1 | 7430 | 2 | 3 |
| 2 | Cisco 2960 | 7431 | 1 | 1 |
| 3 | Server 2 | 7431 | 9 | 1 |
| 15 | Server 3 | 7432 | 27 | 2 |
| 16 | Cisco 2248 | 7432 | 29 | 1 |
+----+-------------+---------------+-----------------+---------------+
rack_location refers to the numbered racks in the first table.
server_unit_loc is the placement of the server in the rack, 1 being the top, 43 would be the bottom. And server_height is just how tall the server is.
mysql> select * from connections;
+----+----------+----------+------------+------------+---------+---------+------------+
| id | rack_id1 | rack_id2 | server_id1 | server_id2 | port_s1 | port_s2 | cable_type |
+----+----------+----------+------------+------------+---------+---------+------------+
| 1 | 7430 | 7430 | 2 | 1 | J01 | J08 | RJ-45 |
| 2 | 7430 | 7431 | 2 | 3 | J03 | J08 | RJ-45 |
| 3 | 7430 | 7432 | 2 | 15 | J02 | J09 | SFP+ |
+----+----------+----------+------------+------------+---------+---------+------------+
rack_id's again (I realize now these are probably pointless...) server_id1 & 2 are the id's from the servers table. ports_1&2 correlate to the jack the cable is plugged in. This is an internal system we use, so J01 means it is the first jack on the server, and it plugs into the 8th(J08) port on the connecting server.
The end goal here is to be able to quickly see how many servers are connected to any chosen server and also the details about that connection info. In this example I connected the Cisco 2960 to multiple servers in different racks.
+What kind of query would I have to put in to return all the servers that are connected to the Cisco (2). I'd like to be able to specify just the server_id# and it would go to the different tables and get all the information such as which rack it's in, it's type, and the height of the server
Cisco2960 7430 A01 J01 --> Server1 7430 A02 J08 "RJ-45"
Cisco2960 7430 A01 J03 --> Server2 7431 A09 J08 "RJ-45"
Cisco2960 7430 A01 J02 --> Server3 7432 A27 J09 "SFP+"
Something like that...^
Thanks! Also if you have a better idea of how to go about accomplishing this I'd love to hear everyone's thoughts.
You would need to create query that joins the related information together.
Think of label S being (source server). Label T being (target server).
Ex.
SELECT S.server_type, S.rack_location, CONCAT('A0',S.server_unit_loc), B.port_s1,T.server_type, T.rack_location, CONCAT('A0',T.server_unit_loc), B.port_s2,B.cable_type FROM servers S
JOIN connections B ON B.server_id1 = S.id
JOIN servers T ON B.server_id2 = T.ID
WHERE S.id = 2
I agree with one of the comments. You could normalize things (rid some redundant rack information).

Mysql how to find cumulative total by group

Can anyone help me to sort this out pleaase. i have a episode table and for an episode there will be following appointments . Episode table will be like
+-------------+------------+------------+------------+----------------+------+
| Episode_id | Patientid | St_date | End_date | Status | ... |
+-------------+------------+------------+------------+----------------+------+
| 61112345 | 100001 | 12-01-2010 | | Active | |
| 61112346 | xxxxxx | 20-01-2010 | 10-10-2011 | Withdrawn | |
| ......... | xxxxxxxx | 30-01-2010 | 10-05-2011 | Lost to follow | |
| ......... | xxxxxxxx | 01-02-2011 | Active | Active | |
+-------------+------------+------------+------------+----------------+------+
Status field holds the status of each episode.A episode has 6 appointments , 3 months per appointment. so totally an episode has 18 months . some patient may complete all 6 appointment , some may withdraw in the middle, or some will be lost to follow up. i need to create a dashboard .
Appointment table will have fields for
Appointment_id
PatientId
...
Stats // Completed or pending, which is used for reporting
For example if a patient complete 2 appointment and if he is marked as Withdrawn on episdode which means that he has withdrawn from 3rd visit and active for 2 visits, if we lost to follow him on 5th app, then he will be active for 4app and then he will be added to lost to follow up on 5th visit. if he completes all then he will added to active for all 6 visits. and the report should be like
Report from 01-01-2010 to 31-12-2010
+--------+--------+-------------+----------------+---------+
| | Active | Withdrawn | Lost to follow | Revised |
+------- +--------+-------------+----------------+---------+
| visit1 | 1500 | 30 | 5 | 5 |
| Visit2 | 1800 | 20 | 4 | 3 |
| Visit3 | 1900 | 45 | 3 | 2 |
| Visit4 | 1800 | 34 | 0 | 1 |
| Visit5 | 1900 | 30 | 0 | 1 |
| Visit6 | 1200 | 20 | 0 | 5 |
+--------+--------+-------------+----------------+---------+
Currently we are fetching the query and using loop only we are generating reports like this, but it is taking time to process, is there any way i can achieve using query itself.
It isn't really clear what you want to group by, but I can give you a general answer. After your where clause you can add "group by fieldname order by fieldname" where fieldname is the element you want to count or sum. You can then count(fieldname) or sum(fieldname) to either add or count.
This may be helpful: http://www.artfulsoftware.com/infotree/qrytip.php?id=105

Find missing calendar weeks in a table

I am looking for a SQL statement that outputs missing calendar weeks based on a table. Here is a short example. We are using MySQL. I dropped all the irrelevant columns.
+------+--------------+
| Year | CalendarWeek |
+------+--------------+
| 2012 | 1 |
| 2012 | 5 |
| 2012 | 8 |
| 2012 | 9 |
| 2012 | 51 |
| 2013 | 2 |
+------+--------------+
What I am trying to get:
+------+--------------+
| Year | CalendarWeek |
+------+--------------+
| 2012 | 2 |
| 2012 | 3 |
| 2012 | 4 |
| 2012 | 6 |
| 2012 | 7 |
| 2012 | 10 |
| ... | ... |
| 2012 | 50 |
| 2012 | 52 |
| 2013 | 1 |
+------+--------------+
I added the dots to shorten the output.
Further background: The columns in each row are computed via some logic in Java. If we create a new row, lets say for the current calendar week, we have to check if we need to fill gaps. Its a simple routine. Is there a row for the previous week? Yes? Fine we are done. Otherwise compute the value for the previous week, insert it and check the week before that. The one and only do-while-loop in the whole program.
This apparently fails if we start with a table that has more than one gap. In this case we have to run through all the calendar weeks for every year and check for missing rows. Takes some time.
tl;dr I am trying to reduce roundtrips to the database with a shortcut.