hi team i'm facing issue while writing join query
3rd question
https://learn.udacity.com/courses/ud198/lessons/279f3b98-a2ca-46ef-975b-e943daffc30b/concepts/e80fffa3-0f87-405e-8675-2d23a4dd51ce
I have written code for this
SELECT region.name,account.name, orders.total_amt_usd/(orders.total + 0.01) unit_price
from orders
join accounts
on orders.account_id=accounts.id
join sales_reps
on accounts.sales_rep_id=sales_reps.id
join region
on sales_reps.region_id=region.id
but the solution is :
SELECT r.name region, a.name account,
o.total_amt_usd/(o.total + 0.01) unit_price
FROM region r
JOIN sales_reps s
ON s.region_id = r.id
JOIN accounts a
ON a.sales_rep_id = s.id
JOIN orders o
ON o.account_id = a.id;
i'm always getting confuesed which query I need to execute first. plese look into this manner .......most of the time i'm facing same issue while writing joins. Please explain
Related
I have a problem with COUNT - INNER JOIN query, I tried to find a soluton, but I didn't..
Someone have idea what is the problem? Thanks on answer!
SELECT COUNT(id) AS total
FROM products
INNER JOIN `navigation_items`
ON navigation_items.id = products.navigation_item
INNER JOIN `navigation_items_group`
ON navigation_items_group.id = products.navigation_item_group
INNER JOIN `navigation_items_brand`
ON navigation_items_brand.id = products.navigation_item_brand
WHERE navigation_items.id='1' AND navigation_items_group.id='1' AND navigation_items_brand.id='1'
This is image od my database table 'products'
This is the error message what I get
this is the schema
Customer(CID,Name,City,State)
Order(OID,CID,Date)
Product(PID,ProductName,Price)
LineItem(LID,OID,PID,Number,TotalPrice),
with mostCustomers (pid, cnt) as
(
select
li.PID, count(distinct o.CID) customers
from LineItem li
inner join order o on o.OID = li.OID
group by li.PID
),
maxCustomers (customers) as (select max(customers) from mostCustomers)
select p.ProductName
from mostCustomers mc
inner join maxCustomers mx on mx customers = mc.customers
inner join product p on p.PID = mc.PID;
You should give sample data and expected output. Also you should have tagged your question with the backend you are using. Above query would work for most but not all.
Please do that next time.
I should start off saying I am using postgresql. I have four tables, the customer_orders, payments and clearinghouse_orders.
Customer_orders is one to many with payments and clearinghouse_orders.
When I use this query I the total of remits is correct:
SELECT mailers.mail_date, SUM(cho.remit) AS remits
FROM mailers
RIGHT JOIN customer_orders co
ON mailers.id = co.mailer_id
LEFT JOIN clearinghouse_orders cho
ON co.id = cho.customer_order_id
GROUP BY mailers.mail_date
The remit totals are perfect. When I add the payments table the remits increase. I suspect when I add the payments it starts counting some of the remits twice where there are two payment. Here is the code that needs correcting:
SELECT mailers.mail_date, SUM(cho.remit) AS remits, SUM(p.payment_amt) AS payments
FROM mailers
RIGHT JOIN customer_orders co
ON mailers.id = co.mailer_id
LEFT JOIN clearinghouse_orders cho
ON co.id = cho.customer_order_id
LEFT JOIN payments p
ON co.id = p.customer_order_id
GROUP BY mailers.mail_date
I am a very early beginner of SQL so please go easy on me. Any help you can offer will be greatly appreciated.
If your theory is correct, a simple sub-query to remove duplicate payment rows should fix this...
SELECT mailers.mail_date, SUM(cho.remit) AS remits, SUM(p.payment_amt) AS payments
FROM mailers
RIGHT JOIN customer_orders co
ON mailers.id = co.mailer_id
LEFT JOIN clearinghouse_orders cho
ON co.id = cho.customer_order_id
LEFT JOIN
(
select
customer_order_id,
sum(payment_amt) as payment_amt
from
payments
group by
customer_order_id
) as p
ON co.id = p.customer_order_id
GROUP BY mailers.mail_date
Sorry about the saga here but am trying to explain everything.
We have 2 databases that I would like to join some tables in.
1 database holds sales data from various different stores/sites. This database is quite large (over 3mill rows currently) This table is ItemSales
The other holds application data from an in house web app. These tables are Departments and GroupItems
I would like to create a query that joins 2 tables from the app database with the sales database table. This is so we can group some items together for a date range and see the amount sold for example.
My first attempt was (DealId being the variable that it is grouped on in the App):
SELECT d.Id, d.ItemNo, d.UnitValue, d.NoGST, d.ItemStartDate, d.ItemEndDate,
(SELECT SUM(ItemQty) AS Expr1
FROM Sales.dbo.ItemSales AS s
WHERE (Store = d.SiteId) AND (ItemNo = d.ItemNo) AND (ItemSaleDate >= d.ItemStartDate) AND (ItemSaleDate <= d.ItemEndDate)) AS ItemsSold, Sales.dbo.ItemSales.ItemDesc, Departments.Description
FROM Departments INNER JOIN
Sales.dbo.ItemSales ON Departments.Id = Sales.dbo.ItemSales.ItemDept RIGHT OUTER JOIN
GroupItems AS d ON Sales.dbo.ItemSales.ItemNo = d.ItemNo
WHERE (d.DealId = 11)
GROUP BY d.Id, d.ItemNo, d.UnitValue, d.NoGST, d.ItemStartDate, d.ItemEndDate, ItemDesc, Departments.Description, d.SiteId
ORDER BY d.Id
This does exactly what I want which is:
-Give me all the details from the GroupItems table (UnitValue, ItemStartDate, ItemEndDate etc)
-Gives me the SUM() on the ItemQty column for the amount sold (plus the description etc)
-Returns NULL for something with no sales for the period
It is VERY slow though. To the point that if the GroupItems table has more than about 7 items in it, it times out.
Second attempt has been:
SELECT d.Id, d.ItemNo, d.UnitValue, d.NoGST, d.ItemStartDate, d.ItemEndDate, SUM(ItemQty) AS ItemsSold, Sales.dbo.ItemSales.ItemDesc, Departments.Description
FROM Departments INNER JOIN
Sales.dbo.ItemSales ON Departments.Id = Sales.dbo.ItemSales.ItemDept RIGHT OUTER JOIN
GroupItems AS d ON Sales.dbo.ItemSales.ItemNo = d.ItemNo
WHERE (Store = d.SiteId) AND (d.DealId = 11) AND (Sales.dbo.ItemSales.ItemSaleDate >= d.ItemStartDate) AND (Sales.dbo.ItemSales.ItemSaleDate <= d.ItemEndDate)
GROUP BY d.Id, d.ItemNo, d.UnitValue, d.NoGST, d.ItemStartDate, d.ItemEndDate, ItemDesc, Departments.Description
ORDER BY d.Id
This is very quick and does not time out but does not return the NULLs for no sales items in the ItemSales table. This is a problem as we need to see nothing or 0 for a no sales item otherwise people will think we forgot to check that item.
Can someone help me come up with a query please that returns everything from the GroupItems table, shows the SUM() of items sold and doesn't time out? I have also tried a SELECT x WHERE EXISTS (Subquery) but this also didn't return the NULLs for me but I may have had that one wrong.
If you want everything from GroupItems regardless of the sales, use it as the base of the query and then use left outer joins from there. Something along these lines:
SELECT GroupItems.Id, GroupItems.ItemNo, GroupItems.UnitValue, GroupItems.NoGST,
GroupItems.ItemStartDate, GroupItems.ItemEndDate,
Sales.ItemDesc,
SUM(ItemQty) AS SumOfSales,
Departments.Description
FROM GroupItems
LEFT OUTER JOIN #tempSales AS Sales ON
Sales.ItemNo = GroupItems.ItemNo
AND Sales.Store = GroupItems.SiteId
AND Sales.ItemSaleDate >= GroupItems.ItemStartDate
AND Sales.ItemSaleDate <= GroupItems.ItemEndDate
LEFT OUTER JOIN Departments ON Departments.Id = Sales.ItemDept
WHERE GroupItems.DealId = 11
GROUP BY GroupItems.Id, GroupItems.ItemNo, GroupItems.UnitValue, GroupItems.NoGST,
GroupItems.ItemStartDate, GroupItems.ItemEndDate,
Sales.ItemDesc,
SUM(ItemQty) AS SumOfSales,
Departments.Description
ORDER BY GroupItems.Id
Does changing the INNER JOIN to Sales.dbo.ItemSales into a LEFT OUTER JOIN to Sales.dbo.ItemSales and changing the RIGHT OUTER JOIN to GroupItems into an INNER JOIN to GroupItems fix your issue?
I wrote a query. this query sum fields from 2 different table. And grouped by main table id field. But second left outer join is not grouped and giving me different results.
SELECT s.*,
f.firma_adi,
sum(sd.fiyat) AS konak,
sum(ss.fiyat) AS sponsor
FROM fuar_sozlesme1 s
INNER JOIN fuar_firma_2012 f
ON ( s.cari = f.cari )
LEFT OUTER JOIN fuar_sozlesme1_detay sd
ON ( sd.sozlesme_id = s.id )
LEFT OUTER JOIN fuar_sozlesme1_sponsor ss
ON ( ss.sozlesme_id = s.id )
GROUP BY s.id
ORDER BY s.id DESC
I know, it is really complicated but I'm stucking on this issue.
My question is: why second left outer join is not correctly sum of field . If I remove second left outer join or first, everything is normal.
The problem is that you have multiple dimensions on your data, and the number of rows is multiplying beyond what you expect. I would suggest that you run the query for one id, without the group by, to see what rows the join is producing.
One way to fix this is by using correlated subqueries:
select s.*, f.firma_adi,
(select SUM(sd.fiyat)
from fuar_sozlesme1_detay fd
where sd.sozlesme_id = s.id
) as konak,
(select SUM(ss.fiyat)
from fuar_sozlesme1_sponsor ss
where (ss.sozlesme_id = s.id)
) as sponsor
from fuar_sozlesme1 s inner join
fuar_firma_2012 f
on (s.cari = f.cari)
order by s.id DESC
By the way, you appear to by using MySQL (because your query is not parsable in any other dialect). You should tag your questions with the version of the database you are using.