Getting repetition of same values multiple time while combining two queries - mysql

I write two queries in mysql workbench at the time of executing the hole query i am getting the same value. what should i do?
This is my query bellow
Select *
from
(
(select division.name,
date(meter_data.reading_date),
sum(meter_data.meter_delta) 'Gas Consumption (m3)'
from meter, meter_data, division
where meter.meter_id = meter_data.meter_id
and meter.division_id = division.division_id
and Date(meter_data.reading_date) between '2018-04-01' and '2018-05-03'
group by date(meter_data.reading_date)
) as table1,
(Select Date(receipt.receipt_date),
sum(case receipt_item.item_name
when 'Gas' then receipt_item.item_amount else 0
end) 'Gas Purchase'
from receipt_item, receipt
where receipt.receipt_id = receipt_item.receipt_id
and Date(receipt.receipt_date) between '2018-04-01' and '2018-05-03'
group by Date(receipt.receipt_date)
) as table2
);
while executing the queries separately each query returns the exact 33rows but combinedly it returns 1089 rwos
for example i am getting this here all the repetition of same data
but need to show like that (this one is demo image)

I have some problem with my queries the queries should like that beloow
Select *
from
(
(select division.name,
date(meter_data.reading_date) AS reading_date,
sum(meter_data.meter_delta) 'Gas Consumption (m3)'
from meter, meter_data, division
where meter.meter_id = meter_data.meter_id
and meter.division_id = division.division_id
and Date(meter_data.reading_date) between '2018-04-01' and '2018-05-03'
group by date(meter_data.reading_date)
) as table1,
(Select Date(receipt.receipt_date) AS receipt_date,
sum(case receipt_item.item_name
when 'Gas' then receipt_item.item_amount else 0
end) 'Gas Purchase'
from receipt_item, receipt
where receipt.receipt_id = receipt_item.receipt_id
and Date(receipt.receipt_date) between '2018-04-01' and '2018-05-03'
group by Date(receipt.receipt_date)
) as table2
)
WHERE table1.reading_date = table2.receipt_date;
this solve my problem

Related

SQL how to create aggregate of two aggregated columns for each row

I'm working on a big query in SQL (MySQL 5.7) to calculate aggregated columns based on raw values in my table. I've created several aggregated columns (see attached screenshot and SQL) and I now need to create a conversion_percent column for each tlp_aff_id in my query.
This conversion_percent should be a division of the aggregated JoinedSessions.total_sessions and COUNT(Report.tlp_aff_id) as leads_total.
My current SQL:
SELECT
# Application details
Report.tlp_aff_id,
# Revenue
JoinedRevenue.total_revenue,
# Commission
JoinedCommission.total_commission,
# Profit
JoinedProfit.total_profit,
# Sessions
JoinedSessions.total_sessions,
# Submits
COUNT(Report.tlp_aff_id) as total_submits,
# Leads
COUNT(Report.tlp_aff_id) as leads_total,
SUM(case when Report.application_result = 'Accepted' then 1 else 0 end) leads_accepted,
SUM(case when Report.application_result = 'Rejected' then 1 else 0 end) leads_rejected
# Conversion percent
# JoinedConversion.conversion_percent
FROM
tlp_payout_report_minute AS Report
INNER JOIN
(
SELECT
JoinedRevenue.tlp_aff_id,
JoinedRevenue.minute_rounded_timestamp,
SUM(commission) AS total_revenue
FROM
tlp_payout_report_minute AS JoinedRevenue
WHERE
JoinedRevenue.minute_rounded_timestamp >= 1664841600
AND
JoinedRevenue.minute_rounded_timestamp <= 1664927999
GROUP BY
JoinedRevenue.tlp_aff_id
) AS JoinedRevenue
ON JoinedRevenue.tlp_aff_id = Report.tlp_aff_id
INNER JOIN
(
SELECT
ReportCommission.tlp_aff_id,
ReportCommission.seller_code,
ReportCommission.minute_rounded_timestamp,
SUM(commission) AS total_commission
FROM
tlp_payout_report_minute AS ReportCommission
WHERE
ReportCommission.minute_rounded_timestamp >= 1664841600
AND
ReportCommission.minute_rounded_timestamp <= 1664927999
AND
ReportCommission.seller_code != 44
GROUP BY
ReportCommission.tlp_aff_id
) AS JoinedCommission
ON JoinedCommission.tlp_aff_id = Report.tlp_aff_id
INNER JOIN
(
SELECT
ReportProfit.tlp_aff_id,
ReportProfit.seller_code,
ReportProfit.application_result,
ReportProfit.minute_rounded_timestamp,
SUM(commission) AS total_profit
FROM
tlp_payout_report_minute AS ReportProfit
WHERE
ReportProfit.minute_rounded_timestamp >= 1664841600
AND
ReportProfit.minute_rounded_timestamp <= 1664927999
AND
ReportProfit.application_result = 'Accepted'
AND
ReportProfit.seller_code = 44
GROUP BY
ReportProfit.tlp_aff_id
) AS JoinedProfit
ON JoinedProfit.tlp_aff_id = Report.tlp_aff_id
INNER JOIN
(
SELECT
Conversion.aff_id,
Conversion.conversion_type,
COUNT(Conversion.ip_address) as total_sessions
FROM
tlp_conversions AS Conversion
WHERE
Conversion.conversion_time >= '2022-10-04 00:00:00'
AND
Conversion.conversion_time <= '2022-10-04 23:59:59'
AND
Conversion.aff_id IS NOT NULL
AND
Conversion.conversion_type = 2
GROUP BY
Conversion.aff_id
) AS JoinedSessions
ON JoinedSessions.aff_id = Report.tlp_aff_id
WHERE
Report.minute_rounded_timestamp >= 1664841600
AND
Report.minute_rounded_timestamp <= 1664927999
GROUP BY
Report.tlp_aff_id
ORDER BY
JoinedRevenue.total_revenue DESC
I'm thinking something along the lines of:
INNER JOIN
(
...
) AS JoinedConversion
ON JoinedConversion.aff_id = Report.tlp_aff_id
But I don't think this is necessary for conversion_percent.
What's the right approach here?

