Join two excel table - sql

I have two excel table structure and order
structure contain
Product | Component | Order_Quantity
A | C1 | 1
A | C2 | 3
A | C3 | 3
B | C1 | 1
B | C4 | 2
C | C1 | 1
D | C2 | 3
C | C3 | 3
order contain
Order_n | Product | Quantity
1 | A | 10
2 | B | 10
3 | A | 10
4 | C | 10
6 | B | 10
How can i join the two table in excel 2013 (without power pivot or another add-in) to see result like this :
Order_n | Product | Order_Qty | component | Quantity | Total_QTY
1 | A | 10 | C1 | 1 | 10
1 | A | 10 | C2 | 3 | 30
1 | A | 10 | C3 | 3 | 30
2 | B | 10 | C1 | 1 | 10
2 | B | 10 | C4 | 2 | 20
3 | A | 10 | C1 | 1 | 10
3 | A | 10 | C2 | 3 | 30
3 | A | 10 | C3 | 3 | 30
4 | C | 10 | C1 | 1 | 10
4 | C | 10 | C3 | 3 | 30
6 | B | 10 | C1 | 1 | 10
6 | B | 10 | C4 | 2 | 20

You'll have to use VBA.
This should work... Couldn't test the whole thing, so let me know if it works.
Sub JoinTables()
Dim cn
Dim rs
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim strTbl1 As String
Dim strTbl2 As String
Dim JoinField As String
Dim Table1Name As String
Dim Table2Name As String
Dim Table3Address As String
Dim Table1Worksheet As String
Dim Table2Worksheet As String
Dim Table3Worksheet As String
' CHANGE THESE!
Table1Name = "structure": Table1Worksheet = "Sheet1" ' Name of table and name of the worksheet it is on
Table2Name = "order": Table2Worksheet = "Sheet1"
Table3Address = "A2": Table3Worksheet = "Sheet2" ' The result
JoinField = "Product"
' ==================================
strFile = ThisWorkbook.FullName
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strTbl1 = " [" & Table1Worksheet & "$" & Replace(ThisWorkbook.Worksheets(Table1Worksheet).ListObjects(Table1Name).Range.AddressLocal, "$", "") & "] AS T1 "
strTbl2 = " [" & Table2Worksheet & "$" & Replace(ThisWorkbook.Worksheets(Table2Worksheet).ListObjects(Table2Name).Range.AddressLocal, "$", "") & "] AS T2 "
strSQL = "SELECT * FROM " & strTbl1 & " LEFT JOIN " & strTbl2 & " ON T1." & JoinField & "=T2." & JoinField
rs.Open strSQL, cn
ThisWorkbook.Worksheets(Table3Worksheet).Range(Table3Address).CopyFromRecordset rs
rs.Close
cn.Close
End Sub

Related

How to correct ‘One to Many table’ with missing references in PL/pgSQL?

