Inner join 2 tables one to many 2 where clauses [closed] - sql

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.

Related

Join on non unique column [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have two tables in DB and I want to select and join some data from these table.
The first table has some customers:
customer
id
Dave
1
Tom
2
The second table has a list of products and a column that indicates which customer bought that Product:
id
product
isin
customer id
1
PC
XV452889
1
2
phone
VN865232
2
3
laptop
PL201325
1
I tried INNER JOIN in order to get as output a table that lists for each product that have been bought, who was its customer.
Desired output:
id
product
isin
customer id
customer
1
PC
XV452889
1
Dave
2
phone
VN865232
2
Tom
3
laptop
PL201325
1
Dave
I tried inner join but the answer is empty, its like you cant join two table on a non-unique column. it has been two days I try to solve it.
select table2.product, table2.customer_id
from table2
inner join table1 on table1.id = table2.customer_id;
Hers is the query I am running on similar tables (allocations table is equivalent to the second table of product above and orders is equivalents to customer table):
You can join the customer.id on product.customer_id:
SELECT p.*, c.customer
FROM products p
JOIN customer c on p.customer_id = c.id
THANK YOU ALL FOR YOUR ANSWERS about he join!!!
I found the error in the query, it was missing ',' after orders.instructions...
too long queries ):

how to remove duplicate rows [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the following query to retrieve bunch of data but somehow it creates duplicate rows for certain records.I tried distinct but no use
What i am doing wrong here
SELECT Distinct dbo.tblAssessmentEcosystemCredit.ManagementZoneID, AssessmentEcosystemCreditID,dbo.tblAssessmentEcosystemCredit.AssessmentVersionID ,
(COALESCE(dbo.tblManagementZone.SiteValueCurrentScore,0)
-COALESCE(dbo.tblManagementZone.SiteValueFutureScore,0)) AS LossinSiteValueScore,
5 AS SaveType, dbo.ufn_varbintohexstr(dbo.tblAssessmentEcosystemCredit.RowTimestamp) AS RowTimestamp,
dbo.tblVegetationZone.EECID,
CASE WHEN dbo.tblVegetationZone.EECID > 0 THEN 3.0
ELSE 1.0
END AS EECOffSetMultiplier
FROM dbo.tblAssessmentEcosystemCredit
INNER JOIN dbo.tblVegetationType
ON dbo.tblAssessmentEcosystemCredit.VegTypeID = dbo.tblVegetationType.VegTypeID
INNER JOIN dbo.tblManagementZone
ON dbo.tblAssessmentEcosystemCredit.ManagementZoneID = dbo.tblManagementZone.ManagementZoneID
INNER JOIN dbo.tblVegetationZone
ON dbo.tblVegetationZone.VegetationZoneID = dbo.tblManagementZone.VegetationZoneID
INNER JOIN dbo.tblAssessmentVersion AV ON
AV.AssessmentVersionID = dbo.tblAssessmentEcosystemCredit.AssessmentVersionID
INNER JOIN tblAssessment TBA ON
TBA.AssessmentID = AV.AssessmentID
WHERE dbo.tblAssessmentEcosystemCredit.AssessmentVersionID= #AssessmentVersionID
Possibly you have duplicate rows in your central base table, dbo.tblAssessmentEcosystemCredit. That should be easy to check, as you know which rows to look at.
More likely, you are obtaining multiple result rows corresponding to a few of the dbo.tblAssessmentEcosystemCredit rows because one of the tables you are joining to it has multiple matches for those rows. That is, one of these columns contains at least one duplicated value:
dbo.tblVegetationType.VegTypeID
dbo.tblManagementZone.ManagementZoneID
dbo.tblVegetationZone.VegetationZoneID
dbo.tblAssessmentVersion.AssessmentVersionID
tblAssessment.AssessmentID
The responsible column must not be subject to a single-column UNIQUE constraint, and must not be a single-column primary key for its table, so that may help narrow it down. Note that the whole row does not need to be duplicated, only the ID.

SQL query to make a list from multiple tables [closed]

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

Postgres Query for product , sub product recursive [closed]

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

SQL - What syntax to use? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table named "product_to_category" with 2 column "product_id and category_id".
I have about 500 product ID with 1000 category ID associate with it.
Now I want to add another category ID into all the product ID.
What syntax should I use to make this happen?
Thank you.
Are you looking for this?
INSERT INTO product_to_category(product_id, category_id)
SELECT product_id, 25 -- < new category that you want to add
FROM product_to_category
GROUP BY product_id
That will add category with id 25 to all unique products that you already have in product_to_category. If not all of your products have at least one category defined, then you can select from some product table that I'm sure you have.
Here is SQLFiddle demo
Use something like this:
UPDATE product_to_category
SET category_id=('your_new_category_id')
WHERE product_id = your_product_id;
Remember the Where clause, otherwise this will update all your rows.
The Where clause could also contain a SELECT statement that would select all product_id's for all your products that need to update their category.
Something like:
UPDATE product_to_category
SET category_id=('your_new_category_id')
WHERE product_id = (SELECT product_id .... condition);