MS Access Crosstab query - sum of columns reported by month - ms-access

I am putting together a Crosstab and I want a report for multiple column values (all numbers) grouped by month. Here is the SQL I used. I understand that this won't bring back the desired results. every "timex" column has a different number in it. I want a query that will return the sum of the column grouped by month.
TRANSFORM Sum(tblTimeTracking.time1+ tblTimeTracking.time2+ tblTimeTracking.time3+ tblTimeTracking.time4+ tblTimeTracking.time5+ tblTimeTracking.time6+ tblTimeTracking.time7+ tblTimeTracking.time8+ tblTimeTracking.time9+ tblTimeTracking.time10+ tblTimeTracking.time11+ tblTimeTracking.time12+ tblTimeTracking.time13+ tblTimeTracking.time14+ tblTimeTracking.time15+ tblTimeTracking.time16+ tblTimeTracking.time17+ tblTimeTracking.time18+ tblTimeTracking.time19+ tblTimeTracking.time20+ tblTimeTracking.time21+ tblTimeTracking.time22 ) AS Total
SELECT tbl_vlookup.Manager AS Manager
FROM tbl_vlookup INNER JOIN tblTimeTracking ON tbl_vlookup.[Associate Name] = tblTimeTracking.Associate
GROUP BY tbl_vlookup.Manager
PIVOT Format([Day],"yyyy-mm");
Associate Day Time 1 Time 2 Time 3 Time 4 Time 5 Time 6 Time 7
John Smith 12/1/9999 1 0 0 5.5 1 0.25 0.25

Something like this:
TRANSFORM Sum(q.Time1) AS SumOfTime1
SELECT q.Associate, q.Day
FROM (SELECT t.Associate, t.Day, t.Time1,"Time1" As TimeType
FROM tbl t
UNION ALL
SELECT t.Associate, t.Day, t.Time2,"Time2" As TimeType
FROM tbl t
UNION ALL
SELECT t.Associate, t.Day, t.Time3,"Time3" As TimeType
FROM tbl t) AS q
GROUP BY q.Associate, q.Day
PIVOT q.TimeType;
As I mentioned, you need to flatten the table. It only seems to be different types of data :)

Related

Insert unique records into table based on calculation of other table column MYSQL

I have two tables(ProductionDetails, Production), I need to populate Production based on calculated values of the ProductionDetails. ProductionDetails has the number of buckets gathered, Production needs to be populated with BINS. For example if I have 100 buckets, and I divide those by 30 that equals 3.33 bins. Now I need to create 3 BINS.
select Round( sum(ep.Buckets)/30),ep.lot,ep.crewid
from On_EmpProdDetails ep
group by ep.buckets, ep.lot,ep.crewid
Results in
Bins
Lot
Crew
3
556790186SOCC1
SOCC1
Now I want to insert 3 unique rows into the Production Table
BinID is auto increment
BinID
LOT
1
556790186SOCC1
2
556790186SOCC1
3
556790186SOCC1
You can use a recursive CTE to generate the rows:
insert into production (lot)
with recursive cte as (
select Round( sum(ep.Buckets)/30) as num_bins, ep.lot, ep.crewid, 1 as bin
from On_EmpProdDetails ep
group by ep.buckets, ep.lot,ep.crewid
union all
select num_bins, lot, crewid, bin + 1
from cte
where bin < num_bins
)
select lot
from cte;
Here is a db<>fiddle.

mySQL group numeric data together with last image from same table

