I currently have 2 tables as follows within my database:
Table: SampleProducts
SampleProductsId (PK) Name
1 A
2 B
3 C
4 D
5 E
6 F
7 G
Table: SampleProductsBoms
SampleProductsBomId (PK) ParentId (FK) ChildId (FK) Quantity
1 1 2 3
2 2 3 4
3 4 6 2
ParentId and ChildId both reference SampleProductsId
In English so I can ensure that we are all on the same page:
Product A is made up of 3 of B
Product B is made up of 4 of C
Product D is made up of 2 of F
I would like to create a Stored Procedure / LinQ statement or something which I can use in my MVC 3 c# Web Application which will give me the following table structure / object to use...
Example:
Recursive Query to find the components of B
ProductId Name Quantity
3 C 4
6 F 2
This could go quite deep, so I really do need recursion!
CTE is helpfull for recursing as require in your problem statement check the link
Common Table Expression
or i think following query may also solve your purpose
select components.SampleProductId as productid,components.Name as Name,Quantity
from SampleProductsBOM bom
inner join SampleProducts products
on products.ParentId=bom.ParentId
inner join SampleProducts components
on components.SampleProductId=bom.ChildId
where products.Name='B'
Related
I got a table like this
a b c
-- -- --
1 1 10
2 1 0
3 1 0
4 4 20
5 4 0
6 4 0
The b column 'points' to 'a', a bit like if a is the parent.
c was computed. Now I need to propagate the parent c value to their children.
The result would be
a b c
-- -- --
1 1 10
2 1 10
3 1 10
4 4 20
5 4 20
6 4 20
I can't make an UPDATE/SELECT combo that works
So far I got a SELECT that procuce the c column I'd like to get
select t1.c from t t1 join t t2 on t1.a=t2.b;
c
----------
10
10
10
20
20
20
But I dunno how to stuff that into c
Thanx in advance
Cheers, phi
You have to look up the value with a correlated subquery:
UPDATE t
SET c = (SELECT c
FROM t AS parent
WHERE parent.a = t.b)
WHERE c = 0;
I finnally found a way to copy back my initial 'temp' SELECT JOIN to table 't'. Something like this
create temp table u as select t1.c from t t1 join t t2 on t1.a=t2.b;
update t set c=(select * from u where rowid=t.rowid);
I'd like to know how the 2 solutions, yours with 1 query UPDATE correlated SELECT, and mine that is 2 queries and 1 correlated query each, compare perf wise. Mine seems more heavier, and less aesthetic, yet regarding perf I wonder.
On the Algo side, yours take care not to copy the parent data, only copy child data, mine copy parent on itself, but that's a nop, yet consuming some cycles :)
Cheers, Phi
In google BigQuery I have done a simple query to get how many music someone has listened.
What I need is to make a sum for all rows returned from the query below (some type of subquery)?
select count(1) cnt
from OF7.PETERV_TEST
where gender='F'
group by userId
Row f0_
1 14
2 1
3 7
4 18
5 1
6 4
7 2
8 2
expected result:
49
you can use:
SELECT sum(cnt)
FROM
(SELECT count(1) cnt
FROM OF7.PETERV_TEST
WHERE gender='F'
GROUP BY userId )
I have 3 tables.
person
equipment
relation between person and equipment
I have a page with a search button and a checklistbox that binds with the equipment. I want the user to be able to select one or more check list and then show people associated with the selected equipment.
-------------------------
table1
p_famil p_code
ali 1
reza 2
mohamad 3
kazem 4
---------------------------------
table 2
e_code e_title
1 home
2 car
3 job
--------------------------------
table 3
er_eq_code er_per_code
2 1
3 1
1 2
1 4
I have two tables customer & home in a local database for a ipad application.
I have a SQL query
SELECT C.CustomerID AS 'CID', H.CustomerID AS 'HID', H.lotNumber AS 'lotNumber' FROM CustomerInformation C LEFT JOIN HomeInformation AS H ON ( H.CustomerID = C.CustomerID )
It returns the proper amount of records, and display my address data correctly. However, all the data in the HomeInformation table is null
CustomerID FirstName HomeID CusomterID lotNumber
1 Josh 1 1 73824
2 Smith 2 2 54353
3 Chris 3 3 75342
4 Bob 4 4 42342
5 John 5 5 24342
I have tried to use INNER join, Cross Join, and Outer Join, but no records are returned.
I have 3 tables. Namely Job_Master,Print_details,Paper_Details.
The Structure of the table is as follows:
Job_Master:
jobno jobname amount
1 A 100
2 B 200
3 C 300
Print_Details id being the PK and jobno being FK
id jobno color
1 1 Cyan
2 1 Red
3 2 Black
4 3 Black
5 3 Green
Paper Details id being the PK and jobno being FK
id jobno Type
1 1 Art Paper
2 1 Photo Paper
3 2 Art Paper
4 3 Copier
5 3 Glossy Paper
I want a write a query in SQL server or perform Dataset operations in ASP.net so as to display the below resultset in the grid view:
Desired Resultset:
jobno jobname printDetails paperDetails amount
1 A CYAN,RED Art Paper,Photo Paper 100
2 B Black Art Paper 200
3 C Black,Green Copier,Glossy Paper 300
is this possible using dataset operations in ASP.net or in a SQL server query. I am using SQL server 2008 R2 as my database and the fromt end has been designed using ASP.net3.5.
Any help on this is much appreciated.Thanks in advance.
Try this
SELECT DISTINCT a.Jobno, a.jobname, COALESCE(b.color + ', ', '') + b.color,COALESCE(c.type + ', ', '') + c.type
FROM job_master a left JOIN cprint_details b ON a.jobno=b.jobno
left join paper_details c on a.jobno=c.jobno
ORDER BY a.jobno
Read here for more info LINK