CREATE TABLE PRODUCT (
ProductID int not null identity primary key,
ProductName varchar(50)
) AS Node;
INSERT INTO PRODUCT(ProductName) VALUES ('Car'),('BodyWork'),('Wheel'),('Tyre');
CREATE TABLE SUPPLIER (
SuppierID int not null identity primary key,
SupplierName varchar(50)
) AS Node;
INSERT INTO SUPPLIER(SupplierName) VALUES ('Continental'),('Tesla');
CREATE TABLE HAS_PART AS EDGE;
INSERT INTO HAS_PART ($from_id, $to_id)
VALUES ((SELECT $node_id FROM PRODUCT WHERE ProductName='Car'), (SELECT $node_id FROM PRODUCT WHERE ProductName='BodyWork'))
,((SELECT $node_id FROM PRODUCT WHERE ProductName='Car'), (SELECT $node_id FROM PRODUCT WHERE ProductName='Wheel'))
,((SELECT $node_id FROM PRODUCT WHERE ProductName='Wheel'), (SELECT $node_id FROM PRODUCT WHERE ProductName='Tyre'));
CREATE TABLE SUPPLIED_BY AS EDGE;
INSERT INTO SUPPLIED_BY ($from_id, $to_id)
VALUES ((SELECT $node_id FROM PRODUCT WHERE ProductName='Car'), (SELECT $node_id FROM SUPPLIER WHERE SupplierName='Tesla'))
,((SELECT $node_id FROM PRODUCT WHERE ProductName='Tyre'), (SELECT $node_id FROM SUPPLIER WHERE SupplierName='Continental'));
SELECT * FROM HAS_PART;
SELECT * FROM SUPPLIED_BY;
CREATE VIEW All_nodes AS
SELECT ProductID, ProductName, NULL AS SupplierID, NULL AS SupplierName, ProductName AS NodeName FROM PRODUCT
UNION ALL
SELECT NULL, NULL, SuppierID, SupplierName , SupplierName AS NodeName FROM SUPPLIER;
CREATE VIEW All_edges AS
SELECT * FROM HAS_PART
UNION ALL
SELECT * FROM SUPPLIED_BY;
SELECT
P1.ProductID,
P1.ProductName,
COUNT(P2.NodeName) WITHIN GROUP (GRAPH PATH) AS [Depth],
STRING_AGG(P2.NodeName,'->') WITHIN GROUP (GRAPH PATH) AS [Assembly],
LAST_VALUE(P2.ProductID) WITHIN GROUP (GRAPH PATH) AS [Final Product ID],
LAST_VALUE(P2.SupplierID) WITHIN GROUP (GRAPH PATH) AS [Final Supplier ID],
LAST_VALUE(P2.ProductName) WITHIN GROUP (GRAPH PATH) AS [Final Product Name],
LAST_VALUE(P2.SupplierName) WITHIN GROUP (GRAPH PATH) AS [Final Supplier Name]
FROM
All_nodes P1,
All_nodes FOR PATH P2,
All_edges FOR PATH HP
WHERE MATCH(SHORTEST_PATH(P1(-(HP)->P2)+))
AND P1.ProductID = 1
ORDER BY P1.ProductID;