mysql Rollup sums to 0 on some columns

I have a Rollup query in mysql to create the weekly report and i want to sum up the numbers in the last row:
SELECT case when ISNULL(Datum) then 'Summe' ELSE Datum end AS Datum,
`Anzahl angenommen`,
`unvollständig`,
KDA,
Freigabe
FROM(
SELECT F.eindat AS Datum,
COUNT(F.eindat) AS 'Anzahl angenommen',
COUNT(T.blocker) AS 'unvollständig',
case when B.KDA IS NULL then 0 ELSE B.KDA END AS KDA,
case when P.Freigabe IS NULL then 0 ELSE P.Freigabe END AS Freigabe
FROM mukl.fall F
left JOIN mukl.ticket T ON T.fall = F.ID
LEFT JOIN (SELECT F.beadat AS Datum, COUNT(F.beadat) AS KDA
FROM mukl.fall F
WHERE F.eindat >= '2021-08-07'
GROUP BY F.beadat) B ON B.Datum = F.eindat
LEFT JOIN (SELECT F.prudat AS Datum, COUNT(F.prudat) AS Freigabe
FROM mukl.fall F
WHERE F.eindat >= '2021-08-07'
GROUP BY F.prudat) P ON P.Datum = F.eindat
WHERE F.eindat >= '2021-08-07'
GROUP BY F.eindat WITH rollup
) AS DT
Sadly the output is only partly what i want:
The first two columns are summed up correctly, the last two just display as 0, although the sum is not 0. Is there a way to fix this?
please try with this pseudocode
-- MySQL (v5.8)
SELECT CASE WHEN datum IS NULL THEN 'sum' ELSE datum END dat
, SUM(a) a, SUM(b) b
, SUM(c) c, SUM(d) d
FROM test
GROUP BY datum WITH ROLLUP;
Please check from url https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=19fdd64ce99647ce751004b1580766d1
In your query use aggregate function of all columns except GROUP BY columns
SELECT F.eindat AS Datum,
COUNT(F.eindat) AS 'Anzahl angenommen',
COUNT(T.blocker) AS 'unvollständig',
SUM(case when B.KDA IS NULL then 0 ELSE B.KDA END) AS KDA,
SUM(case when P.Freigabe IS NULL then 0 ELSE P.Freigabe END) AS Freigabe

How do I calculate the difference of two alias for sorting

Considering the following code:
SELECT SUM(w.valor),
SUM(CASE WHEN w.tipo = '+' THEN w.valor ELSE 0 END) AS total_credit,
SUM(CASE WHEN w.tipo = '-' THEN w.valor ELSE 0 END) AS total_debit,
w.clientUNIQUE,
c.client as cclient
FROM wallet AS w
LEFT JOIN clients AS c ON w.clientUNIQUE = c.clientUNIQUE
WHERE w.status='V'
GROUP BY w.clientUNIQUE
ORDER BY total_credit-total_debit
I'm trying to calculate the difference of two aliased calculated values for sorting purposes, but I'm getting the following error:
Reference 'total_credit' not supported (reference to group function)
What am I doing wrong and how can I order results by using the difference value between the two aliases?
You can't refer to columns by their alias in the same select expression, so there are 2 options...
Repeat the expressions in the order by (yuk):
ORDER BY
SUM(CASE WHEN w.tipo = '+' THEN w.valor ELSE 0 END) AS total_credit -
SUM(CASE WHEN w.tipo = '-' THEN w.valor ELSE 0 END) AS total_debit
Or easier on the brain and easier to maintain (DRY), order via a sub query:
select * from (
<your query without the ORDER BY>
) q
ORDER BY total_credit - total_debit