I have a table with cell viability data containing both numeric data and images (dose-response curves). The table can contain multiple rows for the same compound (uniqued by Batch ID). each row has a unique ID, as well as a date field. Now I want to group the data by Batch ID and produce the average EC50 values, but show this together with the last dose-response Curve generated for each compound Batch_ID. The code below will select the first Curve encountered for a particular compound Batch_ID. How can I select the last one instead, but still show it together with average EC50? Any tips appreciated!
SELECT Batch_ID, avg(EC50), Curve FROM CELL_VIABILITY GROUP BY Batch_ID
Example data:
ID Batch_ID EC50 Curve Date
1 ABC123 6.72 blob_1 18-06-15
2 ABC123 4.74 blob_2 18-07-10
3 ABC123 8.72 blob_3 18-08-22
4 DEF456 1.95 blob_4 18-06-15
5 DEF456 1.66 blob_5 18-07-10
6 DEF456 3.06 blob_6 18-08-22
Expected outcome:
Batch_ID EC50 Curve
ABC123 6.73 blob_3
DEF456 2.22 blob_6
Remember that data is unordered set. Without defining a specific order, we cannot determine what is last, first etc.
We can use the Date column to define the Order. Latest updated entry (Maximum Date value) can be considered as "Last".
We can then use Correlated Subquery to determine the Last Curve for a specific Batch_ID.
SELECT cv1.Batch_ID,
AVG(cv1.EC50),
MAX((SELECT cv2.Curve
FROM CELL_VIABILITY AS cv2
WHERE cv2.Batch_ID = cv1.Batch_ID
ORDER BY cv2.Date DESC LIMIT 1)) AS Last_Curve
FROM CELL_VIABILITY AS cv1
GROUP BY Batch_ID
Another approach would be using a Derived Table. We can get the last Date value for every Batch_ID. Then "Self-Join" to the table using the maximum value of the Date to get the Last Curve:
SELECT
cv1.Batch_ID,
cv1.average,
cv2.Curve
FROM
(
SELECT Batch_ID,
AVG(EC50) AS average,
MAX(Date) AS last_date
FROM CELL_VIABILITY
GROUP BY Batch_ID
) AS cv1
JOIN CELL_VIABILITY AS cv2
ON cv2.Batch_ID = cv1.Batch_ID AND
cv2.Date = cv1.last_date

Comparing attributes from the same table using SQL query

I have a contents table and the entires in it are as shown in the attached figure
There are more than 100,000 entries. I want to fetch the data where the update_date for commit=0 is greater than update_date for commit=1. I also need the corresponding row for commit=1.
I tried a few things, but takes a long time to retrieve the results. What is the best SQL query I can use. I am using MySQL database.
EDIT
I have now updated the table. There is an attribute called content_id which binds the rows together.
A query like this gives me half of what I want
select a.* from contents a, contents b where
a.content_id=b.content_id and
a.update_date > b.update_date and
a.committed=0 and b.committed=1
I also want the corresponding entries from committed=1, but they should be appended at the bottom as rows and not vertically concatenated as columns.
For example, I cannot use
select * from contents a, contents b where
a.content_id=b.content_id and
a.update_date > b.update_date and
a.committed=0 and b.committed=1
because the results from 'b' are appended vertically. Also, is there a better way to write this query. This works really slow if there are many entries in the database.
I am assuming that in the above example, you only need id=2 as for content id = 1, the update_date for commit=0 is greater than update_date for commit=1 and in that case you need data for commited = 1.
I an using Oracle, so you need to find a suitable replacement for row_number() funtion in mysql.
The logic would be
Create a view on the existing table to use rownumber so it will give rownumber like below order by time desc (see if you use a nested query to do it)
ID, CONTENT_ID, COMMITED, UPDATE_DATE, ROWN
2 1 1 06-SEP-15 00:00:56 1
1 1 0 07-SEP-15 00:00:56 2
3 2 0 03-SEP-15 00:00:56 1
4 2 1 04-SEP-15 00:00:56 2
Now select only rows where where rown=1 and commited=1
This is the query in oracle. The second with query c2 will be your view.
Oracle query
with c1 (id, content_id,commited,update_date) as
(
select 1,1,0,sysdate from dual union
select 2,1,1,sysdate-1 from dual union
select 3,2,0,sysdate-4 from dual union
select 4,2,1,sysdate-3 from dual
),
c2 as
(select c1.*,row_number() over(partition by content_id order by update_date) as rown from c1)
select id,content_id,commited,update_date from c2
where rown=1 and commited=1
ID, CONTENT_ID, COMMITED, UPDATE_DATE, ROWN
Output
ID, CONTENT_ID, COMMITED, UPDATE_DATE
2 1 1 06-SEP-15 00:06:17

