I have a query (original question that has table structure too) that is perfect, but it creates a temporary table which unfortunately takes like 12 seconds due to amount of data. (1 table has 95k records, another 155k, and another 21k).
Is there any way to go around the temporary table solution or make it run faster? Maybe suggest which fields should be indexed? I have ID fields, date fields, etc... indexed, but that's not helping at all.
SELECT
(
CASE
WHEN a.winner = a.f_a THEN "Win"
WHEN a.winner = a.f_b THEN "Loss"
WHEN a.winner IS NULL THEN a.method
END
) AS result,
SUM(a.f_a IN (d.fighter_a, d.fighter_b) AND d.winner <=> a.f_a) AS fighter_wincount,
SUM(a.f_a IN (d.fighter_a, d.fighter_b) AND d.winner IS NOT NULL AND d.winner <> a.f_a) AS fighter_losscount,
SUM(a.f_a IN (d.fighter_a, d.fighter_b) AND d.method = "Draw") AS fighter_drawcount,
SUM(a.f_a IN (d.fighter_a, d.fighter_b) AND d.method = "No Contest") AS fighter_nocontestcount,
b.name AS opponent,
SUM(a.f_b IN (d.fighter_a, d.fighter_b) AND d.winner <=> a.f_b) AS opponent_wincount,
SUM(a.f_b IN (d.fighter_a, d.fighter_b) AND d.winner IS NOT NULL AND d.winner <> a.f_b) AS opponent_losscount,
SUM(a.f_b IN (d.fighter_a, d.fighter_b) AND d.method = "Draw") AS opponent_drawcount,
SUM(a.f_b IN (d.fighter_a, d.fighter_b) AND d.method = "No Contest") AS opponent_nocontestcount,
b.fighter_id AS opponent_id,
b.fighting_out_of_country AS opponent_country,
a.method AS method,
a.method_type AS method_type,
a.round AS round,
a.time AS time,
c.event_id AS event_id,
c.event_name AS event,
c.event_date AS date,
c.event_city AS event_city,
c.event_state AS event_state,
c.event_country AS event_country
FROM
(
SELECT
fight_id,
IF(fighter_b = :fighter_id_3, fighter_b, fighter_a) AS f_a,
IF(fighter_b = :fighter_id_4, fighter_a, fighter_b) AS f_b,
winner,
method,
method_type,
round,
time,
event
FROM
fights
WHERE
:fighter_id_5 IN (fighter_a, fighter_b)
) a
INNER JOIN
fighters b ON a.f_b = b.fighter_id
INNER JOIN
events c ON a.event = c.event_id
LEFT JOIN
(
SELECT
a.fighter_a,
a.fighter_b,
a.winner,
a.method,
b.event_date
FROM
fights a
INNER JOIN
events b ON a.event = b.event_id
) d ON
(a.f_a IN (d.fighter_a, d.fighter_b) OR a.f_b IN (d.fighter_a, d.fighter_b)) AND
d.event_date <= c.event_date
GROUP BY
a.fight_id
ORDER BY
date DESC
Using a.id IN (b.a_id, b.b_id) is never going to be performant, you won't be able to create an index to fit the query. You should normalise that aspect of your structure.
I propose the following changes to your schema:
Add fight_date_time to your fights table
Move fighter_a and fighter_b out to a participicants table
fight_id, fighter_id - Normalised - Two rows per fight
The following will create two records for every fight (One for each fighter), but will also list their opponent, and both their records going in to the fight.
SELECT
event.event_name,
event.event_date,
MAX(CASE WHEN participant.fighter_id = fighter.id THEN fighter.name END) AS fighter,
MAX(CASE WHEN participant.fighter_id = fighter.id THEN record.wins END) AS wins,
MAX(CASE WHEN participant.fighter_id = fighter.id THEN record.draws END) AS draws,
MAX(CASE WHEN participant.fighter_id = fighter.id THEN record.losses END) AS losses,
CASE WHEN fight.winner = participant.fighter_id THEN 'Win'
WHEN fight.winner IS NULL THEN 'Draw'
ELSE 'Loss' END AS result,
fight.method,
MAX(CASE WHEN participant.fighter_id <> fighter.id THEN fighter.name END) AS Opp,
MAX(CASE WHEN participant.fighter_id <> fighter.id THEN record.wins END) AS Opp_wins,
MAX(CASE WHEN participant.fighter_id <> fighter.id THEN record.draws END) AS Opp_draws,
MAX(CASE WHEN participant.fighter_id <> fighter.id THEN record.losses END) AS Opp_losses
FROM
event
INNER JOIN
fight
ON event.id = fight.event_id
INNER JOIN
participant
ON participant.fight_id = fight.id
INNER JOIN
(
SELECT
participant.fighter_id,
participant.fight_id,
SUM(CASE WHEN prevFight.winner = participant.fighter_id THEN 1 ELSE 0 END) AS wins,
SUM(CASE WHEN prevFight.winner IS NULL) AS draws,
SUM(CASE WHEN prevFight.winner <> participant.fighter_id THEN 1 ELSE 0 END) AS losses
FROM
participant
INNER JOIN
fight
ON participant.fight_id = fight.id
INNER JOIN
participant AS prevParticipant
ON prevParticipant.fighter_id = participant.fighter_id
INNER JOIN
fight AS prevFight
ON prevFight.id = prevParticipant.fight_id
AND prevFight.fight_date_time < fight.fight_date_time
GROUP BY
participant.fighter_id,
participant.fight_id
)
AS record
ON record.fight_id = participant.fight_id
INNER JOIN
fighter
ON fighter.id = record.fighter_id
GROUP BY
event.id,
fight.id,
participant.fighter_id
If you want to see the results for just one fighter, add:
- WHERE participant.fighter_id = :fighter_id
Even better, keep this kind of data up to date with triggers. That way you don't need to calculate it again and again and again.
Related
I have this pseudo SQL code for what I want to achieve:
UPDATE orders o
SET o.datePaid = null
WHERE
(
SELECT SUM(amount)
FROM transactions t
WHERE t.orderId = o.id
AND t.status = 'success'
AND t.type = 'refund'
)
>=
(
SELECT SUM(amount)
FROM transactions t
WHERE t.orderId = o.id
AND t.status = 'success'
AND t.type IN ('purchase', 'capture')
)
How would I do this in SQL?
I think your approach is interesting. Here is a more concise method:
UPDATE orders o
SET o.datePaid = null
WHERE (SELECT SUM(CASE WHEN t.type = 'refund' THEN amount
WHEN t.type IN ('purchase', 'capture') THEN -amount
END)
FROM transactions t
WHERE t.orderId = o.id AND
t.status = 'success'
) > 0;
Your query works fine as is. However it can be more optimally written using MySQL multi-table UPDATE syntax:
UPDATE orders o
LEFT JOIN (SELECT orderId,
COALESCE(SUM(CASE WHEN type = 'refund' THEN amount END), 0) AS refunds,
COALESCE(SUM(CASE WHEN type IN ('purchase', 'capture') THEN amount END), 0) AS pc
FROM transactions
WHERE status = 'success'
GROUP BY orderId) t ON t.orderId = o.id
SET o.datePaid = NULL
WHERE t.refunds > t.pc
Demo on dbfiddle (includes your query working as well)
Your code would probably work just as it is. Give it a try.
You could also optimize the query to avoid the need for two subqueries by using a JOIN and conditional aggregation in a single subquery:
UPDATE orders o
INNER JOIN (
SELECT orderId
FROM transactions
WHERE
status = 'success'
AND type IN ('success', 'purchase', 'capture') -- this condition might be superfuous
GROUP BY o.id
HAVING
SUM(CASE WHEN type = 'success' THEN amount ELSE 0 END)
>= SUM(CASE WHEN type IN ('purchase', 'capture') THEN amount ELSE 0 END)
) t ON t.orderId = o.id
SET o.datePaid = null
Note: WHERE condition AND type IN ('success', 'purchase', 'capture') is superfluous if that list of 3 values represents all the possible values.
I have the following SQL:
SELECT t.teilnehmer_id, t.familienname, t.vorname, t.ort, t.ortsteil, t.kontrolle_ertrag, t.kontrolle_1j, t.kontrolle_brache,
SUM(fe.nutzflaeche) AS nutzflaeche_ertrag, GROUP_CONCAT(fe.nutzflaeche) AS einzelfl_ertrag,
SUM(fp.nutzflaeche) AS nutzflaeche_pflanzj, GROUP_CONCAT(fp.nutzflaeche) AS einzelfl_pflanzj,
SUM(fb.nutzflaeche) AS nutzflaeche_brache, GROUP_CONCAT(fb.nutzflaeche) AS einzelfl_brache,
SUM(fn.nutzflaeche) AS nutzflaeche_nicht_aush, GROUP_CONCAT(fn.nutzflaeche) AS einzelfl_nicht_aush
FROM teilnehmer t
LEFT JOIN anrede a ON (t.anrede_id = a.anrede_id)
LEFT JOIN antragsform af ON (t.antragsform_id = af.antragsform_id)
LEFT JOIN bank b ON (t.bank_id = b.bank_id)
LEFT JOIN flurverzeichnis fe ON (t.teilnehmer_id = fe.teilnehmer_id AND fe.kulturbez = 'E')
LEFT JOIN flurverzeichnis fp ON (t.teilnehmer_id = fp.teilnehmer_id AND fp.kulturbez = 'P')
LEFT JOIN flurverzeichnis fb ON (t.teilnehmer_id = fb.teilnehmer_id AND fb.kulturbez = 'B')
LEFT JOIN flurverzeichnis fn ON (t.teilnehmer_id = fn.teilnehmer_id AND fn.kulturbez = 'N')
WHERE 1 = 1
GROUP BY t.teilnehmer_id
ORDER BY familienname, vorname
The sum doesn't reflect the correct areas if there is a match in more than one kulturbez. E.g. if I have 5 rows with kulturbez 'E' and 2 rows with kulturbez 'N', each 'E' row shows up twice and each 'N' row shows up 5 times. Any suggestions on how to redo the SQL to only sum each row with the matching kulturbez once?
Thanks,
Gunter
As indicated in my comment, unavoidable 1:N joins usually need subqueries to calculate aggregate values appropriately; but it looks like your need can be solved with conditional aggregation, like so:
SELECT t.teilnehmer_id, t.familienname, t.vorname, t.ort, t.ortsteil, t.kontrolle_ertrag, t.kontrolle_1j, t.kontrolle_brache
, SUM(CASE WHEN f.kulturbez = 'E' THEN f.nutzflaeche ELSE NULL END) AS nutzflaeche_ertrag
, GROUP_CONCAT(CASE WHEN f.kulturbez = 'E' THEN f.nutzflaeche ELSE NULL END) AS einzelfl_ertrag
, SUM(CASE WHEN f.kulturbez = 'P' THEN f.nutzflaeche ELSE NULL END) AS nutzflaeche_pflanzj
, GROUP_CONCAT(CASE WHEN f.kulturbez = 'P' THEN f.nutzflaeche ELSE NULL END) AS einzelfl_pflanzj
, SUM(CASE WHEN f.kulturbez = 'B' THEN f.nutzflaeche ELSE NULL END) AS nutzflaeche_brache
, GROUP_CONCAT(CASE WHEN f.kulturbez = 'B' THEN f.nutzflaeche ELSE NULL END) AS einzelfl_brache
, SUM(CASE WHEN f.kulturbez = 'N' THEN f.nutzflaeche ELSE NULL END) AS nutzflaeche_nicht_aush
, GROUP_CONCAT(CASE WHEN f.kulturbez = 'N' THEN f.nutzflaeche ELSE NULL END) AS einzelfl_nicht_aush
FROM teilnehmer t
LEFT JOIN anrede a ON (t.anrede_id = a.anrede_id)
LEFT JOIN antragsform af ON (t.antragsform_id = af.antragsform_id)
LEFT JOIN bank b ON (t.bank_id = b.bank_id)
LEFT JOIN flurverzeichnis f ON (t.teilnehmer_id = fe.teilnehmer_id)
WHERE 1 = 1
GROUP BY t.teilnehmer_id
ORDER BY familienname, vorname
Aggregate functions ignore NULL values for the most part. (Also, technically ELSE NULL is optional, as it is the assumed value if ELSE is not specified; but is good practice to make your intent clear.)
SELECT
BB.NAME BranchName,
VI.NAME Village,
COUNT(BAC.CBSACCOUNTNUMBER) 'No.Of Accounts',
SUM(BAC.CURRENTBALANCE) SumOfAmount,
SUM(CASE
WHEN transactiontype = 'C' THEN amount
ELSE 0
END) AS CreditTotal,
SUM(CASE
WHEN transactiontype = 'D' THEN amount
ELSE 0
END) AS DebitTotal,
SUM(CASE
WHEN transactiontype = 'C' THEN amount
WHEN transactiontype = 'D' THEN - 1 * amount
ELSE 0
END) AS CurrentBalance
FROM
CUSTOMER CU,
APPLICANT AP,
ADDRESS AD,
VILLAGE VI,
BANKBRANCH BB,
BANKACCOUNT BAC
LEFT OUTER JOIN
accounttransaction ACT ON BAC.CBSACCOUNTNUMBER = ACT.BANKACCOUNT_CBSACCOUNTNUMBER
AND ACT.TRANDATE <= '2013-03-21'
AND BAC.ACCOUNTOPENINGDATE < '2013-03-21'
AND ACT.BANKACCOUNT_CBSACCOUNTNUMBER IS NOT NULL
WHERE
CU.CODE = AP.CUSTOMER_CODE
AND BAC.ENTITY = 'CUSTOMER'
AND BAC.ENTITYCODE = CU.CODE
AND AD.ENTITY = 'APPLICANT'
AND AD.ENTITYCODE = AP.CODE
AND AD.VILLAGE_CODE = VI.CODE
AND AD.STATE_CODE = VI.STATE_CODE
AND AD.DISTRICT_CODE = VI.DISTRICT_CODE
AND AD.BLOCK_CODE = VI.BLOCK_CODE
AND AD.PANCHAYAT_CODE = VI.PANCHAYAT_CODE
AND CU.BANKBRANCH_CODE = BB.CODE
AND BAC.CBSACCOUNTNUMBER IS NOT NULL
AND ACT.TRANSACTIONTYPE IS NOT NULL
GROUP BY BB.NAME , VI.NAME;
Here is my information
I have two tables bankaccount and accountransactions table
If account is created it will go to bankaccount table and if any transaction is done so respective account number record in accounttrasactiosns table however I want to display the count of total account numbers respective to the branch which the account number existed in bankaccount and it is may or may not available in accounttransactions table.
I'm guessing that the problem you have is that you are not getting results for accounts that do not have data in your accounttransaction table, even though you are using a LEFT JOIN. If that is true, the reason is because your join condition includes AND ACT.BANKACCOUNT_CBSACCOUNTNUMBER IS NOT NULL, which defeats the LEFT JOIN. You also have two conditions in your WHERE clause that I bet should not be there.
You should learn to use explicit join syntax in your coding. Your code will be much clearer if you do that; it separates the join conditions from the WHERE clause, which does a very different thing. I took a stab at re-writing your query as an illustration:
SELECT
BB.NAME BranchName,
VI.NAME Village,
COUNT(BAC.CBSACCOUNTNUMBER) 'No.Of Accounts',
SUM(BAC.CURRENTBALANCE) SumOfAmount,
SUM(ACT.CurrentBalance) CurrentBalance,
SUM(ACT.DebitTotal) DebitTotal,
SUM(ACT.CreditTotal) CreditTotal
FROM CUSTOMER CU
JOIN APPLICANT AP
ON AP.CUSTOMER_CODE = CU.CODE
JOIN ADDRESS AD
ON AD.ENTITYCODE = AP.CODE
JOIN VILLAGE VI
ON VI.CODE = AD.VILLAGE_CODE
AND VI.STATE_CODE = AD.STATE_CODE
AND VI.DISTRICT_CODE = AD.DISTRICT_CODE
AND VI.BLOCK_CODE = AD.BLOCK_CODE
AND VI.PANCHAYAT_CODE = AD.PANCHAYAT_CODE
JOIN BANKBRANCH BB
ON BB.CODE = CU.BANKBRANCH_CODE
JOIN BANKACCOUNT BAC
ON BAC.ENTITYCODE = CU.CODE
LEFT OUTER JOIN (
SELECT BANKACCOUNT_CBSACCOUNTNUMBER,
SUM(CASE
WHEN transactiontype = 'C' THEN amount
ELSE 0
END) AS CreditTotal,
SUM(CASE
WHEN transactiontype = 'D' THEN amount
ELSE 0
END) AS DebitTotal,
SUM(CASE
WHEN transactiontype = 'C' THEN amount
WHEN transactiontype = 'D' THEN - 1 * amount
ELSE 0
END) AS CurrentBalance
FROM accounttransaction
WHERE TRANDATE <= '2013-03-21'
GROUP BY BANKACCOUNT_CBSACCOUNTNUMBER
) ACT
ON ACT.BANKACCOUNT_CBSACCOUNTNUMBER = BAC.CBSACCOUNTNUMBER
AND BAC.ACCOUNTOPENINGDATE < '2013-03-21'
WHERE BAC.ENTITY = 'CUSTOMER'
AND AD.ENTITY = 'APPLICANT'
GROUP BY BB.NAME , VI.NAME;
I removed this line from the LEFT JOIN condition
AND ACT.BANKACCOUNT_CBSACCOUNTNUMBER IS NOT NULL
And I removed these two lines from the WHERE clause
AND BAC.CBSACCOUNTNUMBER IS NOT NULL
AND ACT.TRANSACTIONTYPE IS NOT NULL
If that does not solve your problem, please revise your question to explain further.
UPDATE: Based on comments, the query is revised to calculate the debit, credit, and current balance by account using a derived table.
Also note the placement of the BAC.ACCOUNTOPENINGDATE < '2013-03-21' condition on left join. As written, this will return all accounts regardless of the opening date. If you want to only show accounts that were opened before that date, this condition should be moved to the WHERE clause.
I have a query (MySQL) which pulls data from 4 tables within the same database. What I would like to do is have the query run and have the results be either updated if there was a change and have new records be inserted into a separate table in another database.
SELECT a.Created,
a.id 'TicketID',
GROUP_CONCAT((CASE WHEN d.CustomField = 1 THEN d.Content ELSE NULL END)) `CompanyName`,
a.Subject,
c.Name Queue,
b.Name 'Owner',
a.`Status`,
a.LastUpdated,
GROUP_CONCAT((CASE WHEN d.CustomField = 4 THEN d.Content ELSE NULL END)) `Location`,
a.TimeWorked 'TimeWorked',
GROUP_CONCAT((CASE WHEN d.CustomField = 2 THEN d.Content ELSE NULL END)) `OverRide`,
a.Resolved
FROM rt.Tickets a
INNER JOIN rt.Users b
ON a.owner = b.id
INNER JOIN rt.Queues c
ON a.queue = c.id
INNER JOIN rt.ObjectCustomFieldValues d
ON a.id = d.ObjectID
GROUP BY a.id
The above query is pulls data from our ticketing system.
I was able to initally insert the data using the following:
INSERT INTO Support (Created, TicketID, CompanyName, Subject, Queue, Owner, Status, LastUpdated, Location, Timeworked, OverRide, Resolved)
SELECT a.Created,
a.id 'TicketID',
GROUP_CONCAT((CASE WHEN d.CustomField = 1 THEN d.Content ELSE NULL END)) `CompanyName`,
a.Subject,
c.Name Queue,
b.Name 'Owner',
a.`Status`,
a.LastUpdated,
GROUP_CONCAT((CASE WHEN d.CustomField = 4 THEN d.Content ELSE NULL END)) `Location`,
a.TimeWorked 'TimeWorked',
GROUP_CONCAT((CASE WHEN d.CustomField = 2 THEN d.Content ELSE NULL END)) `OverRide`,
a.Resolved
FROM rt.Tickets a
INNER JOIN rt.Users b
ON a.owner = b.id
INNER JOIN rt.Queues c
ON a.queue = c.id
INNER JOIN rt.ObjectCustomFieldValues d
ON a.id = d.ObjectID
GROUP BY a.id
However when trying to update the data that is already there or adding additional new data I get errors.
UPDATE Support
SELECT a.Created,
a.id 'TicketID',
GROUP_CONCAT((CASE WHEN d.CustomField = 1 THEN d.Content ELSE NULL END)) `CompanyName`,
a.Subject,
c.Name Queue,
b.Name 'Owner',
a.`Status`,
a.LastUpdated,
GROUP_CONCAT((CASE WHEN d.CustomField = 4 THEN d.Content ELSE NULL END)) `Location`,
a.TimeWorked 'TimeWorked',
GROUP_CONCAT((CASE WHEN d.CustomField = 2 THEN d.Content ELSE NULL END)) `OverRide`,
a.Resolved
FROM rt.Tickets a
INNER JOIN rt.Users b
ON a.owner = b.id
INNER JOIN rt.Queues c
ON a.queue = c.id
INNER JOIN rt.ObjectCustomFieldValues d
ON a.id = d.ObjectID
GROUP BY a.id
Thanks,
Given that the user you are using has proper permissions in both, you can do it this way:
INSERT INTO destination_database.table_name
(SELECT source_database.source_table.field_name
FROM source_database.source_table);
In your case your destination table must have 11 fields that you are selecting, with comparable data types.
It would be possible to define a view, rather than manually manage the 'derived table' yourself. Then the database can just reflect the changes in your source tables as they are made.
See: http://www.w3schools.com/sql/sql_view.asp
SELECT p.value AS __color__,
milestone AS __group__,
milestone,
priority,
time AS created,
COUNT(t.id) as 'total open tickets',
SUM(c.value) as 'Total Dev LOE',
SUM(d.value) as 'Total QALOE'
FROM ticket t
LEFT JOIN ticket_custom c ON (t.id = c.ticket AND c.name = 'devloe')
LEFT JOIN ticket_custom d ON (t.id = d.ticket AND d.name = 'qaloe')
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
WHERE t.milestone = '$MILESTONE'
AND status <> 'closed'
GROUP BY milestone, priority, DATE(FROM_UNIXTIME(time/1000000)) DESC
Then add subquery to return the sum total devloe and qaloe for each priority.
Select p.value AS __color__
, milestone AS __group__
, milestone
, priority
, time AS created
, Count(t.id) as 'total open tickets'
, Sum( Case When c.name = 'devloe' Then c.value End ) As 'Total Dev LOE'
, Sum( Case When c.name = 'qaloe' Then c.value End ) As 'Total QALOE'
, PriorityCounts.Total_Dev_LOE As Priority_Total_QALOE
, PriorityCounts.Total_QALOE As Priority_Total_QALOE
From ticket As t
Left Join ticket_custom As c
On t.id = c.ticket
And c.name In('devloe', 'qaloe')
Left Join enum p
On p.name = t.priority
AND p.type = 'priority'
Join (
Select t1.priority,
, Sum( Case When c1.name = 'devloe' Then c1.value End ) As Total_Dev_LOE
, Sum( Case When c1.name = 'qaloe' Then c1.value End ) As Total_QALOE
From ticket As t1
Left Join ticket_custom As c1
On t1.id = c1.ticket
And c1.name In('devloe', 'qaloe')
Group By t1.priority
) As PriorityCounts
On PriorityCounts.priority = t.priority
Where t.milestone = '$MILESTONE'
And status <> 'closed'
Group By milestone, priority, DATE(FROM_UNIXTIME(time/1000000)) DESC
Doesn't answer your question, but you could simplify the query:
SELECT p.value AS __color__,
milestone AS __group__,
milestone,
priority,
time AS created,
COUNT(t.id) as 'total open tickets',
SUM(CASE WHEN c.name = 'devloe' THEN c.value ELSE 0 END) as 'Total Dev LOE',
SUM(CASE WHEN c.name = 'qaloe' THEN c.value ELSE 0 END) as 'Total QALOE'
FROM TICKET t
LEFT JOIN TICKET_CUSTOM c ON c.ticket = t.id
AND c.name IN ('devloe', 'qaloe')
LEFT JOIN ENUM p ON p.name = t.priority
AND p.type = 'priority'
WHERE t.milestone = '$MILESTONE'
AND status <> 'closed'
GROUP BY milestone, priority, DATE(FROM_UNIXTIME(time/1000000)) DESC