I have a large datafile featuring a unique 11-digit reference on the first record of each related subset, but not on the subsequent records belonging to the same subsets, which instead feature a bogus 1 or 2-digit reference. The table is in the order it was originally created in, so the subsets are still correctly grouped together, and the field comprising these references is called ‘Old Ref’. I’ve just created a new field, called ‘New Ref’, which I want to populate with all the appropriate 11-digit references for each subset. How can this best be achieved in a script I can run on PL/pgSQL?
Here's an example of the data as is (current), and as I would like it to be updated:
Current datafile
__________________________________
ID | Old Ref | New Ref
==================================
1 | 14740807000 |
2 | 1 |
3 | 2 |
4 | 3 |
5 | 58 |
6 | 14735113000 |
7 | 1 |
8 | 2 |
9 | 39 |
10 | 4 |
11 | 5 |
12 | 14915146000 |
13 | 9 |
14 | 27 |
15 | 14915146000 |
16 | 3 |
17 | 4 |
==================================
Sought updated datafile
__________________________________
ID | Old Ref | New Ref
==================================
1 | 14740807000 | 14740807000
2 | 1 | 14740807000
3 | 2 | 14740807000
4 | 3 | 14740807000
5 | 58 | 14740807000
6 | 14735113000 | 14735113000
7 | 1 | 14735113000
8 | 2 | 14735113000
9 | 39 | 14735113000
10 | 4 | 14735113000
11 | 5 | 14735113000
12 | 14915146000 | 14915146000
13 | 9 | 14915146000
14 | 27 | 14915146000
15 | 14915175959 | 14915175959
16 | 3 | 14915175959
17 | 4 | 14915175959
==================================
I had what I thought was a neat way of doing this in MS Access, but that can’t handle the size of the datafile I need to update, which is one of the reasons why I’ve switched to PostgreSQL.
Here’s that MS Access script:
Sub UpdateRef()
On Error GoTo ErrorHandler
Dim strSQL, Var1 As String
Dim rs As New ADODB.Recordset
rs.Open "SELECT test.* FROM test", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
rs.MoveFirst
Var1 = rs![Old ref]
rs![New Ref] = Var1
rs.Update
Do Until rs.EOF
'SysCmd acSysCmdUpdateMeter, n
If len(rs![Old ref]) > 2 Then
Var1 = rs![field2]
End If
rs![New Ref] = Var1
rs.Update
rs.MoveNext
Loop
rs.Close
ExitSub:
Set rs = Nothing
Exit Sub
ErrorHandler:
Resume ExitSub
End Sub
You can do it with this update statement:
update test t
set newref = (select tt.oldref from test tt where tt.id = (
select max(id) from test where id <= t.id and length(oldref) = 11
));
See the demo
This:
select max(id) from test where id <= t.id and length(oldref) = 11
gets the id of the row containing the 11 digit oldref

Excel help for data conversion

Excel Help Needed: I am having the excel sheet with data in below format.
From/to | C1 | C2 | C3 |
---------------------------
R1 | 30 | 31 | 32 |
R2 | 40 | 41 | 42 |
I want to rearrange them in below format using micros/VBA Script or any other feature in excel.
S.no | From| TO |Rate|
-------------------------
1 | R1 | C1 | 30 |
2 | R1 | C2 | 31 |
3 | R1 | C3 | 32 |
4 | R2 | C1 | 40 |
5 | R2 | C2 | 41 |
6 | R2 | C3 | 42 |
Any help for rearranging the excel without manual effort is appreciated
Test bellow.
Sub transData()
Dim vDB, vR()
Dim i As Long, r As Long, j As Integer
Dim n As Long
vDB = Range("a1").CurrentRegion
r = UBound(vDB, 1)
For i = 2 To r
For j = 2 To 4
n = n + 1
ReDim Preserve vR(1 To 4, 1 To n)
vR(1, n) = n
vR(2, n) = vDB(i, 1)
vR(3, n) = vDB(1, j)
vR(4, n) = vDB(i, j)
Next j
Next i
Sheets.Add
Range("a1").Resize(1, 4) = Array("S.no", "From", "TO", "Rate")
Range("a2").Resize(n, 4) = WorksheetFunction.Transpose(vR)
End Sub

Issue on create vlookup function and check the condition?

I am try to create vlookup function for compare date with in two sheet to find out given date is available in sheet2. If it is not available in sheet 2 means we can create other wise tell message already available for that code
mycode
Sub Button1_Click()
Dim ColNum As Integer
Dim Line As String
Dim LineValues() As Variant
Dim OutputFileNum As Integer
Dim PathName As String
Dim RowNum As Integer
Dim SheetValues() As Variant
PathName = Application.ActiveWorkbook.Path
OutputFileNum = FreeFile
Open PathName & "\Upload Additional PDP.csv" For Output Lock Write As #OutputFileNum
Print #OutputFileNum, "Dist_Code" & "," & "Actual_PDP_Date (dd/MM/yyyy)" & "," & "Reason_Code"
Dim rng1 As Range, rng2 As Range, i As Integer, j As Integer
End Sub
for ex:
1
+--------+--------+---------------+---------+--------+
| A | B | C | D | E |
+--------+--------+---------------+---------+--------+
| Code | Name | Description | Price | Cost | Date
+--------+--------+---------------+---------+--------+
| AC33 | Prod 1 | Prod Desc 1 | 3.99 | 2.00 | 16/7/2014
+--------+--------+---------------+---------+--------+
| AC34 | Prod 2 | Prod Desc 2 | 4.99 | 3.00 | 16/7/2014
+--------+--------+---------------+---------+--------+
| AC35 | Prod 3 | Prod Desc 3 | 5.99 | 4.00 | 16/7/2014
+--------+--------+---------------+---------+--------+
2
+--------+--------+---------------+---------+
| A | B | C | D |
+--------+--------+---------------+---------+
| Code | Name |Updated Price | Cost | Date
+--------+--------+---------------+---------+
| AC33 | Prod 1 | 16.99 | |
+--------+--------+---------------+---------+
| AC37 | Prod 2 | 18.99 | |
+--------+--------+---------------+---------+
| AC38 | Prod 3 | 21.99 | |
+--------+--------+---------------+---------+
Use the IsError function to test the result of the VLOOKUP, here's something to get you started:
If IsError(Application.VLookup(Range("A1"), Range("C:C"), 1, False)) Then
Debug.Print "Not found"
Else
Debug.Print "Found"
End If

