Join tables and accumulate values - sql

I'm a SQL rookie.
Table A contains information about my projects. Here we have a field for "ExpectedValue_amount" - meaning. How much were we expecting to earn/invoice on this project.
Table B contains our actual invoices. One project can have multiple invoices. So I want to accumulate all values on a certain project to see the difference between "ExpectedValue" and "Actual invoice value".
My query for Table A:
SELECT
name,
number,
customer_name,
expectedValue_amount,
FROM
Projects
In Table B I have projectnumber which == number in Table A. But I want to see the total on a project.
SELECT
projects_number,
totalExcludingTax_amount
FROM
Invoices
Desired output:

Maybe try to mess a little around with joining the tables but something like this may work for you
SELECT
p.name,
p.number,
p.customer_name,
p.expectedValue_amount,
SUM(i.totalExcludingTax_amount)
FROM
Projects p JOIN Invoices i ON i.projects_number = p.number
GROUP BY p.number

SELECT
p.name,
p.number,
p.customer_name,
p.expectedValue_amount,
SUM(i.totalExcludingTax_amount) as totalExcludingTax_amount
FROM
Projects p
JOIN
Invoices i
ON CAST(p.number as STRING) = i.projects_number
GROUP BY 1,2,3,4

Related

SQL Server question - subqueries in column result with a join?

I have a distinct list of part numbers from one table. It is basically a table that contains a record of all the company's part numbers. I want to add columns that will pull data from different tables but only pertaining to the part number on that row of the distinct part list.
For example: if I have part A, B, C from the unique part list I want to add columns for Purchase quantity, repair quantity, loan quantity, etc... from three totally unique tables.
So it's almost like I need 3 subqueries that will sum of that data from the different tables for each part.
Can anybody steer me in the direction of how to do this? Please and thank you so much!
One method is correlated subqueries. Something like this:
select p.*,
(select count(*)
from purchases pu
where pu.part_id = p.part_id
) as num_purchases,
(select count(*)
from repairs r
where r.part_id = p.part_id
) as num_repairs,
(select count(*)
from loans l
where l.part_id = p.part_id
) as num_loans
from parts p;
Another option is joins with aggregation before the join. Or lateral joins (which are quite similar to correlated subqueries).

Left Join on three tables in access with Group by

I have broken my head with syntax error response from Access Jet engine.
I have three tables.
First one "tblMstItem" is the master table for Item details contains two columns "colITemID" PK and "colItemName"
Second one "tblStocks" is the table where the purchases are maintained. This table has a column "colRQty" which keeps the quantity of the particular item purchased. "colItemID" FK from "tblMstItem"
Third one "tblSales" is the table where the sales are maintained. This table has a column "colSoldQty" which keeps the quantity of the particular item sold. "colItemID" FK from "tblMstItem"
Therefore "colItemID" is common in all the three tables and has links created.
My requirement is I need all the Items listed in the "tblMstItem" table columns are "colItemID" "colItemName" and if there is any item purchased or any item sold should be shown as sum of that particular item.
I have used Left Join shown in the following select statement but it always giving me an error message.
Select statement as follows:
SELECT
i.colItemID,
i.colItemName,
s.rqty,
n.soldqty
from tblMstItem i
left join
( select sum( colRQty ) as rqty from tblStocks group by colItemID ) s
on i.colItemID = s.colItemID
left join
( select sum( colSoldQty ) as soldqty from tblSales group by colItemID ) n
on i.colItemID=n.colItemID``
I tried the above given code with many different syntax but every time I get syntax error. It is making me to doubt do MS Access support three table joins, I am sure I am wrong.
See the error Message below
Table columns and table link shown below
I would be very thankful to get any help on this. Access sql please because this I am able to get results in SQL Server.
Thanks in Advance
MS Access has a picky syntax. For instance, joins need extra parentheses. So, try this:
select i.colItemID, i.colItemName,
s.rqty, n.soldqty
from (tblMstItem as i left join
(select colItemID, sum(colRQty ) as rqty
from tblStocks
group by colItemID
) as s
on i.colItemID = s.colItemID
) left join
(select colItemID, sum( colSoldQty ) as soldqty
from tblSales
group by colItemID
) as n
on i.colItemID = n.colItemID;
You also need to select colItemID in the subqueries.

Using two tables to get one report in SQL Server

I have two tables, product and download as follows.
product (product_id (pk), product_name)
download (download_date(pk), download_version(pk), product_id(pk,fk))
I need a report to show how many downloaded, form which version of what product took place in each month.
SELECT
[product_name],
[version],
MONTH(download_date) AS [Month],
COUNT(MONTH(download_date)) AS [Count]
FROM
product
INNER JOIN
download ON product.product_id = download.product_id
GROUP BY
MONTH(download_date)
and I get this error
Column 'product.product_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Use alias names for the tables for better readability.
Mention the aliasname.columnname in the SELECT to avoid fetching the wrong values.
You missed the other columns except the aggregate values in the GROUP BY.
So the query below will return the result.
SELECT P.[product_name],
P.[version],
MONTH(D.download_date) AS [Month],
COUNT(MONTH(D.download_date)) AS [Count]
FROM product P
INNER JOIN download D ON D.product_id = P.product_id
GROUP BY P.[product_name], P.[version], MONTH(D.download_date)
You have some issue with tables and primary key.
create the table like this.
product(product_id(PK),name,verion)
download(date,product_id)
and run this query
SELECT product.name,product.version,COUNT(download.product_id)
FROM product INNER JOIN download ON product.product_id=download.download_id
Group BY(download._productid);
i think this is what you want if not post replay i will answer when i get back to stack.

Sql query join?

I have one table containing car inventory. This has two columns one is car_id and the other is car_info e.g. bmw 320
The other table also contains a table with two columns. One is sales_ids and the other is car_id they have sold.
I want to create an sql query were the output will be the sales_id and the car name which was sold by that sales_id.
I have tried using a join query but have had no sucess thus far. Can anyone offer some help
select s.sales_id, i.car_info
from inventory i
join sales s on s.car_id = i.car_id;
To find cars sold for one specific sales_id:
select i.car_info
from inventory i
join sales s on s.car_id = i.car_id
where s.sales_id = 'some id';
select c.car_id,c.car_info,s.sales_id from
car_table_name c,
sales_table_name s
where c.car_id = s.car_id
Assuming table one is named car_inventory and table two car_sales the query should be this.
However for a better answer you should provide the code you try with your question.
select car_sales.sales_ids, car_inventory.car_info
from car_sales join car_inventory
on car_sales.car_id = car_inventory.car_id

distinct group by join problem

Here's what I want to achieve:
I have a number of categories, each one with products in it.
I want to produce a report that shows various information about those products for each category. So I have a query that looks something like:
select
category,
count(products),
sum(product_price),
from product
group by category
So far so good.
But now I also want to get some category-specific information from a table that has information by category. So effectively I want to say:
join category_info on category
except that that will create a join for each row of each group, rather than just one join for each group.
What I really want to be able to say to sql is 'for each group, take the distinct category value, of which there's guaranteed to only be one since I'm grouping on it, and then use that to join to the category info table'
How can I accomplish this in SQL? By the way, I'm using Oracle 10g..
Many thanks!
select a.category, a.Count, a.SumPrice
ci.OtherColumn
from (
select p.category,
count(p.products) as Count,
sum(p.product_price) as SumPrice,
from product p
group by category
) a
inner join category_info ci on a.category = ci.category