MySql Insert Into Select Sum - mysql

TABLE ONE DATA - po_header
PO_ID PROJ_ID SUP_ID TOT_SUM SUBM_DATE
32 5555 AccAYOU 99.00 2016-11-29
33 5555 AccAYOU 990.00 2016-11-29
34 25412 AccAYOU 248778595.08 2016-11-30
TABLE TWO DATA - po_details
PO_ID amount
32 110.00
33 1500000.00
34 565079266.00
34 1.00
How can I Run the following SQL ?
INSERT INTO po_header (TOT_SUM) VALUES (SELECT SUM(amount)
FROM po_details WHERE PO_ID = '34') WHERE PO_ID ='34';

Even it's un correct syntax but this is what you want:
UPDATE po_header h
SET TOT_SUM = (Select sum(amount) From po_details d Where d.po_id = h.po_id)

UPDATE po_header SET TOT_SUM = (SELECT SUM(amount) FROM po_details WHERE PO_ID='34') WHERE PO_ID='34';

Related

self join providing wrong answers

Hypothetical data - tbl1:
orderID
SupplierID
Status
Reason
Created At
29
1
22-01-2021 22:08
29
2
22-01-2021 22:10
29
265
3
23-01-2021 06:25
29
2
sometext
23-01-2021 12:25
29
1605
3
24-01-2021 10:21
29
1605
4
anothertext
24-01-2021 11:03
29
324
3
26-01-2021 06:43
29
2
sometext
26-01-2021 12:43
29
1564
3
26-01-2021 16:09
Desired result:
orderID
SupplierID
Status
Reason
Created At
29
265
3
23-01-2021 06:25
29
324
3
26-01-2021 06:43
My query -
select distinct tbl1.orderID, tbl1.created_at, tbl2.supplierID
from tblxyz as tbl1 left join tblxyz as tbl2
on tbl1.orderID = tbl2.orderID
where tbl1.status=2 and tbl1.reason='sometext' and tbl2.status=3 and tbl1.created_at < (tbl2.created_at + INTERVAL 1 DAY)
group by tbl2.supplierID
I am unable to figure out where is my query wrong.
You can try to use LAG window function to get previous status and reason, then do your judgment.
Schema (MySQL v8.0)
CREATE TABLE tblxyz(
orderID int,
SupplierID INT,
Status INT,
Reason VARCHAR(50),
CreatedAt DATETIME
);
INSERT INTO tblxyz VALUES (29,NULL, 1,'','2021-01-22 22:08');
INSERT INTO tblxyz VALUES (29,NULL, 2,'','2021-01-22 22:10');
INSERT INTO tblxyz VALUES (29,265 , 3,'','2021-01-23 06:25');
INSERT INTO tblxyz VALUES (29,NULL, 2,'sometext','2021-01-23 12:25');
INSERT INTO tblxyz VALUES (29,1605, 3,'','2021-01-24 10:21');
INSERT INTO tblxyz VALUES (29,1605, 4,'anothertext','2021-01-24 11:03');
INSERT INTO tblxyz VALUES (29,324 , 3,'','2021-01-26 06:43');
INSERT INTO tblxyz VALUES (29,NULL, 2,'sometext','2021-01-26 12:43');
INSERT INTO tblxyz VALUES (29,1564, 3,'','2021-01-26 16:09');
Query #1
SELECT t1.orderID,t1.SupplierID,t1.Status,t1.Reason,t1.PreviewCreatedAt
FROM (
select *,
LAG(Status) OVER(PARTITION BY orderID ORDER BY CreatedAt) PreviewStatus,
LAG(Reason) OVER(PARTITION BY orderID ORDER BY CreatedAt) PreviewReason,
LAG(CreatedAt) OVER(PARTITION BY orderID ORDER BY CreatedAt) PreviewCreatedAt
from tblxyz
) t1
WHERE PreviewStatus = 2 AND Status = 3 AND PreviewReason='sometext';
orderID
SupplierID
Status
Reason
PreviewCreatedAt
29
1605
3
2021-01-23 12:25:00
29
1564
3
2021-01-26 12:43:00
View on DB Fiddle
Do you need in this:
SELECT t2.*
FROM tbl1 t1
JOIN tbl1 t2 USING (orderID)
WHERE t1.Status = 2
AND t2.Status = 3
AND t1.Reason = 'sometext'
AND t2.Created_At BETWEEN t1.Created_At - INTERVAL 1 DAY AND t1.Created_At
ORDER BY t1.Created_At;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=543d9150d1b23a01df01e0223f3fb3f2

Mysql sum column according to another column

