Files
sql-server-samples/samples/demos/sql-graph/recommendation-system/demo3-create-and-populate-nodes-and-edges.sql
T
Sergio Govoni 96f947b085 SQL Server 2017, Thanks to Graph Database, can express certain kinds of queries more easily than a relational database by transforming complex relationships into graphs (#354)
* Added some media for incoming demos on SQL Graph database

* Added the file README.md and the first demo of the session SQL Server 2017 Graph Database that I have done at SQL Saturday Parma 2017 #sqlsat675

* Fixed a bug in SQL text visualization

* Deleted some Unicode characters

* Update file README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md added the demo "Build a sample recommendation system using SQL Graph"

* Added the file sqlsat675 40 Recommendation system.sql that contains source code of the demo "Build a sample recommendation system using SQL Graph"

* Update README.md

* Added the file sqlsat675 30 Many-to-many relationships.sql

* Delete folder sql-saturday-675-parma-italy

* Add folder recommendation-system

* Rename files

* Update README.md

* Update README.md

* Update media file related to SQL Graph demos

* Used function JSON_VALUE instead of OPENJSON to extract the first language of a Person

* Update README.md

* Update README.md

* Update README.md

* Update README.md
2018-01-29 14:44:32 -08:00

119 lines
2.3 KiB
Transact-SQL

------------------------------------------------------------------------
-- Event: SQL Saturday #675 Parma, November 18 2017 -
-- http://www.sqlsaturday.com/675/EventHome.aspx -
-- Session: SQL Server 2017 Graph Database -
-- Demo: Demo2: Create and populate nodes and edges -
-- Author: Sergio Govoni -
-- Notes: -- -
------------------------------------------------------------------------
USE [WideWorldImporters];
GO
-- Create Customers node
-- This node holds the main details of the Customer
/*
DROP TABLE IF EXISTS Nodes.Customers;
GO
*/
CREATE TABLE Nodes.Customers
(
[CustomerID] INTEGER NOT NULL
,[CustomerName] NVARCHAR(100) NOT NULL
,[WebsiteURL] NVARCHAR(256) NOT NULL
)
AS NODE;
GO
INSERT INTO Nodes.Customers
(
[CustomerID]
,[CustomerName]
,[WebsiteURL]
)
SELECT
[CustomerID]
,[CustomerName]
,[WebsiteURL]
FROM
Sales.Customers;
GO
-- Create StockItems node
/*
DROP TABLE IF EXISTS Nodes.StockItems;
GO
*/
IF OBJECT_ID('Nodes.StockItems', 'U') IS NULL
CREATE TABLE Nodes.StockItems
(
StockItemID INTEGER IDENTITY(1, 1) NOT NULL
,StockItemName NVARCHAR(100) NOT NULL
,Barcode NVARCHAR(50) NULL
,Photo VARBINARY(MAX)
,LastEditedBy INTEGER NOT NULL
)
AS NODE;
GO
IF OBJECT_ID('Nodes.StockItems', 'U') IS NOT NULL
BEGIN
SET IDENTITY_INSERT Nodes.StockItems ON;
INSERT INTO Nodes.StockItems
(
StockItemID
,StockItemName
,LastEditedBy
)
SELECT
StockItemID
,StockItemName
,LastEditedBy
FROM
Warehouse.StockItems;
SET IDENTITY_INSERT Nodes.StockItems OFF;
END;
-- Create the edge from nodes Customers and StockItems
/*
DROP TABLE IF EXISTS Edges.Bought;
GO
*/
CREATE TABLE Edges.Bought
(
[PurchasedCount] BIGINT
)
AS EDGE;
GO
INSERT INTO Edges.Bought
(
$from_id
,$to_id
,[PurchasedCount]
)
SELECT
-- $from_id
C.$node_id
-- $to_id
,P.$node_id
-- PurchasedCount
,PurchasedCount = COUNT(OD.OrderLineID)
FROM
Sales.OrderLines AS OD
JOIN
Sales.Orders AS OH ON OH.OrderID = OD.OrderID
JOIN
Nodes.Customers AS C ON C.CustomerID = OH.CustomerID
JOIN
Nodes.StockItems AS P ON P.StockItemID = OD.StockItemID
GROUP BY
C.$node_id
,P.$node_id;
GO