query to fetch record without duplication

MS-access query : group by name and age, and get the detail without duplicate data(name and age)
please find below the detail
table schema:
id: integer
name: varchar(100)
age: integer
city: varchar(100)
records in table:
------------------------------------
id| name | age | city
------------------------------------
1 | ram | 25 | bhopal
2 | brajesh | 30 | indore
3 | ram | 25 | indore
4 | ram | 26 | bhopal
5 | ram | 27 | mumbai
6 | brajesh | 30 | mumbai
7 | brajesh | 26 | dehli
------------------------------------
Expected result :
------------------------------------
name | age | city | city
------------------------------------
ram | 25 | bhopal | indore
brajesh | 30 | indore | mumbai
------------------------------------
Other format of Expected output :
Expected result :
------------------------------------
name | age | city
------------------------------------
ram | 25 | bhopal, indore
brajesh | 30 | indore, mumbai
------------------------------------
This will do the trick :
select distinct d1.name, d1.age, concat(d1.city, ' , ' ,d2.city) AS city from
Details d1 join Details d2
on d1.name = d2.name
and d1.age = d2.age
and d1.city != d2.city
and d1.id < d2.id
Here is the SQLFiddle
Also note that for this to work id must be unique.
Unfortunately it is impossible to get first output by query, due to need spawning columns for every distinct city.
This is how to get second variant in ms-access, but it is not optimal:
define sfunction
public function SitiesOf(Name as String,Age as long) as String
dim rst as recordset
SitiesOf = ", "
set rst = currentdb.Openrecordset("SELECT [city] FROM [Table] WHERE [name]='" & Name & "' AND [age]=" & Age & ";")
while not rs.eof
SitiesOf = SitiesOf & rst![city] & ", "
rst.MoveNext
wend
rst.close
set rst=Nothing
SitiesOf = left(SitiesOf , len(SitiesOf)-2)
end function
Write query
SELECT [name] , [age] , SitiesOf([name],[age]) FROM [Table] GROUP BY [name] , [age]

VB.NET LINQ Group By Multiple Columns

I have the following LINQ code which has a syntax error and i've no idea how to fix it
Dim query =
From row In mainDatatable.AsEnumerable
'the syntax Error is in the following line
Group row By new { row.Field(Of Double)("cprice") , row.Field(Of Integer)("ccategory")}
Into ProductGroups
'**************************
Dim grpNumber = 1
For Each grp In query
For Each row In grp.ProductGroups
row.SetField("gnum", grpNumber)
Next
grpNumber += 1
Next
End Sub
so what am i doing wrong?
Here is how the table looks like and i want to group by the columns cprice and ccategory
+---------------+---------------+-----------+------+
| Product Name | cprice | ccategory | gnum |
+---------------+---------------+-----------+------+
| Skirt Red | 99 | 1 | |
| Jeans Blue | 49 | 2 | |
| Jeans Black | 49 | 2 | |
| Skirt Blue | 99 | 1 | |
| T-shirt White | 20 | 2 | |
| T-shirt Green | 20 | 2 | |
| Jeans Grey | 49 | 2 | |
+---------------+---------------+-----------+------+
Try this:
new with { Key.Price = row.Field(Of Double)("cprice") ,
Key.Category = row.Field(Of Integer)("ccategory")
Uisng the Key modifier, this creates an anonymous type that implements equality based on equality of its members.