Explain performance schema count_star - mysql

Can you please explain MySQL 8
performance_schema events_statements_summary_by_digest count_start
?
+------------+----------------+
| COUNT_STAR | SUM_TIMER_WAIT |
+------------+----------------+
| 21562 | 1422617337000 |
| 5134 | 231538954000 |
| 5134 | 42625981791000 |
| 184 | 6224664000 |
| 65 | 67034144000 |
| 48 | 80661283000 |
| 39 | 25631638000 |
| 32 | 1131746000 |
| 32 | 1206939000 |
| 32 | 5997462000 |
| 20 | 2349761000 |
| 8 | 284036000 |
| 8 | 2976134000 |
| 7 | 1254130000 |
| 6 | 1792915000 |
| 6 | 784386000 |
| 6 | 821875000 |
| 6 | 1102248000 |
| 6 | 1079227000 |
| 5 | 11042289000 |
| 5 | 4207319000 |
| 5 | 4012671000 |
| 5 | 6844824000 |
+------------+----------------+
I found this in documentation https://dev.mysql.com/doc/mysql-perfschema-excerpt/8.0/en/wait-summary-tables.html but it's hard to understand for me.
COUNT_STAR
The number of summarized events. This value includes all events, whether timed or nontimed.
Thanks!

Related

Query the differences between records with same ID

I have a table like this in MS Access 2019:
+-----------+------------+--------+----------+-------+
| BillingID | Date | RoomID | Electric | Water |
+-----------+------------+--------+----------+-------+
| 1 | 12/23/2018 | 4 | 1669 | 106 |
| 2 | 12/26/2018 | 1 | 5035 | 289 |
| 3 | 12/27/2018 | 6 | 0 | 0 |
| 4 | 12/31/2018 | 5 | 3158 | 223 |
| 5 | 1/6/2019 | 2 | 3823 | 194 |
| 6 | 1/15/2019 | 3 | 1772 | 125 |
| 7 | 1/23/2019 | 4 | 1796 | 117 |
| 8 | 1/26/2019 | 1 | 5231 | 299 |
| 9 | 1/27/2019 | 6 | 0 | 0 |
| 10 | 1/31/2019 | 5 | 3366 | 242 |
| 11 | 2/14/2019 | 2 | 3975 | 201 |
| 12 | 2/15/2019 | 3 | 1839 | 129 |
+-----------+------------+--------+----------+-------+
I could calculate the electricity and water usage with Index & Match in MS Excel. However, I've had a lot of trouble to achieve this with MS Access. The result I want is as below:
+-----------+------------+--------+----------+---------------+-------+------------+
| BillingID | Date | RoomID | Electric | ElectricUsage | Water | WaterUsage |
+-----------+------------+--------+----------+---------------+-------+------------+
| 1 | 12/23/2018 | 4 | 1669 | | 106 | |
| 2 | 12/26/2018 | 1 | 5035 | | 289 | |
| 3 | 12/27/2018 | 6 | 0 | | 0 | |
| 4 | 12/31/2018 | 5 | 3158 | | 223 | |
| 5 | 1/6/2019 | 2 | 3823 | | 194 | |
| 6 | 1/15/2019 | 3 | 1772 | | 125 | |
| 7 | 1/23/2019 | 4 | 1796 | 127 | 117 | 11 |
| 8 | 1/26/2019 | 1 | 5231 | 196 | 299 | 10 |
| 9 | 1/27/2019 | 6 | 0 | | 0 | |
| 10 | 1/31/2019 | 5 | 3366 | 208 | 242 | 19 |
| 11 | 2/14/2019 | 2 | 3975 | 152 | 201 | 7 |
| 12 | 2/15/2019 | 3 | 1839 | 67 | 129 | 4 |
+-----------+------------+--------+----------+---------------+-------+------------+
For example, for RoomID = 4, the ElectricUsage is the difference between the Electric in BillingID #7 and BillingID #1 and so on.
I've tried some answer like this or this but Access ran into errors when using those solutions in SQL view (Syntax error in FROM clause).
Thanks.
You can use a couple of sub-queries to return the Electric/Water for each room on the previous date:
SELECT
B.BillingID, B.BillingDate, B.RoomID, B.Electric,
B.Electric-(SELECT TOP 1 E.Electric FROM tblBilling AS E WHERE B.RoomID=E.RoomID AND E.BillingDate<B.BillingDate ORDER BY E.BillingDate DESC) AS ElectricUsage,
B.Water,
B.Water-(SELECT TOP 1 W.Water FROM tblBilling AS W WHERE B.RoomID=W.RoomID AND W.BillingDate<B.BillingDate ORDER BY W.BillingDate DESC) AS WaterUsage
FROM tblBilling AS B
Note that I've renamed your Date field to be BillingDate, as Date is a reserved word in Access, and will cause you problems in the future.
Regards,

