Connecting multiple tables with different foreign keys SQL - mysql

I am struggling in finding the solution on how to connect 5 different tables, but with different foreign keys. The table looks like that:
I need to write a query that would return all customers visit costs and payment differences (debt or overpay) and the last payment date on selected dates:
SELECT Cusomer ID, Name, Surname, SUM(Visit fee + materials price sum + service sum) - SUM(Payment amount) AS pay_diff, MAX(payment_date) WHEN DATE IS BETWEEN (LAST MONTH) AND (TODAY)
Expected Output choosing between 01-07-2021 and today:
Update: my SQL tables and db response:
customers
customer_visit
visit_services
visit_materials
customer_payments:
Query calculations:

select c.ID, c.Name, c.Surname
sum(cv.visit_fee) + sum(vm.Price_sum) + sum(vs.Service_sum) - sum(cp.Payment_amount) as pay_diff, max(payment_date)
from customer c
left join customer_visit cv
on c.ID = cv.customer_id
left join Visit_materials vm
on cv.ID = vm.Visit_ID
left join Visit_services vs
on cv.ID = vs.Visit_ID
left join Customer_payment cp
as c.ID = cp.Customer_id
where Payment_date >= '01-07-2021'
group by c.ID, c.Name, c.Surname
I noticed that the column names are inconsistent (diagram vs. table samples), you may need to adjust according to actual DB. If you got future date data, you can use between '01-07-2021' and 'xx-xx-2021' inside where clause. I hope it helps. If you get errors, can you post a screenshot of the error message?

Related

MySQL Inner join naming error?

