use Aggregate function with multiple table in Access - mysql

I have Four tables Items,Customer,Invoice_summary,Invoice_details. Here I want to join these four tables and to get Sum(Invoice_details.Item_quntity) and Sum(Invoice_details.Price) for a specific item_code and for a specific date range . Main Columns are as follows:
Invoice_summary :Inv_num,Inv_date,Cus_id,Total
Items :Item_code,Item_name,Unit_price
Invoice_details :Inv_num,Item_code,Item_qty,Price
Customers :Cus_id,Cus_name,Route
Here is what I currently have.this return more than a row(whole itemsnames) i need only for a specific item code.Could someone explain where I am going wrong.
SELECT Invoice_Table.Item_Code, Items.Item_Name,
(Select sum(Invoice_Table.Item_Quntity) from (Invoice_Table INNER JOIN Invoice ON Invoice_Table.Inv_Num = Invoice.Inv_Num) where ((Invoice_Table.Item_Code=[?]) And Invoice.inv_date Between #3/4/2013# And #6/4/2013#) group BY Invoice_Table.Item_Code) AS Quntity,
(Select sum(Invoice_Table.Price) from (Invoice_Table INNER JOIN Invoice ON Invoice_Table.Inv_Num = Invoice.Inv_Num) where ((Invoice_Table.Item_Code=[?]) And Invoice.inv_date Between #3/4/2013# And #6/4/2013#) group BY Invoice_Table.Item_Code) AS Price
FROM Invoice_Table
INNER JOIN Items ON Invoice_Table.Item_Code = Items.Item_Code
GROUP BY Invoice_Table.Item_Code, Items.Item_Name;

The sums you're interested in should be given by this query.
select Inv_num, Item_code,
sum(Item_qty) item_code_qty,
sum(Price) item_code_price
from invoice_details
group by Inv_num, Item_code
It looks like the date range should be found in the invoice summary table. It looks like Invoice_summary and Invoice_details should be joinable on Inv_num.
select inv_s.Inv_num, inv_s.Inv_date, inv_s.Cus_id, inv_s.Total,
inv_t.item_code_qty, inv_t.item_code_price
from Invoice_summary inv_s
inner join (select Inv_num, Item_code,
sum(Item_qty) item_code_qty,
sum(Price) item_code_price
from invoice_details
group by Inv_num, Item_code
) inv_t
on inv_s.Inv_num = inv_t.Inv_num
where inv_s.Inv_date between ? and ?;
And to round that out, join the other two tables on their keys, and add some of their columns to the SELECT clause.
select inv_s.Inv_num, inv_s.Inv_date, inv_s.Cus_id, cus.Cus_name, inv_s.Total,
inv_t.item_code_qty, inv_t.item_code_price,
items.name
from Invoice_summary inv_s
inner join (select Inv_num, Item_code,
sum(Item_qty) item_code_qty,
sum(Price) item_code_price
from invoice_details
group by Inv_num, Item_code
) inv_t
on inv_s.Inv_num = inv_t.Inv_num
inner join items on items.Item_code = inv_t.Item_code
inner join Customers cus on cus.Cus_id = inv_s.Cus_id
where inv_s.Inv_date between ? and ?;

Try this..
Select ID.Item_qty,ID.Price,I.Item_code from Invoice_summary IS
INNER JOIN Invoice_details ID ON ID.Inv_num=IS.Inv_num
INNER JOIN Customers C ON C.Cus_id=IS.Inv_num
INNER JOIN Items I ON I.Item_code=ID.Item_code

Related

Is there a method of counting an attribute that is in a GROUP BY clause?

I need have created a select statement to list out all the customers that have been to multiple merchants below.
I want to create another statement to display how many of those customers have been to each merchant.
What is the optimal method of approaching this problem?
Lists out all customers that have been to multiple merchants.
WITH valentinesDayMerchant AS (
SELECT m.MerchantId, m.MerchantGroupId, m.WebsiteName
FROM Merchant m
INNER JOIN OpeningHours oh ON m.MerchantId = oh.MerchantId AND oh.DayOfWeek = 'TUE'
LEFT JOIN devices.DeviceConnectionState AS dcs ON dcs.MerchantId = oh.MerchantId
WHERE MerchantStatus = '-' AND (m.PrinterType IN ('V','O') OR dcs.State = 1 OR dcs.StateTransitionDateTime > '2023-01-23')
)
SELECT DISTINCT ul.UserLoginId, ul.FullName, ul.EmailAddress, ul.Mobile
FROM dbo.UserLogin AS ul
INNER JOIN dbo.Patron AS p ON p.UserLoginId = ul.UserLoginId
INNER JOIN valentinesDayMerchant AS m ON (m.MerchantId = ul.ReferringMerchantId OR m.MerchantId IN (SELECT pml.MerchantId FROM dbo.PatronMerchantLink AS pml WHERE pml.PatronId = p.PatronId AND ISNULL(pml.IsBanned, 0) = 0))
LEFT JOIN (
SELECT mg.MerchantGroupId, mg.MerchantGroupName, groupHost.HostName [GroupHostName]
FROM dbo.MerchantGroup AS mg
INNER JOIN dbo.Merchant AS parent ON parent.MerchantId = mg.ParentMerchantId
INNER JOIN dbo.HttpHostName AS groupHost ON groupHost.MerchantID = parent.MerchantId AND groupHost.Priority = 0
) mGroup ON mGroup.MerchantGroupId = m.MerchantGroupId
LEFT JOIN (
SELECT po.PatronId, MAX(po.OrderDateTime) [LastOrder]
FROM dbo.PatronsOrder AS po
GROUP BY po.PatronId
) orders ON orders.PatronId = p.PatronId
INNER JOIN dbo.HttpHostName AS hhn ON hhn.MerchantID = m.MerchantId AND hhn.Priority = 1
WHERE ul.UserLoginId NOT IN (1,2,100,372) AND ul.UserStatus <> 'D' AND (
ISNULL(orders.LastOrder, '2000-01-01') > '2020-01-01' OR ul.RegistrationDate > '2022-01-01'
)
GROUP BY ul.UserLoginId, ul.FullName, ul.EmailAddress, ul.Mobile
HAVING COUNT(m.MerchantId) > 1
Methods I have tried include adding the merchant name to a group by and displaying the count of the customers, however this does not work as I cannot have anything related to the Merchant in the GROUP BY, or I wouldn't be able to use HAVING clause to identify the customers that have been to multiple merchants. I have also tried selecting all the merchants and counting the distinct customers which doesn't work as it takes into account all the customers, not specifically the customers that have been to multiple merchants only.

Convert Subquery with ORDER BY in 'ON' clause to Join for optimization

I have this query:
SELECT prod.ProductID, prod.Name, prod.ProdExtID, ls.ProdServiceID
FROM Products prod
LEFT JOIN ProductServices ls ON ls.ProdServiceID=(SELECT ProdServiceID FROM
ProductServices WHERE ProductID=prod.ProductID ORDER BY Modified DESC LIMIT
1) ;
This query returns 175 rows
I want to convert this to JOIN.
I used below query:
SELECT prod.ProductID, prod.Name, prod.ProdExtID, ls1.ProductServicesID
FROM Products prod
inner join ProductServices ls on ls.ProductID=prod.ProductID
inner JOIN (SELECT ProductServicesID, ProductID, max(Modified) as Modified
FROM
ProductServices group by Modified) as ls1 ON ls.ProductServicesID =
ls1.ProductServicesID and ls.Modified = ls1.Modified and ls.ProductID =
ls1.ProductID;
which doesn't return correct result. Can I get some direction on this.
The purpose is to optimize the query. Would it be good idea to use join in place of original query.
Thanks!
Try this:
SELECT ProductID
, Name
, ProdExtID
, ProdServiceID
FROM
(
SELECT prod.ProductID
, prod.Name
, prod.ProdExtID
, ls.ProdServiceID
, RANK() OVER(PARTITION BY ls.ProdServiceID ORDER BY ls.Modified DESC) AS rnk
FROM Products prod
LEFT JOIN ProductServices ls ON ls.ProductID=prod.ProductID
) x
WHERE rnk = 1
So basically: your join needs to be on ProductID but you only want the most recent ProdServiceID, right?
try this select query please.
Here you get the serviceid which corresponds to productid and the last modiied
SELECT prod.ProductID, prod.Name, prod.ProdExtID, ls.ProdServiceID
FROM Products prod
LEft join ProductServices ls on ls.ProductID=prod.ProductID
inner JOIN (SELECT ProductID, max(Modified) as Modified
FROM
ProductServices group by ProductID) as ls1 ON ls.ProductID =
ls1.ProductID and ls.Modified = ls1.Modified;

Mysql Select unique record based on multiple columns and display only group and sum amount

Hi I am trying to query a table that conatains multiple duplicates on Code,Amount and Status How will I do this if I only one to get a result group according to the client_group name and get the sum of amount under that group
SELECT `client`.`client_group`
, FORMAT(SUM(`Data_result`.`Data_result_amount` ),2) as sum
FROM
`qwer`.`Data_result`
INNER JOIN `qwer`.`Data`
ON (`Data_result`.`Data_result_lead` = `Data`.`Data_id`)
INNER JOIN `qwer`.`Data_status`
ON (`Data_result`.`Data_result_status_id` = `Data_status`.`Data_status_id`)
INNER JOIN `qwer`.`client`
ON (`Data`.`Data_client_id` = `client`.`client_id`)
WHERE `Data_status`.`Data_status_name` IN ('PAID') AND MONTH(`Data_result`.`result_ts`) = MONTH(CURRENT_DATE())
AND YEAR(`Data_result`.`result_ts`) = YEAR(CURRENT_DATE())
GROUP BY `client`.`client_group`
Result of said query:
Table
Try to distinct before run the 'sum' check whether this solve your problem
SELECT `client_group` , FORMAT(SUM(`Data_result_amount` ),2) as sum from (
SELECT DISTINCT `client`.`client_group` , `Data_result`.`Data_result_amount`
FROM
`qwer`.`Data_result`
INNER JOIN `qwer`.`Data`
ON (`Data_result`.`Data_result_lead` = `Data`.`Data_id`)
INNER JOIN `qwer`.`Data_status`
ON (`Data_result`.`Data_result_status_id` = `Data_status`.`Data_status_id`)
INNER JOIN `qwer`.`client`
ON (`Data`.`Data_client_id` = `client`.`client_id`)
WHERE `Data_status`.`Data_status_name` IN ('PAID') AND MONTH(`Data_result`.`result_ts`) = MONTH(CURRENT_DATE())
AND YEAR(`Data_result`.`result_ts`) = YEAR(CURRENT_DATE())
) T
GROUP BY `client_group`
you can check the query here http://sqlfiddle.com/#!9/36a3f8/6

Sql 2 inner joins other data outcome

Can someone explain the following.
SELECT results.`referentie`,products.`categorie`,products.`url`,products.`inkoopPrijs`,SUM(results.`stockverschil`) AS 'stockverschil'
FROM `results` results
INNER JOIN products
ON products.`referentie` = results.`referentie`
WHERE results.`datum` BETWEEN '2017-01-11' AND '2017-01-12' AND results.`referentie`= '1'
The result is a stockverschil outcome of 200. (the right amount)
SELECT results.`referentie`,products.`categorie`,products.`url`,products.`inkoopPrijs`,sold.`winstPerProductPerDag`,SUM(results.`stockverschil`) AS 'stockverschil'
FROM `results` results
INNER JOIN products
ON products.`referentie` = results.`referentie`
INNER JOIN sold
ON sold.`referentie` = results.`referentie`
WHERE results.`datum` BETWEEN '2017-01-11' AND '2017-01-12' AND results.`referentie`= '1'
The result is a stockverschil outcome of 4800.
Am i doing something wrong with the INNER JOIN?
Seems like an issue on sold, being a one[product] to many[sold] relationship
Try this, with the appropriate aggregate function forSUM(sold.winstPerProductPerDag)
SELECT results.`referentie`,products.`categorie`,products.`url`,products.`inkoopPrijs`,s.`winstPerProductPerDag`,SUM(results.`stockverschil`) AS 'stockverschil'
FROM `results` results
INNER JOIN products
ON products.`referentie` = results.`referentie`
INNER JOIN ( select SUM(sold.`winstPerProductPerDag`) as `winstPerProductPerDag`, sold.`referentie` from sold group by sold.`referentie`) s on s.`referentie` = results.`referentie`
WHERE results.`datum` BETWEEN '2017-01-11' AND '2017-01-12' AND results.`referentie`= '1'

How to deduct in group by query in sum in access

I have query like this ::
SELECT account.AccountNumber, account.NAME, Sum(agro.price * agro.qty) AS Expr1
FROM ((account
INNER JOIN data ON account.AccountNumber = data.acno)
INNER JOIN agro ON agro.BillNo = data.BillNo)
WHERE data.db='true'
GROUP BY account.AccountNumber, account.NAME;
I want to deduct another groupby query output in to Sum(agro.price * agro.qty) this
the another group by query is SELECT Sum(rs),acno
FROM jma group by acno;
i want to deduct Sum(agro.price * agro.qty)-Sum(rs) how its work please help me solve this
If I am understanding you correctly the following query may work for you:
SELECT subQ.AccountNumber, subQ.NAME, (subQ.subSum - jmaSum.jSum) AS FinalSum
FROM
(
SELECT a.AccountNumber, a.NAME, Sum(ag.price * ag.qty) AS subSum
FROM (account AS a
INNER JOIN data AS d ON a.AccountNumber = d.acno)
INNER JOIN agro AS ag ON ag.BillNo = d.BillNo
WHERE d.db = 'true'
GROUP BY a.AccountNumber, a.NAME
) AS subQ
LEFT JOIN
(
SELECT Sum(j.rs) AS jSum, j.acno
FROM jma AS j
GROUP BY j.acno
) AS jmaSum ON subQ.AccountNumber = jmaSum.acno