i want to sum all the values in a specific date in mysql but idk what is the right syntax
CREATE TABLE `trans` (
`id` int(12) NOT NULL,
`date_sold` datetime NOT NULL,
`total` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `trans`
ADD PRIMARY KEY (`id`);
ALTER TABLE `trans`
MODIFY `id` int(12) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
id
date_sold
total
1
2021-02-23
12
2
2021-02-23
6
3
2021-02-24
32
4
2021-02-24
10
now i want to sum all the values in that specific date
ex:
id
date
total
1
2021-02-23
18
2
2021-02-24
42
is that possible?
Alternative Use of ROW_NUMBER() function. Because ROW_NUMBER() isn't supported in below v5.8. First calculate date wise total and then apply id incrementally.
-- MySQL(5.6)
SELECT (#row_number:= #row_number + 1) id
, t.date_sold, t.total
FROM (SELECT date_sold
, SUM(total) total
FROM trans
GROUP BY date_sold ) t, (SELECT #row_number := 0) x
ORDER BY t.date_sold
Please check from URL https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=5cc2305b465ac2454be5bdb1a9e8af4a
Related
I want to generate daywise report from two table
Table 1: opd
CREATE TABLE `opd` (
`id` int(50) NOT NULL,
`Date` date NOT NULL,
`time` time NOT NULL,
`opd_no` varchar(150) NOT NULL,
`patientid` varchar(150) DEFAULT NULL,
`name` varchar(150) NOT NULL,
);
Table structure
id
Date
Time
opd_no
Patient_id
name
1
2022-03-02
18:30:10
OPD/2122/1
PT01
Siba
2
2022-03-03
18:30:10
OPD/2122/2
PT02
Deba
3
2022-03-04
18:30:10
OPD/2122/3
PT03
Haris
4
2022-03-04
18:31:10
OPD/2122/4
PT04
ravish
Table 2: ipd_pn
CREATE TABLE `ipd_pn` (
`id` int(11) NOT NULL,
`ipd_no` varchar(40) DEFAULT NULL,
`patientid` varchar(170) NOT NULL,
`opd_reg_date` date DEFAULT NULL,
`time` time DEFAULT NULL,
`opd` varchar(40) DEFAULT NULL,
`date` date DEFAULT NULL,
)
Table structure
id
IPD_no
Patient_id
opd_Reg_date
time
opd_no
date
1
IPD/2122/1
PT01
2022-03-02
15:40:10
OPD/2122/1
2022-03-02
2
IPD/2122/2
PT03
2022-03-04
16:35:10
OPD/2122/3
2022-03-03
3
IPD/2122/3
PT02
2022-03-03
15:45:10
OPD/2122/2
2022-03-03
T tried to generate daywise how much opdno's generated by the below query
SELECT DATE(Date) AS date, COUNT(opdno) AS total_opd
FROM opd
WHERE (date BETWEEN '2022-02-1' AND '2022-03-28')
GROUP BY Date
Got output like below
date
total_opd
2022-03-02
1
2022-03-03
1
2022-03-04
2
But I can't generate how much ipd no's generated in this date period.
Please help.
I want a report like below
date
total_opd
total_ipd
2022-03-02
1
1
2022-03-03
1
1
2022-03-04
2
1
I tried with join query i.e
SELECT DATE(Date) AS date, COUNT(opdno) AS total_opd
FROM opd 0
INNER JOIN ipd_pn i ON o.Date = i.date
WHERE (date BETWEEN '2022-02-1' AND '2022-02-28')
GROUP BY Date
But that gives date ambiguity error.
So please try to help me how can I generate reports from both the tables.
You can try to use UNION ALL to combine two tables in a subquery then use a flag column to represent which row from opd or ipd_pn
SELECT date,
SUM(flag = 1) total_opd,
SUM(flag = 2) total_ipd
FROM (
SELECT DATE(Date) AS date, 1 flag
FROM opd
UNION ALL
SELECT Reg_date,2
FROM ipd_pn
) t1
WHERE date between '2022-02-1' and '2022-03-28'
GROUP BY Date
sqlfiddle
i have table with id that is primary key activated with 20 data inserted . and i have deleted row 15,16,17 and how can i arrange increasing order from 1 to 17
CREATE TABLE `cart` (
`id` int(255) NOT NULL,
`productname` varchar(255) NOT NULL,
`productquantity` varchar(255) NOT NULL,
`productsize` varchar(255) NOT NULL,
`productprice` varchar(255) NOT NULL
)
Determine row_number for each row, in an increasing order of id, starting from 1 in a Derived Table.
Join this Derived table with your main table, and update the id value to be equal to row_number. This will reset all the id values to start from 1 (with no gaps).
Try (works for all MySQL versions):
UPDATE your_table AS t1
JOIN
(
SELECT #row_no := #row_no + 1 AS row_num,
id
FROM your_table
JOIN (SELECT #row_no := 0) AS init
ORDER BY id
) AS dt ON dt.id = t1.id
SET t1.id = dt.row_num;
DB Fiddle DEMO
This is the definition of the "exchange" table:
CREATE TABLE `exchange` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`rank` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`volume` varchar(255) NOT NULL,
`timestamp` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=75032 DEFAULT CHARSET=utf8;
About 209 records will store every 5 minutes.
How can I get this data structure?
rank, name, [volume,...](last 144 value), timestamp
I use this query:
SELECT `volume`
FROM `exchange`
WHERE `exchange`.`name` = 'binance'
ORDER BY `timestamp` DESC
LIMIT 144
Is there better way to get the data once? Thanks.
Frankly, your method might be the best method, particularly if you have an index on (name, timestamp).
You can try:
select e.volume
from exchange e
where e.timestamp >= (select e2.timestamp
from exchange e2
where e2.name = e.name
limit 1 offset 143
);
You can then aggregate the values as:
select e.name, sum(e.volume)
from exchange e
where e.timestamp >= (select e2.timestamp
from exchange e2
where e2.name = e.name
limit 1 offset 143
)
group by e.name;
Note: In MySQL 8+, this is much simpler using row_number():
select name, sum(volume)
from (select e.*,
row_number() over (partition by name order by timestamp desc) as seqnum
from exchange e
) e
where seqnum <= 144
group by name;
I'm getting an headache of solving the following problem.
We are looking for a query where it retrieves a the data when the SUM(price_total) reached a certain level grouped by type. Table structures are as followed:
CREATE TABLE `Table1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Date` datetime DEFAULT NULL,
`type` int(11) DEFAULT NULL,
`price_total` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
Data
INSERT INTO `Table1` (`id`, `Date`, `type`, `price_total`)
VALUES
(1,'2013-02-01 00:00:00',1,5),
(2,'2013-02-01 00:00:00',2,15),
(3,'2013-02-02 00:00:00',1,25),
(4,'2013-02-03 00:00:00',3,5),
(5,'2013-02-04 00:00:00',4,15),
(6,'2013-03-05 00:00:00',1,20),
(7,'2013-08-07 00:00:00',4,15);
Example outcome for threshold 15, in chronological order.
Type 1: 2013-02-02 00:00:00 Because here it came above 15. (15+5)
Type 2: 2013-02-01 00:00:00
Type 3: n/a SUM(price_total) < 15
Type 4: 2013-02-04 00:00:00
To sum up. I want to know the date that they crossed the threshold. Price total should be summed up in chronological order.
Here's a simple, self explanatory query:
select type, min(date) as date
from (
select t1.type, t1.date, sum(t2.price_total) as total
from table1 t1
join table1 t2
on t2.type = t1.type
and t2.date <= t1.date
group by t1.type, t1.date
having total >= 15
) sub
group by type
Demo: http://rextester.com/GMAK8269
I'm trying to increase several rows order field by one while taking into account max value of that field.
CREATE TABLE IF NOT EXISTS `jobs`(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(128) NOT NULL,
`order` INT(11) NOT NULL DEFAULT 0,
PRIMARY KEY(`id`));
INSERT INTO `jobs`(`name`) VALUES("John"),("Steven"),("Marie"),("Clair"),("Richard"),("Rober"),("Barbara")
UPDATE
`jobs` AS `j1`,
(SELECT MAX(`order`) AS `max` FROM jobs) AS `j2`
SET `j1`.`order` = `j2`.`max` + 1
WHERE `j1`.`id` > 4
It sets rows of Richard, Rober and Barbara to 1 and I want to be 1,2,3 and if I would execute it again I want them to be 4,5,6
I know It would be perfect if the column order would be auto_increment / unique but it can't be in this case.
If you can use user defined variables then you can do so
UPDATE
`jobs` AS `j1`
cross join (
select #r:= (SELECT MAX(`order`) AS `max` FROM jobs)
) t
SET `j1`.`order` = #r:= #r + 1
WHERE `j1`.`id` > 4
Demo for single update
Demo for 2 times update