how to add relationship within joined records in NEO4J - database

i have two records ('orders' and 'menulist') that are joined with 'orderitem' by orderID nad menuID
[1]: https://i.stack.imgur.com/5kF8R.png
[2]: https://i.stack.imgur.com/sabUE.png
[3]: https://i.stack.imgur.com/0sqk1.png
i was trying to promote each orderitem record into a relationship in the graph
what I did is:
LOAD CSV WITH HEADERS FROM "file:///orders.csv" AS row
CREATE (n:Orders)
SET n = row
LOAD CSV WITH HEADERS FROM "file:///menulist.csv" AS row
CREATE (n:Menu)
SET n = row
CREATE INDEX FOR (m:Menu) ON (m.MenuID)
CREATE INDEX FOR (o:Orders) ON (o.OrderID)
LOAD CSV WITH HEADERS FROM "file:///orderitem.csv" AS row
MATCH (m:Menu), (o:Orders)
WHERE m.MenuID = row.MenuID AND o.orderID = row.orderID
CREATE (o)-[oi:CONTAINS]->(m)
SET oi = row,
oi.Quantity = toInteger(row.Quantity)
but I got (no changes, no records), seems there is an error here, can anyone help to solve?

That is not the proper way of creating node from csv. Follow the examples found in this link: https://neo4j.com/docs/cypher-manual/current/clauses/load-csv/#load-csv-import-data-from-a-csv-file
In your example, remove the Set n = row because it will not assign the columns in the csv to each property in the node. You need to define the attributes one at a time. See below.
LOAD CSV WITH HEADERS FROM "file:///orders.csv" AS row
CREATE ( :Orders {ID: row.OrderID, <and so on> , paymentID: row.PaymentID })
Also notice that the variable n in label :Orders is not needed.

Related

How can I JOIN a VIEW to a TABLE based on unique values?

How can I join a VIEW to a TABLE based on unique values? Background information: a CSV file is downloaded from a web browser containing customer information, including shipping addresses and order numbers. The order numbers are our unique identifiers. The CSV file will contain duplicate information that we've already imported earlier. We want to prevent the duplicates from coming into our main table.
The CSV file is difficult to work with to directly import into SQL so it requires some Excel work to get it prepared by inserting double quotes in each data field for the import. The import goes into a table - the dbo.staging with double quotes for each data field. Every import will delete the existing rows.
Then, it goes into a view (dbo.cleandata) removing double quotes. Every import will delete the existing rows. We now have clean data.
From the view, we now want to append every NEW order to the main table (dbo.main), and NOT add the existing order data to the main table. This is where I am stuck. How do I write data from the view, dbo.cleandata, into dbo.main where order number does not exist in table dbo.main? The below query does not give me the results we desire. Any suggestions?
INSERT INTO [dbo].[main] ([bill-to-name]
,[order-number]
,[ship-to-name]
,[ship-to-address1]
,[ship-to-city]
,[ship-to-state]
,[ship-to-zip])
SELECT A.[bill-to-name]
,A.[order-number]
,A.[ship-to-name]
,A.[ship-to-address1]
,A.[ship-to-city]
,A.[ship-to-state]
,A.[ship-to-zip]
FROM [$dsp].[dbo].[cleandata] as A
JOIN
[main] on [main].[order-number] = A.[order-number]
WHERE [main].[order-number] <> A.[order-number]
INSERT INTO [dbo].[main] ([bill-to-name]
,[order-number]
,[ship-to-name]
,[ship-to-address1]
,[ship-to-city]
,[ship-to-state]
,[ship-to-zip])
SELECT A.[bill-to-name]
,A.[order-number]
,A.[ship-to-name]
,A.[ship-to-address1]
,A.[ship-to-city]
,A.[ship-to-state]
,A.[ship-to-zip]
FROM [$dsp].[dbo].[cleandata] as A
LEFT JOIN [main] on [main].[order-number] = A.[order-number]
WHERE [main].[order-number] IS NULL;

SQL Server : update join statement of two tables with multiple columns from existing table to match

I need to update an existing table (First) with a new column (Ticket_No_Calc). Basically, there are two columns (Doc_Header and Doc_No) that I have to lookup/refer to in the first table against the second table (Doc_No) column.
The desired column Ticket_No_Calc result is retrieved from the column Ticket_No.
Below is the screenshot of sample data of the first table
and this is the screenshot of sample data of the second table
Currently, in Excel, I use the following formula to get the desired column in the existing table
=IFERROR(VLOOKUP(FIRST!B2,'SECOND'!A:B,1,0),(VLOOKUP(FIRST!C2,'SECOND'!A:B,1,0)))
I am not sure how to do this in SQL. I know that I can get this done in SQL with the following formula if I only have to refer to one column Doc_Header:
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A
LEFT JOIN Second AS B ON A.Doc_Header = B.Doc_NO ;
But I need to refer to two columns in the existing table. I have been researching for sometimes to get the correct command but I can't seem to be able to crack it.
Following statement may help
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A
INNER JOIN Second AS B ON (A.Doc_Header = B.Doc_NO) OR (A.Doc_NO = B.Doc_NO) ;
As per the sample data provided this is query seems incorrect. But as per the comments mentioned in your question it is better to perform this in 2 steps.
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A
LEFT JOIN Second AS B ON A.Doc_Header = B.Doc_NO ;
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A
LEFT JOIN Second AS B ON A.DOC_NO=B.DOC_NO
WHERE A.Doc_Header <> B.Doc_NO
Try this:
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A ,Second AS B where A.Doc_Header = B.Doc_NO or A.Doc_NO = B.Doc_NO

T-SQL - Update long list of values based on long list of ids

