Postgres Query for product , sub product recursive [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 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

Related

how do i add text with union in sql? [closed]

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 have two tables:
Family - with columns: Name, ID
Money - with columns: ID, salary, expenses
I have to add each name that his expenses bigger then his salary, the text "spender".
and to all the rest names add the text "frugal".
How to use the Union command to do this?
You shouldn't need a union for this. A join should do:
SELECT
f.Name,
CASE WHEN(SUM(m.Expenses) > SUM(m.Salary)) THEN 'Spender'
ELSE 'Frugal' END AS SomeClass
FROM
Family f
INNER JOIN Money m
ON f.ID = m.ID
GROUP BY
f.Name
SqlFiddle here

Querying multiple tables and returning 1 compiled result [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
Say I have two tables
Table 1
--------
price
product
san
code
Table 2
-------
price
Can I say something like, select price from table1 and select price from table2 and then return the results? So if table 1 had 3 records and table 2 had 5 records, it would return 8 records?
You should use union all as below
select price
from tabel1
union all
select price
from tabel2

Inner join 2 tables one to many 2 where clauses [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 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.

SQL QUERY for finding set of highest numbers [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
how to get top 1,00,000 customers name and email id who have booked maximum number of tickets? The table has columns like:
NAME, CUST_ID, JOINING DATE, NO. OF TICKETS BOOKED, EMAIL_ID.
Something like the following should work if you are using Microsoft Sql server (tsql)
Select TOP 120 columns FROM table ORDER BY columns desc
SELECT TOP 100000 NAME, CUST_ID, [JOINING DATE], [NO. OF TICKETS BOOKED], EMAIL_ID
FROM YOUR_TABLE
ORDER BY [NO. OF TICKETS BOOKED] DESC
Are you looking for this ?
if you are working with SQL SERVER

SQL Server SELECT first [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
Improve this question
I am trying to create a JOIN with 2 tables and having ce first element of the joined table.
My tables look like this :
Product
id
name
Sales
idProduct
prices
date
I want to have the last sales price for each product but the function FIRST doesn't exist in SQL Server.
Someone have an idea ?
Thanks,
You can use a ranking function like ROW_NUMBER:
WITH CTE AS(
SELECT id, name, idProduct, prices, date,
RN = ROW_NUMBER() OVER (PARTITION BY idProduct ORDER BY date DESC)
FROM dbo.Product p INNER JOIN dbo.Sales s on p.id = s.idProduct
)
SELECT * FROM CTE WHERE RN = 1
Ranking Functions (Transact-SQL)
The CTE is a common-table-expression similar to a sub-query but more readable and maintainable.
If it's SQL Server, simply use:
SELECT TOP 1 *
FROM Product p
JOIN Sales s ON p.id = s.idProduct
ORDER BY s.Date DESC