Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have multiple tables that I want to pull data from to make an item list
Below are the columns I want to capture in my list
item.code, item.description, itemtype.description, subcategory.description, UOM.description
The tables and columns are listed below
Items table - This is the main source of information
The columns I want to display are
item.code, item.description,
Itemtype - Items is linked to item type by Item_type_ID
The column I want to display is
itemtype.description
Subcategories - Items is linked to the subcategories by subcateg_id
The column I want to display is
subcategory.description
Units of measure Items is link to the UOM by uom.id
The column I want to display is
uom.description
Any help would be greatly appreciated. I have been playing with JOINS with varying results.
SELECT i.code,
i.description,
it.description,
sc.description,
um.description
FROM items AS i
INNER JOIN itemtype AS it
ON i.item_type_id = it.id
INNER JOIN subcategory AS sc
ON i.subcateg_id = sc.id
INNER JOIN uom AS um
ON i.uom_id = um.id
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am just starting to learn SQL. Can someone explain what ON means in the example?
SELECT title, imdb_score
FROM films
JOIN reviews
ON films.id = reviews.film_id
WHERE title = 'To Kill a Mockingbird';
ON films.id = reviews.film_id
To specify JOIN condition. With ON clause you specify exactly on which column you want to match both table records. You can as well specify extra conditions like
ON films.id = reviews.film_id
AND <more condition>
'ON' shows the connection between tables. For your example, the main table is 'films' and you want to bring data from table 'reviews'. If you dont tell computer how to connect data it cant know which movie has which reviews. You are saying that id's are the same for both of this table when you use 'ON'
It's a join condition.
In your example you will get title and imdb_score from those rows from films and reviews, which have the same id and film_id.
https://www.geeksforgeeks.org/sql-on-clause/
I would add for better readability, the ON right next to the table name you want to join
SELECT title, imdb_score
FROM films
JOIN reviews ON films.id = reviews.film_id
WHERE title = 'To Kill a Mockingbird';
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm trying to get an extract of data using two tables in Sql. I have an AddressBook table and a companies table. The AddressBook table has a foreign key called companyid which is the primary key in the companies table. The companies table has a column called accountno. How do I lookup all the addresses on the AddressBook table and find the accountno in the companies table using the companyId?
Please let me know if you need any more info
Use the JOIN, i think you want left join. With left join you fetch the companies even if they dont have an adress, but i see you have an inner join tag so i will include that.
left join:
SELECT * FROM companies LEFT JOIN adressbook ON adressbook.companyid = companies.id
inner join:
SELECT * FROM companies INNER JOIN adressbook ON adressbook.companyid = companies.id
select *
from companies
inner join adressbook on adressbook.companyid = companies.id
if i read it correctly this is what you are looking for
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 2 tables:
OrderDetail
OrderMaster
Both have a column named SalesOrder.
OrderDetail table has multiple rows per unique SalesOrder.
OrderMaster table has one row per unique SalesOrder.
OrderDetail has a column named LineType.
OrderMaster has a column named OrderStatus.
I want to select all records from OrderDetail that have a LineType of "1" AND whose matching SalesOrder line in the OrderMaster table has a OrderStatus column value of "4".
In plain English, orders with a Status 4 are open and ready to ship, LineType value of 1 means the Detail Line is a product code.
How should this query be structured? It's going into VS 2008 (VB).
I can give you some sql:
SELECT d.*
FROM OrderDetails d
INNER JOIN OrderMaster m ON m.SalesOrder = d.SalesOrder
WHERE d.LineType = 1 and m.OrderType = 4
How you'll use that from VB.Net depends on a number of things that weren't included with your question.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following tables:
Product
id
name
list price
mrp_bom
id
produ_id (reference of product table)
bom_id (contain reference of self table)
product_qty
Now I want this stub if I select any product than related bom_product and if in bom_product any sub bom_product up to nested.
Following image for mrp_bom table. Now suppose I select computer which id is 1 than for computer I requires CPU,LCD,Key board as a BOM Now CPU also requires harddisk motherboard and ram so I want recursive query if I select computer than give all bom as well as sub bom.
I wrote following query but it give one level out put
select mb.product_id,mb.name
from
(with recursive subproduct as
(select name,product_id,id from mrp_bom
where product_id=2
Union All
select mb.name,mb.product_id,mb.id
from mrp_bom as mb
Join
subproduct as sp on (mb.bom_id=sp.id)
)
select name,product_id,id from subproduct) mb
join mrp_bom mp on mb.product_id=mp.id
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to get the total cost of all the invoices for a customer. Ideally the end format will be two columns [customer name] and [total of invoices]. I have broken it down into the parts so far so I can check and better understand the process of joining the tables and have done a calculation to get the total of items on each invoice but now I am stuck.
As you can see from my screenshot ( Had to link to my google docs as I couldn't post the image up here - sorry) I am getting the company name listed multiple times. Once for each item and also for each invoice number and then item. How can I change my query to show the customer name only once with the corresponding totals of all the invoices combined?
I have lines 3 and 4 as comments of what I think is next so I can work this in steps before fine tuning the query to my desired output.
Thanks
Select Customer.CustName, Sum(InvoiceItem.Quantity*Item.ItemPrice) As TotalValue
From Customer
Inner Join Invoice On Customer.CustABN = Invoice.CustABN
Inner Join InvoiceItem On Invoice.InvoiceNo = InvoiceItem.InvoiceNo
Inner Join Item On InvoiceItem.ItemNo = Item.ItemNo
Group By Customer.CustName
Something like this should work using SUM and GROUP BY:
SELECT CustomerName, SUM(itemPrice * qty) InvoiceTotal
FROM YourTables With Your Joins
GROUP BY CustomerName
If you posted your entire query above, I could copy and paste into the example. But this should get you going in the right direction.
grouping could help, also you need to check if your dbms allows grouping without using agregate functions (some DBMS do not allw it, and return misleading results).
multiple companies is because of the relation company-invoice-product i guess.