http://sqlfiddle.com/#!9/e6effb/1
I'm trying to get a top 10 by revenue per brand for France on december.
There are 2 tables (first table has date, second table has brand and I'm trying to join them)
I get this error "FUNCTION db_9_d870e5.SUM does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual"
Is my use of Inner join there correct?
It's because you had an extra space after SUM. Please change it from
SUM (o1.total_net_revenue)to SUM(o1.total_net_revenue).
See more about it here.
Also after correcting it, your query still had more error as you were not selecting order_id on your intermediate table i2 so edited here as :
SELECT o1.order_id, o1.country, i2.brand,
SUM(o1.total_net_revenue)
FROM orders o1
INNER JOIN (
SELECT i1.brand, SUM(i1.net_revenue) AS total_net_revenue,order_id
FROM ordered_items i1
WHERE i1.country = 'France'
GROUP BY i1.brand
) i2
ON o1.order_id = i2.order_id AND o1.total_net_revenue = i2.total_net_revenue
AND o1.total_net_revenue = i2.total_net_revenue
WHERE o1.country = 'France' AND o1.created_at BETWEEN '2016-12-01' AND '2016-12-31'
GROUP BY 1,2,3
ORDER BY 4
LIMIT 10`
--EDIT stack Fan is correct that the o2.total_net_revenue exists. My confusion was because the data structure duplicated three columns between the tables, including one that was being looked for.
There were a couple errors with your SQL statement:
1. You were referencing an invalid column in your outer-select-SUM function. I believe you're actually after i2.total_net_revenue.
The table structure is terrible, the "important" columns (country, revenue, order_id) are duplicated between the two tables. I would also expect the revenue columns to share the same name, if they always have the same values in them. In the example, there's no difference between i1.net_revenue and o1.total_net_revenue.
In your inner join, you didn't reference i1.order_id, which meant that your "on" clause couldn't execute correctly.
PROTIP:
When you run into an issue like this, take all the complicated bits out of your query and get the base query working correctly first. THEN add your functions.
PROTIP:
In your GROUP BY clause, reference the actual columns, NOT the column numbers. It makes your query more robust.
This is the query I ended up with:
SELECT o1.order_id, o1.country, i2.brand,
SUM(i2.total_net_revenue) AS total_rev
FROM orders o1
INNER JOIN (
SELECT i1.order_id, i1.brand, SUM(i1.net_revenue) AS total_net_revenue
FROM ordered_items i1
WHERE i1.country = 'France'
GROUP BY i1.brand
) i2
ON o1.order_id = i2.order_id AND o1.total_net_revenue = i2.total_net_revenue
AND o1.total_net_revenue = i2.total_net_revenue
WHERE o1.country = 'France' AND o1.created_at BETWEEN '2016-12-01' AND '2016-12-31'
GROUP BY o1.order_id, o1.country, i2.brand
ORDER BY total_rev
LIMIT 10

JOIN on multiple tables giving duplicate records - MySql

Here is list of my tables and necessary columns
users u .
screen_name,
country,
status
twitter_users_relationship tf. This table have multiple target_screen_name for each screen_name.
screen_name,
target_screen_name,
target_country,
follow_status
user_twitter_action_map ta
screen_name,
action_name,
action_status
user_targeted_countries utc .This table have multiple countries for each screen_name
screen_name,
country_name
I want to get all target_screen_name from twitter_users_relationship that have matched target_country with u.country or utc.country_name
My query so far
SELECT u.screen_name,
u.country,
tf.target_screen_name,
tf.target_country,
ta.action_name,
ta.action_status,
utc.country_name
FROM users u
LEFT JOIN twitter_users_relationship tf
ON u.screen_name=tf.screen_name
LEFT JOIN user_twitter_action_map ta
ON u.screen_name=ta.screen_name
AND ta.action_name='follow'
AND ta.action_status='active'
LEFT JOIN user_targeted_countries utc
ON u.screen_name= utc.screen_name
WHERE u.status = 'active'
AND tf.follow_status = 'pending'
AND tf.target_country != ''
AND tf.target_country IS NOT NULL
AND ( utc.country_name=tf.target_country OR u.country=tf.target_country)
AND u.screen_name = 'my_screen_name';
But this query giving me duplicate record for each entry of countries in user_targeted_countries. If there are 3 counties in user_targeted_countries the it will return 3 duplicate records.
Please let me know what JOIN I need to use with user_targeted_countries to get desired results.
u.country can be different than countries in utc.country_name
UPDATE -
If I removes OR u.country=tf.target_country from the WHERE clause then I get all the matched target_screen_name without duplicate. But I am not sure how to get all those records also that matches with u.country=tf.target_country ?
Depends on the business logic required ..
First, regardless to the question, your query is wrong(Either the LEFT JOIN or the conditions) . When using LEFT JOIN , conditions on the right table should only be specified in the ON clause, which means you need to move all the conditions on tf. and utc. to the ON clause.
Secondly, you can use a GROUP BY clause and choose one of the utc.country_name (different answers will be if you want a specific one, if it doesn't matter, use MAX() on this column).

Grouping method

I am working on a query with the following format:
I require all the columns from the Database 'A', while I only require the summed amount (sum(amount)) from the Database 'B'.
SELECT A.*, sum(B.CURTRXAM) as 'Current Transaction Amt'
FROM A
LEFT JOIN C
ON A.Schedule_Number = C.Schedule_Number
LEFT JOIN B
ON A.DOCNUMBR = B.DOCNUMBR
ON A.CUSTNMBR = B.CUSTNMBR
GROUP BY A
ORDER BY A.CUSTNMBR
My question is regarding the grouping statement, database A has about 12 columns and to group by each individually is tedious, is there a cleaner way to do this such as:
GROUP BY A
I am not sure if a simpler way exists as I am new to SQL, I have previously investigated GROUPING_ID statements but thats about it.
Any help on lumped methods of grouping would be helpful
Since the docnumber is the primary key - just use the following SQL:
SELECT A.*, sum(B.CURTRXAM) as 'Current Transaction Amt'
FROM A
LEFT JOIN C
ON A.Schedule_Number = C.Schedule_Number
LEFT JOIN B
ON A.DOCNUMBR = B.DOCNUMBR
ORDER BY RM20401.CUSTNMBR
GROUP BY A.DOCNUMBR

How to minimize time in mySQL select query?

I have this query wherein I want to find out the sales for the current year and the sales for last year. I cannot make it into 2 separate queries since it has to be of the same item code. Meaning the item codes used in the sales for the current year must also be the item codes used for the sales last year.
The code below is working but it takes almost 8 to 9 minutes to fetch
select p.itemcode,
p.itemdescription,
( select round((SUM(SA.QUANTITY*P.SellingPrice)),2)
from sales s
join product p on s.itemcode=p.itemcode
where YEAR(s.date) = 2013
),
( select round((SUM(SA.QUANTITY * P.SellingPrice)),2)
from sales s
join product p on s.itemcode=p.itemcode
where YEAR(s.date) = 2012
)
from product p
join supplier s on p.suppliercode = s.suppliercode
join currency c on c.countrycode=s.countrycode
join country co on co.countrycode=c.countrycode
JOIN SALES SA ON SA.ITEMCODE=P.ITEMCODE
where c.countrycode = 'NZ'
group by p.itemcode
limit 10
Ideally the output should be
Itemcode Itemdescription SalesforCurrentYear SalesforLastYear
GS771516 BUBBLE PARTY MACHINE 1035300.00 2079300.00
GSNBC-025 X'MAS HOUSE 600612.25 1397163.25
GSNBC-031 BRANDENBURGER TOR 741010.75 1572207.25
Thanks!!
The query can be simplified by eliminating two joins:
select .......
.......
from product p
join supplier s on p.suppliercode = s.suppliercode
JOIN SALES SA ON SA.ITEMCODE=P.ITEMCODE
where s.countrycode = 'NZ'
group by p.itemcode
limit 10
Afterwards, two dependent subqueries in the select clause can be reduced to one outer join:
select p.itemcode,
p.itemdescription,
round((SUM( CASE WHEN YEAR(s.date) = 2013
THEN SA.QUANTITY*P.SellingPrice
ELSE 0 END
)),2) As Sum2013,
round((SUM( CASE WHEN YEAR(s.date) = 2012
THEN SA.QUANTITY * P.SellingPrice
ELSE 0 END
)),2) As Sum2012
from product p
join supplier s on p.suppliercode = s.suppliercode
LEFT JOIN SALES SA ON SA.ITEMCODE=P.ITEMCODE
where s.countrycode = 'NZ'
group by p.itemcode
limit 10
Please try this query and let us know how it will perform.
Follow any of these steps
1.You can parse your query.
2.Remove redundant statements.
3.Use inner join or outer join.
You've got sales three times in the same scope. I'd get rid of two of those, and that should help. Also, in terms of business logic, all of these tables seem mandatory for a sale. If that's true, you should use "inner join", for compatibility with standard SQL - even though it's the same in MySQL.

join table and filter and take aggregrate function sql access

Is it possible to join two tables,filter the data and take aggregate function in MS access? I have two tables
Inv_details:invnum,itmcde,qnt,price
Inv_summary:invnum,date,total
I need to join this tables and filter them with date range and have sum of qnt and price order by itmcde.
I tried something like this.
SELECT inv.itmcde,SUM(inv.qnt),SUM(inv.price) FROM (SELECT inv_s.invnum,inv_s.date,inv_d.itmcde,inv_d.total FROM Inv_summary as inv_s INNER JOIN Invoice_details as inv_d ON inv_d.invnum =inv_s.invNum WHERE inv_s.date BETWEEN #3/4/2013# And #5/16/2013# ) as inv ORDER BY inv.itmcde
But this is not working.
Try
SELECT d.itmcde,
SUM(d.qnt) AS qnt,
SUM(d.price) AS price
FROM Inv_summary s INNER JOIN Inv_details d
ON d.invnum = s.invnum
WHERE s.date BETWEEN #3/4/2013# AND #5/16/2013#
GROUP BY d.itmcde
ORDER BY d.itmcde
SQLFiddle Although this fiddle is for SQL Server same general rules should apply in MS Access