I have a query that I need to SUM the trid column after summing the raised amount
SELECT
`ppm_campaign_id`.`meta_value` AS `campaign_id`,
`trasnlations`.`trid` AS `trid`,
`ppm_campaign_value`.`meta_value` AS `raised`
FROM (((`thf_posts` `posts`
LEFT JOIN `thf_postmeta` `ppm_campaign_id`
ON (((`ppm_campaign_id`.`meta_key` = 'campaign_id')
AND (`ppm_campaign_id`.`post_id` = `posts`.`ID`))))
LEFT JOIN `thf_postmeta` `ppm_campaign_value`
ON (((`ppm_campaign_value`.`meta_key` = 'campaign_value')
AND (`ppm_campaign_value`.`post_id` = `posts`.`ID`))))
LEFT JOIN `thf_icl_translations` `trasnlations`
ON ((`trasnlations`.`element_id` = `ppm_campaign_id`.`meta_value`)))
WHERE ((`ppm_campaign_id`.`meta_value` IS NOT NULL)
AND (`trasnlations`.`trid` IS NOT NULL))
GROUP BY `ppm_campaign_id`.`meta_value`
Result
campaign_id trid raised
1022 564 4137.5
1031 564 3937.5
1698 653 3010
1700 655 10
1702 657 750
1712 653 3030
1713 655 20
1727 657 20
2163 682 0.9
2164 682 50
2166 683 200
2168 684 50
Now I want the sum of trid combined so summing trid = 682 the result of raised should be 50.9 in both campaign_id
Expected Result
campaign_id trid raised
1022 564 8075
1031 564 8075
1698 653 3010
1700 655 10
1702 657 750
1712 653 3030
1713 655 20
1727 657 20
2163 682 50.9
2164 682 50.9
2166 683 200
2168 684 50
Considering the output of your query is stored in temptable. Please try the below query. Hope it helps.
SELECT t1.campaign_id, t1.trid, t2.raised_sum
FROM temptable t1 JOIN (
SELECT trid, SUM(raised) as raised_sum
FROM temptable GROUP BY trid) t2
ON t1.trid=t2.trid
GROUP BY t1.campaign_id, t1.trid, t2.raised_sum;
If more details on the tables included in your query and sample data can be given, we can try to get the data directly from them.
Related
I have a table named pwrDay containing electric index counters (always growing).
jour
pwrconsohp
pwrconsohc
pwrprod
pwrprodmax
2021-09-26
35 736 527
18 073 331
12 629 677
0
2021-09-27
35 754 125
18 073 331
12 637 154
0
2021-09-28
35 780 113
18 073 331
12 646 963
0
2021-09-29
35 807 081
18 073 331
12 657 084
0
2021-09-30
35 833 193
18 073 331
12 668 804
0
2021-10-01
35 861 259
18 073 331
12 682 444
0
2021-10-02
35 888 342
18 073 331
12 693 908
0
2021-10-03
35 917 218
18 073 331
12 704 696
0
2021-10-04
35 944 869
18 073 331
12 706 056
0
2021-10-05
35 972 043
18 073 331
12 708 309
0
I need to extract the difference between previous and current row (maybe create a view?) The following query works for most days, but it's wrong every first day of month (or if I miss a control day):
SELECT pwr.jour,
(pwr.pwrconsoHP-ifnull(oldpwr.pwrconsoHP, 0)) as deltaconsoHP,
(pwr.pwrconsoHC-ifnull(oldpwr.pwrconsoHC, 0)) as deltaconsoHC,
(pwr.pwrProd-ifnull(oldpwr.pwrProd, 0)) as deltaProd
FROM pwrDay pwr
LEFT OUTER JOIN pwrDay oldpwr ON
(day(pwr.jour)-day(oldpwr.jour)=1 AND MONTH(pwr.jour)=MONTH(oldpwr.jour))
ORDER BY jour;
I also tried this query:
SELECT pwr.jour,
(pwr.pwrconsoHP-LAG(pwr.pwrconsoHP, 0)) as deltaconsoHP,
(pwr.pwrconsoHC-LAG(pwr.pwrconsoHC, 0)) as deltaconsoHC,
(pwr.pwrProd-LAG(pwr.pwrProd, 0)) as deltaProd
FROM pwrDay pwr
ORDER BY jour;
However, it doesn't run at all. I get this error message:
Erreur SQL (1305) : FUNCTION velbus.LAG does not exist
How can I write this query?
SELECT pwr.jour,
(pwr.pwrconsoHP-LAG(pwr.pwrconsoHP, 0) OVER(order by jour)) as deltaconsoHP,
(pwr.pwrconsoHC-LAG(pwr.pwrconsoHC, 0) OVER(order by jour)) as deltaconsoHC,
(pwr.pwrProd-LAG(pwr.pwrProd, 0) OVER(order by jour)) as deltaProd
FROM pwrDay pwr
ORDER BY jour;
give it a try ...
Does the following MySQL code or "DENSE_RANK()" function works in MySQL or is it only used in Oracle database ???
Select Employee, Cost_Center, Cost_Grant, Percent
,DENSE_RANK() over (PARTITION BY Employee order by Percent ASC) as Rank
Employee
Cost_Center
Cost_Grant
Percent
AB61526
10030
54
AB61526
14020
46
AB60020
1040
68
AB60020
10010
32
AB60038
11000
71
AB60038
10010
29
AK50051
10020
23
AK50051
11520
78
Expected results output:
Employee
Cost_Center
Cost_Grant
Percent
Rank
AB61526
10030
54
1
AB61526
14020
46
2
AB60020
1040
68
2
AB60020
10010
32
1
AB60038
11000
71
2
AB60038
10010
29
1
AK50051
10020
23
1
AK50051
11520
78
2
DENSE_RANK is supported in mysql beginning with version 8.0, and in MariaDB beginning with version 10.2.
Hello I am trying to select data from the already selected values.
Current query:
select t.ticket_num, t.item_id, t.trans_id
from tickets as t
inner join ticket_items as ti
on ti.id = t.item_id
where ti.online = 1
and ti.bundle = 5
and ti.type_id = 2
Data sample:
num | item | tran
5699 1328 766
5700 1328 766
5701 1328 766
5702 1328 766
5703 1328 766
5704 1330 767
5705 1330 767
5706 1330 767
5707 1330 767
5708 1330 767
5709 1332 768
5710 1332 768
5711 1332 768
5712 1332 768
5713 1332 768
5714 1333 768
5715 1333 768
5716 1333 768
5717 1333 768
5718 1333 768
5719 1334 768
5720 1334 768
5721 1334 768
5722 1334 768
5723 1334 768
5724 1336 769
5725 1336 769
5726 1336 769
5727 1336 769
5728 1336 769
5729 1338 770
5730 1338 770
5731 1338 770
5732 1338 770
5733 1338 770
5734 1339 770
5735 1339 770
5736 1339 770
5737 1339 770
5738 1339 770
5739 1340 770
5740 1340 770
5741 1340 770
5742 1340 770
From this data I want only the ones that have 15 or more items on tran, as you can see 766 has only 5 but 768 has 15 so I want everything from this data that has more than or equal to 15.
Thanks
Perform a subquery that looks up all groups of tran in your desired query that have 15 or more records (the key is using the GROUP BY and HAVING clauses). Then in your main query select all records where tran is found IN the subquery. So for instance:
select
t.ticket_num, t.item_id, t.trans_id
from tickets as t
inner join ticket_items as ti
on ti.id = t.item_id
where ti.online = 1
and ti.bundle = 5
and ti.type_id = 2
and trans_id in
(select t.trans_id
from tickets as t
inner join ticket_items as ti
on ti.id = t.item_id
where ti.online = 1
and ti.bundle = 5
and ti.type_id = 2
GROUP BY t.trans_id
HAVING count(t.trans_id)>=15
)
You could probably clean up the redundant JOIN by pulling it out into a Common Table Expression like so:
WITH tquery as (SELECT *
from tickets
inner join ticket_items as ti
on ti.id = t.item_id
where ti.online = 1
and ti.bundle = 5
and ti.type_id = 2
)
select
ticket_num, item_id, trans_id
from tquery
where trans_id in
(select t.trans_id
from tquery as t
GROUP BY t.trans_id
HAVING count(t.trans_id)>=15
)
More about the having clause in case you're wondering how it works: What's the difference between HAVING and WHERE?
I have a table containing answer data answered by a unique user. So I have this query.
select ta.user_id, count(ta.answer_id) count_answer_id
from survey_answers ta
group by ta.user_id
having count(ta.answer_id) < 124
order by count_answer_id desc;
userid count of answers
3702 123
2866 120
5483 120
565 120
1292 120
621 119
2250 119
2719 119
4192 119
5539 119
354 119
1441 119
2501 115
1636 115
866 109
53 108
3091 107
329 106
285 105
997 104
1352 103
5281 103
430 102
2125 102
But I would like to get the results like this.
user count answer count
1 123
4 120
7 119
2 115
.
.
.
.
select count(*) as user_count, count_answer_id
from
(
select ta.user_id, count(ta.answer_id) count_answer_id
from survey_answers ta
group by ta.user_id
having count(ta.answer_id) < 124
) tmp
group by count_answer_id
order by count_answer_id desc
I need to get GROUPed BY values,
but I need to see not a random price value (when I select price), but latest price value (price for a row with highest ID within this GROUP)
SELECT ID, price,
ROUND(AVG(price)),
MIN(price),
MAX(price),
ROUND((AVG(price)-MIN(price))/AVG(price) * 100) as differenceinprices
FROM `m-orbitzone`
WHERE dep = 'MOW'
AND returnornot = 1
GROUP BY arv, date1, date2
ORDER BY differenceinprices DESC LIMIT 1000
ID price <-- ROUND(AVG(price)) MIN(price) MAX(price) differenceinprices
122841 834 816 534 834 35
122708 783 790 524 821 34
122754 766 796 529 815 34
28528 810 766 512 810 33
28529 799 765 512 799 33
122603 766 798 534 841 33
122848 766 794 529 810 33
122589 778 765 519 778 32
122591 778 768 519 778 32
122749 766 775 529 814 32
28527 752 749 512 773 32
122744 766 773 529 814 32
122843 766 771 529 802 31
Need 'price' to be latest price for this GROUP (row with highest ID)
May be need to do SELECT and then to do one more SELECT from result?
Thank you!
This should do the trick:
SELECT m.ID, price,
ROUND(AVG(price)),
MIN(price),
MAX(price),
ROUND((AVG(price)-MIN(price))/AVG(price) * 100) as differenceinprices
FROM `m-orbitzone` m
INNER JOIN (
SELECT
ID
FROM
`m-orbitzone` m
WHERE ID = (SELECT MAX(ID) FROM `m-orbitzone` sm WHERE m.arv = sm.arv AND m.date1 = sm.date1 AND m.date2 = sm.date2)
) s ON m.ID = s.ID
WHERE dep = 'MOW'
AND returnornot = 1
GROUP BY arv, date1, date2
ORDER BY differenceinprices DESC LIMIT 1000
A good read about the topic is this manual entry.