How to do a SELECT for total from beginning until the specified date in MySQL?

I have entry table:
I need to do a SELECT to receive 'Date', 'Number of entries' (in that date), 'Total number of entries until that date'.
When I do the SELECT:
SELECT e1.*,
(select count(*) from entry where date(dateCreated) <= e1.date) as Total
from (
SELECT
DATE(e.dateCreated) as "Date",
count(e.dateCreated) as "No of Entries",
sum( case when e.premium='Y' then 1 else 0 end ) as Premium,
sum( case when e.free='Y' then 1 else 0 end ) as Free,
sum( case when e.affiliateID IS NOT NULL then 1 else 0 end) as Affiliate
FROM entry e
WHERE e.competitionID=166
GROUP BY DATE(e.dateCreated)
) as e1
ORDER BY Date DESC
I've got a result table
but the column 'Total' has a wrong data.
How the correct select should be? Is this logic of select is the best and more efficient one?
Here is a demo
If it is just the 5 vs 7 that is off I think it is because that subquery in your select list, which accesses the inline view e1 (which is filtered to competitionID = 166), is not itself filtered when also utilizing the original entry table (unfiltered). You have to filter the original table to that competitionID as well.
Notice line 3 in sql below (only change)
SELECT e1.*,
(select count(*) from entry where date(dateCreated) <= e1.date
and competitionID=166) as Total
from (
SELECT
DATE(e.dateCreated) as "Date",
count(e.dateCreated) as "No of Entries",
sum( case when e.premium='Y' then 1 else 0 end ) as Premium,
sum( case when e.free='Y' then 1 else 0 end ) as Free,
sum( case when e.affiliateID IS NOT NULL then 1 else 0 end) as Affiliate
FROM entry e
WHERE e.competitionID=166
GROUP BY DATE(e.dateCreated)
) as e1
ORDER BY Date DESC
Fiddle - http://sqlfiddle.com/#!9/e5e88/22/0

SQL statement for GROUP BY

I am really stucked with one sql select statement.
This is output/result which I get from sql statement below:
WHAT I need: I need to have columns assignedVouchersNumber and usedVouchersNumber together in one row by msisdn. So for example if you can see "msisdn" 723709656 there are two rows now.. one with assignedVouchersNumber = 1 and second with assignedVouchersNumber = 1 too.
But I need to have it in one row with assignedVouchersNumber = 2. Do you now where is the problem?
SELECT eu.msisdn,
eu.id as userId,
sum(case ev.voucherstate when '1' then 1 else 0 end) as assignedVouchersNumber,
sum(case ev.voucherstate when '2' then 1 else 0 end) as usedVouchersNumber,
ev.extra_offer_id,
ev.create_time,
ev.use_time,
ev.id as voucherId,
ev.voucherstate
FROM extra_users eu
JOIN (SELECT sn.msisdn AS telcislo,
stn.numberid
FROM stats_number sn
JOIN stats_target_number AS stn
ON ( sn.numberid = stn.numberid )
WHERE stn.targetid = 1) xy
ON eu.msisdn = xy.telcislo
JOIN extra_vouchers AS ev
ON ( eu.id = ev.extra_user_id )
WHERE ev.create_time BETWEEN '2012-07-23 00:00:00' AND '2013-08-23 23:59:59'
AND ev.use_time <= '2013-08-23 23:59:59'
AND ev.use_time >= '2012-07-23 00:00:00'
AND ev.voucherstate IN ( 1, 2 )
AND Ifnull(ev.extra_offer_id IN( 2335, 3195, 30538 ), 1)
GROUP BY eu.msisdn, ev.extra_offer_id, ev.voucherState
ORDER BY eu.msisdn ASC
You have two different extra_offer_id for same msisdn and VouchersNumber. Thats why you get two rows.
I got it... there should not be groupping by ev.voucherState in
GROUP BY eu.msisdn, ev.extra_offer_id, ev.voucherState
After then I have removed ev.voucherState it is working now.