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
Related
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
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).
I am new to sql and have a question about joining 2 tables. Why is there a . in between customers.custnum in this example. What is its significance and what does it do?
Ex.
Select
customers.custnum, state, qty
From
customers
Inner join
sales On customers.custnum = sales.custnum
The . is to specify a column of a table.
Let's use your customer table; we could do:
SELECT c.custnum, c.state, c.qty FROM customers as c INNER JOIN
sales as s ON c.custnum = s.custnum
You don't really need the . unless two tables have columns with the same name.
In the below query, there are two tables being referred. One is CUSTOMERS another is STATE. Since both has same column CUSTNUM, we need a way to tell the database which CUSTNUM are we referring to. Same as there may be many Bob's, if so their last name is used for disambiguation.
I would consider the below style as more clearer. That's opinionated.
Select
cust.custnum, cust.state, s.qty
From
customers cust -- use alias for meaningful referencing, you may be self-joining, during that time you can use cust1, cust2 as aliases.
Inner join
sales as s On cust.custnum = s.custnum
Think of it as a way to categorize the hierarchical nature of the database. Within a DB, there are tables, and within tables there are columns. It's just a way of keeping track, especially if you are working with multiple tables that may have the same column name.
For example, a table called Sales and a table called Customers might both have a column called Date. You may be writing a query where you only want the date from the Sales table, so you would specify that by writing:
Select *
From Sales
inner join Customers on Sales.ID = Customers.ID
where Sales.Date = '1/1/2019'
Hi I’m trying to write a query and I’m struggling to figure out how to go about it.
I have a suppliers table and a supplier parts table I want to write a query that lists suppliers that have specified related Parts in the supplier parts table. If a supplier doesn’t have all specified related parts then they should not be listed.
At the moment I have written a very basic query that lists the supplier if they have a related supplier part that meets the criteria.
SELECT id ,name
FROM
efacdb.dbo.suppliers INNER JOIN [efacdb].[dbo].[spmatrix] ON
id = spmsupp
WHERE spmpart
IN ('ALUM_5083', 'ALUM_6082')
I only want to show the supplier if they have both parts related. Does anyone know how I could do this?
Use a subquery with counting distinct occurences:
select * from suppliers s
where 2 = (select count(distinct spmpart) from spmatrix
where id = spmsupp and spmpart in ('ALUM_5083', 'ALUM_6082'))
As a note, you can modify your query to get what you want, just by using an aggregation:
SELECT id, name
FROM efacdb.dbo.suppliers INNER JOIN
[efacdb].[dbo].[spmatrix]
ON id = spmsupp
WHERE spmpart IN ('ALUM_5083', 'ALUM_6082')
GROUP BY id, name
HAVING MIN(spmpart) <> MAX(spmpart);
If you know there are no duplicates, then having count(*) = 2 also solves the problem.
i hava a set of following tables
customer(cus_id,cus_name);
jointAccount(cus_id,acc_number,relationship);
account(acc_number,cus_id)
now i want to create a select statement to list all the jointAccounts,
it should included the both customer name, and relationship.
I have no idea how to retrieve both different user name, is that possible to do this?
Generally speaking, yes. I'm assuming you mean you want to get customer info for both sides of the joint account per your jointAccount table. Not sure what database you're using so this answer is assuming MySQL.
You can join on the same table twice in a single SQL query. I'm assuming you have not yet created your tables, as you have cus_id listed twice in the jointAccount table. Typically these would be something like cus_id1 and cus_id2, which I've used in my sample query below.
Example:
SELECT c1.cus_id AS cust1_id, c1.cus_name AS cust1_name
, c2.cus_id AS cust2_id, c2.cus_name AS cust2_name, j.relationship
FROM customer c1
INNER JOIN jointAccount j
ON c1.cus_id = j.cus_id1
, customer c2
INNER JOIN jointAccount j
ON c2.cus_id = j.cus_id2
I haven't tested this but that's the general idea.
try this query:
SELECT * FROM jointAccount a LEFT JOIN customer c ON a.cus_id = c.cus_id;
just replace the * with the name of the columns you need.