Sql query to sum up total of columns previously used aggregate function

From this post, enter link description here
I would like to improve the query
SELECT `BetType`,
count(`BetType`) AS COUNT,
sum(`BetAmount`) AS BetAmountTotal,
sum(`Payout`) AS PayoutTotal
FROM `betdb`
LEFT JOIN `matchdb` ON `betdb`.`MatchID` = `matchdb`.`MatchID`
WHERE `betdb`.`MatchID`=135
GROUP BY `BetType`
thanks to Sadikhasan, who helped on this query
I would like to add another row showing the totals of the columns
BetType Count BetAmount Total Payout Total
Handi 2 60000 950000
Homerun Count 4 10000 0
Total 6 70000 950000
this seems to be needing another SELECT statement but how would I put another row explicitly showing the "Total" string and getting the sum of the previously used columns with Aggregate Functions?
You can use WITH ROLLUP modifier to GROUP BY, which will give you another row with totals, but the column you group on (BetType) will show NULL for that row. But nothing stops you from using COALESCE() to replace that NULL with 'Total' string.
SELECT COALESCE(`BetType`,'Total') AS BetType,
COUNT(*) AS `Count`,
sum(BetAmount) AS BetAmountTotal,
sum(Payout) AS PayoutTotal
FROM betdb
WHERE betdb.MatchID=135
GROUP BY BetType WITH ROLLUP

Mysql single column result to multiple column result

I have a problem with a MySQL query, the problem is I have the following table:
id, rep, val dates
1 rep1 200 06/01/2014
2 rep2 300 06/01/2014
3 rep3 400 06/01/2014
4 rep4 500 06/01/2014
5 rep5 100 06/01/2014
6 rep1 200 02/06/2014
7 rep2 300 02/06/2014
8 rep3 900 02/06/2014
9 rep4 700 02/06/2014
10 rep5 600 02/06/2014
and I want a result like this:
rep 01/06/2014 02/06/2014
rep1 200 200
rep2 300 300
rep3 400 900
rep4 500 700
rep5 100 600
thank you very much!
You seem to want the most recent row for each rep. Here is an approach that often performs well:
select t.*
from table t
where not exists (select 1
from table t2
where t2.repid = t.repid and
t2.id > t.id
);
This transforms the problem to: "Get me the rows in table t where there is no other row with the same repid and a larger id." That is the same logic as getting the last one, just convoluted a bit to help the database know what to do.
For performance reasons, an index on t(repid, id) is helpful.
You seem to want the val for each of the dates.
Assuming the dates you are interested in are fixed then you can do that as follows. For output date column you check of the row matches the date for that column. If so you use the value of val , if not you just use 0. Then you sum all the resulting values, grouping by rep. I have assumed a fixed format of date.
SELECT rep, SUM(IF(dates='2014/06/01'), val, 0) AS '2014/06/01', SUM(IF(dates='2014/06/02'), val, 0) AS '2014/06/02'
FROM sometable
GROUP BY rep
Or if you just wanted the highest val for each day
SELECT rep, MAX(IF(dates='2014/06/01'), val, 0) AS '2014/06/01', MAX(IF(dates='2014/06/02'), val, 0) AS '2014/06/02'
FROM sometable
GROUP BY rep
If the number of dates is variable then not really a direct way to do it (as the number of resulting columns would vary). It would be easiest to do this manly in your calling script based on the following, giving you one row per rep / possible date with a sum of the values of val for that rep / date combination:-
SELECT rep, sub0.dates, SUM(IF(sometable.dates=sub0.dates), val, 0)
FROM sometable
CROSS JOIN
(
SELECT DISTINCT dates
FROM sometable
) sub0
GROUP BY rep, sub0.dates