It may seem as a straight forward task, but I can't really find a good approach.
I have a long list of ids with a long list of corresponding values to update for a field (a single field)
id = 1 | field = value_1
id = 2 | field = value_2
.......................
id = n | field = value_n
I can put the fields in 2 lists (or any other way i choose to) but i have to loop through and update each value..
What would be the best approach for this?
To add few more details: The values are in a big excel, but this is not about processing that excel, I will copy paste the list of values into.. text. I was thinking 2 long list (id1, id2,..) (value_1, value_2,...)
For a one time job, convert the text into a CSV or other format that is processable by bcp.exe, then import it into a temp table, do the update via a JOIN, then drop the temp table.
For a repeatable job I would us SSIS: flat file source the data or even directly Excel source, source the table, merge the two sources, apply the result back into the table.
The selected answer is a good method, but for completeness: when this is a one-time task, and the updates all follow a simple pattern like that, it can also be effective to convert the input text directly into a series of update statements, using an Excel formula and fill down or using a text editor's replace function.
Example:
id newvalue
1 foo
2 grok
becomes
id newvalue generated statement
1 foo update dbo.mytable set field1 = 'foo' where id = 1
2 grok update dbo.mytable set field1 = 'grok' where id = 2
Quick and dirty, but apply with care and watch out for unexpected syntax errors.
is what i did in the end, I created a temp table, I imported all the values in it and updated via a join

View showing incorrect data

I have a very simple database. It contains 3 tables. The first is the primary input table where values go in. The 2nd and 3rd are there purely for translating values to names for a view. When I view the rows in table 1, I can see that column businessUnit contains valid values in all rows. When I add the Business_Units table (The 3rd table in this DB), all but 2 rows go away and despite the businessUnit value both being 1 in the 1st table, the view gives them different names.
I created a DB diagram and uploaded a screenshot to imgur. Link: http://imgur.com/jXF7L1R
I only have 2 relationships in the table. One from equipType on New_Equipment_User to id in Equipment_Type and one from businessUnit in New_Equipment_User to id in Business_Units. The weird thing is that the Equipment Type works perfectly, yet when I replicate the table, relationship and view information exactly, it doesn't work. Instead of 6 rows appearing, there are only 2 which share the same value in businessUnit, but gives 2 different names for it.
In case it matters, here is my view Query:
SELECT dbo.New_Equipment_User.id, dbo.Equipment_Type.name AS equipType, dbo.New_Equipment_User.jobNumber, dbo.New_Equipment_User.costCode,
dbo.New_Equipment_User.reason, dbo.New_Equipment_User.mobile, dbo.New_Equipment_User.mobileQty, dbo.New_Equipment_User.mobileComment,
dbo.New_Equipment_User.laptop, dbo.New_Equipment_User.laptopQty, dbo.New_Equipment_User.laptopComment, dbo.New_Equipment_User.desktop,
dbo.New_Equipment_User.desktopQty, dbo.New_Equipment_User.desktopComment, dbo.New_Equipment_User.modem, dbo.New_Equipment_User.modemQty,
dbo.New_Equipment_User.modemComment, dbo.New_Equipment_User.printer, dbo.New_Equipment_User.printerQty, dbo.New_Equipment_User.printerComment,
dbo.New_Equipment_User.camera, dbo.New_Equipment_User.cameraQty, dbo.New_Equipment_User.cameraComment, dbo.New_Equipment_User.dateRequired,
dbo.New_Equipment_User.requestedBy, dbo.New_Equipment_User.dateRequested, dbo.New_Equipment_User.approvalStatus,
dbo.Business_Units.name AS businessUnit
FROM dbo.New_Equipment_User
JOIN dbo.Equipment_Type ON dbo.New_Equipment_User.equipType = dbo.Equipment_Type.id
JOIN dbo.Business_Units ON dbo.New_Equipment_User.id = dbo.Business_Units.id
WHERE (dbo.New_Equipment_User.approvalStatus = '0')
And here is an image of the view since it is easier to read: http://imgur.com/pZ97ehQ
Is anyone able to assist with why this might be happening?
Try using a LEFT JOIN
SELECT ...
FROM dbo.New_Equipment_User
JOIN dbo.Equipment_Type ON dbo.New_Equipment_User.equipType = dbo.Equipment_Type.id
LEFT JOIN dbo.Business_Units ON dbo.New_Equipment_User.id = dbo.Business_Units.id
This will ensure that all dbo.New_Equipment_User and all dbo.Equipment_Type is present

Insert fields from one table to another in SQL Server 2005

I have a table from which I need to insert some of its data into another table.
I have tables "Provider" and "1_MAIN - Contacts" (I know really bad name, lets just call it "Main" in this discussion). The 2 tables are linked by a column named "Contact_ID". All records in the Provider table have a record in the Main table with a matching "Contact_ID".
The "Providers" table has 4 columns called "geri", "adol", "adult", and "pedi". These fields are datatype bit with a default value of 0.
I have created 4 new columns in "Main" with the same names and want to pull the values from the "Provider" table into the "Main" table.
So basically what I want to accomplish is this:
If a "Main" record has a record in "Provider" then grab the values of the "geri", "adol", "adult", and "pedi" columns from its "Provider" record and copy them into its "Main" record.
You need UPDATE, not INSERT :
UPDATE m
SET geri = p.geri
, adol = p.adol
, adult = p.adult
, pedi = p.pedi
FROM Main m
JOIN Provider p
ON p.Contact_ID = m.Contact_ID
You're looking for "INSERT INTO...SELECT". Something like (you'll need to modify this)
Insert Into Main(geri, adol, adult, pedi)
Select p.geri, p.adol, p.adult, p.pedi
from Provider p
where p.ProviderID = Main.ProviderID
That's rough, because I haven't done it in a while, but it should get you fairly close.

Resources