This is my table data from my Mysql Table
t_Id t_Type t_Date t_acd_Id t_acc_Id t_Amount t_Desc t_u_Id c_Id
------ ------ ---------- -------- -------- -------- ------ ------ --------
1 0 2016-01-26 266 29 400.00 1 1
2 0 2016-01-27 266 29 160.00 1 1
3 1 2016-01-28 29 266 83.30 1 1
4 2 2016-01-27 29 272 400.00 1 1
5 0 2016-01-27 266 272 300.00 1 1
6 1 2016-01-28 272 22 20.00 1 1
I Want my result like
accout_Id rec_Amount pay_Amount
------ ---------- ----------
29 483.30 560.00
where rec_Amount is sum of t_acd_Id and pay_Amount is sum of t_acc_Id
How to get this result?
My current query
SELECT
(SELECT SUM(t_Amount) FROM tbl_transactions WHEREt_acd_Id= 29) AS rec_Amount,
(SELECT SUM(t_Amount) FROM tbl_transactions WHEREt_acc_Id= 29) AS pay_Amount
FROM tbl_transactions
which gives multiple rows
This query only serves the above requirement (for single account). If you want to get result for all the accounts then you need to group the records by account.
Try this(It's based on your requirement):
SELECT CASE
WHEN t_acc_id = 29 THEN t_acc_id
WHEN t_acd_id = 29 THEN t_acd_id
END account_id,
Sum(CASE
WHEN t_acd_id = 29 THEN t_amount
ELSE 0
END) rec_Amount,
Sum(CASE
WHEN t_acc_id = 29 THEN t_amount
ELSE 0
END) pay_Amount
FROM tbl_transactions
WHERE t_acc_id = 29
OR t_acd_id = 29
can you try with this query? as i have checked it manually.
SELECT t4.t_acd_Id as accout_Id ,sum(t4.t_Amount) as rec_Amount, (SELECT SUM(t_Amount) from table4 WHERE t_acc_Id =t4.t_acd_Id) as pay_Amount FROM `table4` as t4 WHERE t4.t_acd_Id IN (29,266) GROUP BY t4.t_acd_Id
Thanks
The following query takes into account the possibility that a given idea might appear only in t_acd_Id or t_acc_Id, but not both. In this case, we would like to do a full outer join, but MySQL does not directly support this. Instead, the first subquery in my answer obtains all unique ID values. This is then LEFT JOINed to two other subqueries for each of the totals you want.
SELECT t1.accout_Id, t2.rec_Amount, t3.pay_Amount
FROM
(
SELECT DISTINCT t_acd_Id AS accout_Id FROM tbl_transactions
UNION
SELECT DISTINCT t_acc_Id AS accout_Id FROM tbl_transactions
) t1
LEFT JOIN
(
SELECT t_acd_Id AS accout_Id, SUM(t_acd_Id) AS rec_Amount
FROM tbl_transactions
GROUP BY t_acd_Id
) t2
ON t1.accout_Id = t2.accout_Id
LEFT JOIN
(
SELECT t_acc_Id AS accout_Id, SUM(t_acc_Id) AS pay_Amount
FROM tbl_transactions
GROUP BY t_acc_Id
) t3
ON t1.accout_Id = t3.accout_Id
Click the link below for a running demo:
SQLFiddle

MySQL :: group by between price AND two dates

I have a table like following columns:
id date price pid
---------------------------
1 2015-02-01 34 2
2 2015-02-02 34 2
3 2015-02-03 34 2
4 2015-02-04 78 2
5 2015-02-05 78 2
6 2015-02-06 78 2
7 2015-02-07 52 2
8 2015-02-08 52 2
9 2015-02-09 52 2
10 2015-02-10 34 2
11 2015-02-11 34 2
12 2015-02-12 34 2
Now I want following result:
date_from date_to price pid
-------------------------------------
2015-02-01 2015-02-03 34 2
2015-02-04 2015-02-06 78 2
2015-02-07 2015-02-09 52 2
2015-02-10 2015-02-12 34 2
IMPORTANT:
I don't want to group the price "34" in this case.
One solution i can think of using user defined variables also i assume the id part is set to auto_increment
select min(t1.date) date_from,
max(t1.date) date_to,
t1.price,
t1.pid
from (
select t.*,
#r:= case when #g = price then #r else #r + 1 end r,
#g:= price g
from test t
cross join (select #g:=null,#r:=0) t
order by id
) t1
group by t1.r
DEMO
Borrowing M Khalid Junaid's fiddle...
SELECT a.date date_from
, MIN(c.date) date_to
, price
, pid
FROM test a
LEFT
JOIN test b
ON b.pid = a.pid
AND b.price = a.price
AND b.id = a.id - 1
LEFT
JOIN test c
ON c.pid = a.pid
AND c.price = a.price
AND c.id >= a.id
LEFT
JOIN test d
ON d.pid = a.pid
AND d.price = a.price
AND d.id = c.id + 1
WHERE b.id IS NULL
AND c.id IS NOT NULL
AND d.id IS NULL
GROUP
BY a.id, a.price, a.pid;
http://sqlfiddle.com/#!2/478f9/6
Try below query, it will group by three records on basis of date and price
SELECT min(created) AS date_from, max(created) AS date_to, price, pid
FROM t1
GROUP BY price, floor(DATEDIFF("2015-02-01", created) / 3);
Try this in MySQL. Here app_date is Date in your question:
set #rownum=1;
select min(app_date),max(app_date),price,pid from
(select t.id1 id,t.app_date app_date,t.price1 price,t.pid pid,
case when (t.price1=t.price2 or t.price2 is null) then #rownum else #rownum:=#rownum+1 end temp_num from
(select a.id id1,b.id id2,a.app_date app_date,a.price price1,b.price price2,a.pid pid from
test a left outer join test b on a.id=b.id+1) t
order by id1) temp
group by price,pid,temp_num
order by min(app_date);

MySQL Query suggestion with access id

I have a MySQL table like this
ownerlisting_access_id property_id mainaccess_id subaccess_id access_value
62 2 35 41 Yes
64 2 35 36 Yes
123 4 35 41 Yes
125 4 35 36 Yes
306 7 35 41 Yes
307 7 35 42 Yes
308 7 35 36 Yes
I want a query that will give me this output using subaccess_id(41,42,36) and mainaccess_id(35) -
ownerlisting_access_id property_id mainaccess_id subaccess_id access_value
306 7 35 41 Yes
307 7 35 42 Yes
308 7 35 36 Yes
I need to get the property_id as 7 using sub access id with 41, 42, 36
The fastest way to get an answer to your question is to describe the PROBLEM not just show results you need. It's not clear what is the logic behind your desired output. I guess you need the rows with the highest property_id for each group subaccess_id. If so here is the query:
select * from t
join (select subaccess_id, max(property_id) MAX_property_id
from t
where mainaccess_id=35
and
subaccess_id in (41,42,36)
group by subaccess_id
) t1
on t.subaccess_id=t1.subaccess_id
and
t.property_id=t1.MAX_property_id
SQLFiddle demo
Also here is a query that outputs results you needed :) But I guess it doesn't solve your PROBLEM:
select * from t where property_id=7
Try this:
SELECT table1.* FROM (
select property_id, group_concat(DISTINCT subaccess_id ORDER BY subaccess_id) as list
from table1 as t1 group by property_id
) a, table1
WHERE a.property_id = table1.property_id
AND a.list = '36,41,42'
Working query: http://sqlfiddle.com/#!2/4744ea/2
SELECT MAX(DISTINCT property_ID) AS property_ID, mainaccess_id,
MAX(DISTINCT subaccess_id) AS subaccess_id, MAX(DISTINCT access_value)
FROM tableName GROUP BY mainaccess_id ORDER BY mainaccess_id
SELECT property_ID, mainaccess_id, subaccess_id, access_value
FROM tableName t1
WHERE t1.mainaccess_id = 35
AND (t1.subaccess_id = 41 OR t1.subaccess_id = 42 OR t1.subaccess_id = 36)
AND t1.property_ID = (SELECT MAX(t2.property_ID)
FROM tableName t2
WHERE t2.mainaccess_id = 35
AND (t2.subaccess_id = 41 OR t2.subaccess_id = 42 OR t2.subaccess_id = 36))

How to get date_of_highest_points

I have still problem with select date_of_highest_points:
$qry1 = mysql_query("INSERT INTO bbdnes_hraci (nick, sumfrags, sumpoints,
sumhours, lastdate1, highest_points, date_of_highest_points)
SELECT DISTINCT nick, SUM(frags), SUM(points), SUM(hours),
MAX(lastdate), MAX(points), ??????
FROM hraci
GROUP BY nick ");
I asked about that here https://stackoverflow.com/questions/18817900/select-in-select , but i have not solved it, despite council. Can somebody help me with code?
TABLE: hraci
nick frags points hours lastdate
Gamer1 20 100 1 2013-09-17 22:16:08
Gamer1 30 150 3 2013-09-18 20:17:15
Gamer1 25 125 0.5 2013-09-18 23:16:06
Gamer2 50 250 4 2013-09-17 21:11:30
Gamer2 5 25 2 2013-09-17 23:13:59
Need get:
TABLE: bbdnes_hraci
nick sumfrags sumpoints sumhours lastdate1 highest_points date_of_highest_points
Gamer1 75 375 4.5 2013-09-18 23:16:06 150 2013-09-18 20:17:15 ??
Gamer2 55 275 6 2013-09-17 23:13:59 250 2013-09-17 21:11:30 ??
You can do something like this:
select nick, SUM(frags) sumfrags, SUM(points) sumpoints, SUM([hours]) sumhours, max(lastdate) lastdate, max(points) highest_points
into #t1
from hraci
group by Nick
select t1.*, x.lastdate date_of_highest_points
from #t1 t1
outer apply
(select top 1 lastdate
from hraci t
where t.Nick = t1.Nick and t.points = t1.highest_points
order by t.lastdate desc) x
try this
select * from table_name where highest_points = (select max(highest_points) from table_name)
Untested:
SELECT DISTINCT nick, SUM(frags), SUM(points), SUM(hours),
MAX(lastdate), MAX(highest_points),
(
SELECT lastdate As date_of_highest_points
FROM hraci
WHERE points =(SELECT MAX(points) FROM hraci)
)
(rest of query. i.e. FROM and ORDER By's)