Transpose rows to columsn in SQL

I would like to transpose the rows to columns in sql.
My Table looks like this:
+-------+--------+--------------+---------+--------------+---------+----------------+---------+---------+---------+---------+---------+
| ID | Desk | Reason1 | Amount1 | Reason2 | Amount2 | Reason3 | Amount3 | Reason4 | Amount4 | Reason5 | Amount5 |
+-------+--------+--------------+---------+--------------+---------+----------------+---------+---------+---------+---------+---------+
| 34850 | Desk1 | nktp | 2 | sectors | 1 | auc | 1 | thr | -13 | other | -3 |
| 34851 | Desk2 | TOC Reb | 5 | SG & HK ETF | 5 | | 0 | | 0 | | 0 |
| 34853 | Desk3 | China | -5 | HK | 0 | CNH | 0 | HK2 | 35 | | 0 |
| 34854 | Desk4 | ETFs | 2 | KSTA Opening | 6 | KSTA Rebalance | 14 | | 0 | | 0 |
| 34855 | Desk5 | BTC | 5 | | 0 | | 0 | | 0 | | 0 |
| 34856 | Desk6 | Sales | 10 | Delta | 5 | | 0 | | 0 | | 0 |
| 34857 | Desk7 | ES | 1 | HSI | 0 | | 0 | | 0 | | 0 |
| 34858 | Desk8 | OTC | 10 | SPREADS | 10 | | 0 | | 0 | | 0 |
| 34859 | Desk9 | MES/ZTW | 10 | O/N Spreads | -20 | | 0 | | 0 | | 0 |
| 34860 | Desk10 | CBBC TENCENT | 4 | CBBC HSI | 1 | | 0 | | 0 | | 0 |
+-------+--------+--------------+---------+--------------+---------+----------------+---------+---------+---------+---------+---------+
How do I transpose the table in SQL where the reasons are the rows and the desk are columns?
Output wanted:
+
----------------+---------+--------+-------------+--------+-------+--------+
| | Desk1 | Amount | Desk2 | Amount | Desk3 | Amount |
+----------------+---------+--------+-------------+--------+-------+--------+
| Reason1 | nktp | 2 | TOC Reb | 5 | China | -5 |
| Reason2 | sectors | 1 | SG & HK ETF | 5 | HK | 0 |
| Reason3 | auc | 1 | | | CNH | 0 |
| Reason4 | thr | -13 | | | HK2 | 35 |
| Reason5 | other | -3 | | | | |
| General_Remark | | | | | | |
+----------------+---------+--------+-------------+--------+-------+--------+
A normalized design might look something like this:
reasons
+-----------+---------+----------------+--------+
| reason_id | desk_id | reason | amount |
+-----------+---------+----------------+--------+
| 1 | 34850 | nktp | 2 |
| 2 | 34851 | TOC Reb | 5 |
| 3 | 34853 | China | -5 |
| 4 | 34854 | ETFs | 2 |
| 5 | 34855 | BTC | 5 |
| 6 | 34856 | Sales | 10 |
| 7 | 34857 | ES | 1 |
| 8 | 34858 | OTC | 10 |
| 9 | 34859 | MES/ZTW | 10 |
| 10 | 34860 | CBBC TENCENT | 4 |
| 11 | 34850 | sectors | 1 |
| 12 | 34851 | SG & HK ETF | 5 |
| 13 | 34853 | HK | 0 |
| 14 | 34854 | KSTA Opening | 6 |
| 15 | 34856 | Delta | 5 |
| 16 | 34857 | HSI | 0 |
| 17 | 34858 | SPREADS | 10 |
| 18 | 34859 | O/N Spreads | -20 |
| 19 | 34860 | CBBC HSI | 1 |
| 20 | 34850 | auc | 1 |
| 21 | 34853 | CNH | 0 |
| 22 | 34854 | KSTA Rebalance | 14 |
| 23 | 34850 | thr | -13 |
| 24 | 34853 | HK2 | 35 |
| 25 | 34850 | other | -3 |
+-----------+---------+----------------+--------+
desks
+---------+------------+
| desk_id | Desk_name |
+---------+------------+
| 34850 | Desk1 |
| 34851 | Desk2 |
| 34853 | Desk3 |
| 34854 | Desk4 |
| 34855 | Desk5 |
| 34856 | Desk6 |
| 34857 | Desk7 |
| 34858 | Desk8 |
| 34859 | Desk9 |
| 34860 | Desk10 |
+---------+------------+
If it was me, I'd start from here.

Join two tables on nearest matching string

I have a query_table Table and wants to join with match_table Table with nearest matching string. If it was vice-versa then 'like' would have worked but have no idea how to do this.
query_table
+----+------------------+
| id | string |
+----+------------------+
| 1 | fcc9e8796feb |
| 2 | fcdbd7ebcf89 |
| 3 | fccc87896feb |
| 4 | fcc7c7896fef |
| 5 | fcced777aaaf |
+----+------------------+
match_table
+----+-----------+
| id | match_code|
+----+-----------+
| 1 | fcff |
| 2 | fcccc |
| 3 | fccc8 |
| 4 | fccc9 |
| 5 | fccdb |
| 6 | fccdc |
| 7 | fccd8 |
| 8 | fcce |
| 9 | fcced |
| 10 | fccee |
| 11 | fcce6 |
| 12 | fcc7b |
| 13 | fcc7c |
| 14 | fcc8e |
| 15 | fcc87 |
| 16 | fcc88 |
| 17 | fcc9e |
| 18 | fcdbb |
| 19 | fcdbc |
| 20 | fcdbd |
+----+-----------+
I expect
result
+----+------------------+----+----------------+
| id | string | id | match_code |
+----+------------------+----+----------------
| 1 | fcc9e8796feb | 17 | fcc9e |
| 2 | fcdbd7ebcf89 | 20 | fcdbd |
| 3 | fccc87896feb | 3 | fccc8 |
| 4 | fcc7c7896fef | 13 | fcc7c |
| 5 | fcced777aaaf | 9 | fcced |
+----+------------------+----+----------------+

not able to fetch date value using subqueries mysql

i have two tables called
1 table smartpos.pos_order_Id
+---------+--------------+---------+--------+--------------+----------------+------------+
| orderId | restaurantId | tableId | closed | customerName | customerNumber | dateorderd |
+---------+--------------+---------+--------+--------------+----------------+------------+
| 7 | 14 | 0 | yes | | | 21/03/2018 |
| 8 | 14 | 0 | yes | | | 21/03/2018 |
| 9 | 14 | 0 | no | | | 20/03/2018 |
| 10 | 14 | 0 | yes | soumya | 1234567890 | 21/03/2018 |
| 11 | 14 | 0 | yes | | | 21/03/2018 |
| 12 | 14 | 0 | yes | | | 21/03/2018 |
| 13 | 14 | 0 | yes | | | 21/03/2018 |
| 14 | 14 | 0 | yes | | | 20/03/2018 |
| 15 | 14 | 0 | no | | | 22/03/2018 |
+---------+--------------+---------+--------+--------------+----------------+------------+
2smartpos.pos_invoice
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| invoiceNumber | orderId | totalAmt | discountAmt | totalTaxAmt | grandTotal | paymentmode | paymentrefNum |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| 1 | 7 | 200 | 34 | 46 | 212 | Cash | |
| 2 | 10 | 1200 | 200 | 280 | 1280 | Cash | |
| 3 | 1 | 720 | 34 | 120 | 806 | Cash | |
| 4 | 12 | 240 | 34 | 58 | 264 | Cash | |
| 5 | 13 | 330 | 32 | 83 | 381 | Cash | |
| 6 | 14 | 80 | 2 | 22 | 100 | Cash | |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
i want to fetch invoice details using restaurantId and two dates by providing restaurantId and dates as follows
select inv.invoiceNumber ,inv.totalAmt,inv.discountAmt,inv.totalTaxAmt,inv.grandTotal,i.dateorderd from smartpos.pos_invoice inv,smartpos.pos_order_Id i where inv.invoiceNumber in (select invv.invoiceNumber from smartpos.pos_invoice invv where invv.orderId in(select ii.orderId from smartpos.pos_order_Id ii where ii.closed='yes' and ii.restaurantId=14 and STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y'))) group by inv.invoiceNumber ;
out put:
+---------------+----------+-------------+-------------+------------+------------+
| invoiceNumber | totalAmt | discountAmt | totalTaxAmt | grandTotal | dateorderd |
+---------------+----------+-------------+-------------+------------+------------+
| 1 | 200 | 34 | 46 | 212 | NULL |
| 2 | 1200 | 200 | 280 | 1280 | NULL |
| 4 | 240 | 34 | 58 | 264 | NULL |
| 5 | 330 | 32 | 83 | 381 | NULL |
| 6 | 80 | 2 | 22 | 100 | NULL |
+---------------+----------+-------------+-------------+------------+------------+
but when i run above query it gives null values , how to fetch the date as well?
Its difficult to understand what you wrote but i think you query is much simple than it seems, try to use this approach :
select
inv.invoiceNumber ,
inv.totalAmt,
inv.discountAmt,
inv.totalTaxAmt,
inv.grandTotal,
i.dateorderd
from
smartpos.pos_invoice inv,
smartpos.pos_order_Id i
where
inv.orderId = i.orderId
and
i.closed='yes'
and
i.restaurantId=14
and
STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y')
group by
inv.invoiceNumber;

Mysql rollup issue when grouping with quarter and date

I have this simple query:
select quarter(invitation_date) as `iQuarter`, invitation_date as `iDate`,count(*) as count from data group by iQuarter,iDate with rollup
I would expect to get a table like this:
+----------+------------+-------+
| iQuarter | iDate | count |
+----------+------------+-------+
| 1 | 2010-02-23 | 133 |
| 1 | 2010-03-02 | 14 |
| 1 | 2010-03-09 | 74 |
| 1 | 2010-03-15 | 102 |
| 1 | 2010-03-22 | 76 |
| 1 | 2010-03-30 | 16 |
| NULL | NULL | 415 |
| 2 | 2010-04-06 | 20 |
| 2 | 2010-04-12 | 84 |
| 2 | 2010-04-19 | 85 |
| 2 | 2010-04-26 | 51 |
| 2 | 2010-05-03 | 73 |
| 2 | 2010-05-11 | 76 |
| 2 | 2010-05-18 | 54 |
| 2 | 2010-05-24 | 83 |
| 2 | 2010-06-01 | 73 |
| 2 | 2010-06-07 | 74 |
| 2 | 2010-06-24 | 81 |
| 2 | 2010-06-28 | 110 |
| NULL | NULL | 864 |
+----------+------------+-------+
But what I get has the total in the wrong position and before every quarters total we have a row with the next quarter with a count of one.
+----------+------------+-------+
| iQuarter | iDate | count |
+----------+------------+-------+
| 1 | 2010-02-23 | 1 |
| NULL | NULL | 1 |
| 1 | 2010-02-23 | 132 |
| 1 | 2010-03-02 | 14 |
| 1 | 2010-03-09 | 74 |
| 1 | 2010-03-15 | 102 |
| 1 | 2010-03-22 | 76 |
| 1 | 2010-03-30 | 16 |
| 2 | 2010-04-06 | 1 |
| NULL | NULL | 415 |
| 2 | 2010-04-06 | 19 |
| 2 | 2010-04-12 | 84 |
| 2 | 2010-04-19 | 85 |
| 2 | 2010-04-26 | 51 |
| 2 | 2010-05-03 | 73 |
| 2 | 2010-05-11 | 76 |
| 2 | 2010-05-18 | 54 |
| 2 | 2010-05-24 | 83 |
| 2 | 2010-06-01 | 73 |
| 2 | 2010-06-07 | 74 |
| 2 | 2010-06-24 | 81 |
| 2 | 2010-06-28 | 110 |
| 3 | 2010-07-06 | 1 |
| NULL | NULL | 864 |
+----------+------------+-------+
Invitation date is defined as:
+-------------------+---------------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------------+------+-----+---------------------+-------+
| invitation_date | date | NO | | | |
+-------------------+---------------------------+------+-----+---------------------+-------+
Anyone have a clue on why this is happening? Mysql version: 5.0.27