vb.net compare two table, insert the result into new table - mysql

I have two identical table which consist of 3 field: ID, TID, and ITEM..
ID acts as primary key, TID identifies group of ITEM. Each TID could have different number of ITEM.
i want to compare the 2 identical table with condition like
check each grouped ITEM by TID in table 1 to each grouped ITEM by TID in table2
if table1. grouped Item exist as much as N in table2, it will be inserted into new table by query INSERT SELECT
for example at the screenshot, C - F -A has 3 occurences in table 2, thus they will be inserted into new table..
could it be done using mysql query?
i've already tried this using program by populating the two table into two 2d array, comparing them and with simple IF CLAUSE, getting the desired results.. and it worked well..
but the problem is, when i'm using huge database, it take ridiculous time to complete..
when i'm comparing 1st 2d array(table1 with 2k records) and 2nd array (table2 with 870 records), it take 2 hours to complete!!
this is the actual database
and this one is what i've already tried, populating two table above into 2d array.. while assigning both 2d arrays with table records, i also include the IF CLAUSE to check if element of 1st 2d array has N occurences in 2nd 2d arrays, the INSERT SELECT query will be executed..
the yellow blocked node col in 1st 2d array above, will e inserted into new table because it has 3 occurences in 2nd 2d array,
the number 3 came from user input
but since i'm using database object anyway, it cost me an expensive performance and time, too, i'm using vb.net 2005
i believe that there are alternative method for this problem.. could you all please tell me how the method? using mysql query or other method that could perform faster than one that i've tried?
best regards from me, thanks..

use linq its does all the mysql quires. You can find great documentation on it on msdn. Just build the db with mysql sever and use linq or entity they will both do all the commands for you. That way you can do data binding and save that info and pull it or whatever if you have more quesiton I have a copy of code in c# that uses tables for contact info I can see to you
This is a link to GitHub that contains the C# version using Linq
https://github.com/psychotiic/WebContactAppUsingLinq.cs

you will be able to use linq with that code that I just provided even though its C# its will be easy to figure out and if not you can find plenty of C# - VB code changers on the web for free.

Related

Yii2 grid data from multiple tables (selecting records of multiple tables)

I have a grid view where I need to show data of more than a table, each record in a row (Not relations).
Thus, a group of grid rows may come from table one, another group of rows from table two, ect...
For example, I need to select from tbl1 rows (suppose r1 to r10), and from table tbl2, rows (suppose r11 to r20) r1 to r10 and r11 to r20 may have a lot of common id's (because from different tables). And I want to show all these records in a single grid view, with search and actions enabled.
I have made an attempt to get data in arrayDataProvider, and it worked perfect.
The problems I am trying to fix are two:
1. Enabling the searchModel in the grid. (For that, I have also get all data in the search model in arrayDataProvider, but still need to enable search).
2. I need to know which record is selected for(view, update, or delete) and take action based on the selection, because the same id may exist in the grid multiple times, each from a table.
To Enable Search:
1. I have used all search models to return arrays based on filtering queries.
2. I have used a basic search model that includes common attributes between all tables, it calls functions from other search models to get array from them, then it concatenates all these arrays and returns them as an array data provider.
3. Needed some attention when dealing with parameters in search models, because they are using the same model as the basic, they have more fields.
When I treat id's and acyions on rows, I will post the method.
If any one having the same issue, and need help, I will be :-)

Find top 5 field values in each record. Return value and column heading. MS Access

Hello and thanks in advance for any help.
I wrote a small program in Access that imports text files and places them into tables. The table is laid out with about 20 fields and each field represents a different category that contains a number (hours) for different reasons, for each record. I have been unable to find anything that will get me started in the right direction and need help.
What I want to do is search each record and find the highest five numbers for each record and then return each value with the associated heading for that field.
For example
PGM ASD HFR STE NHU
Client A _____365.4__ 255___254.6___180.1___26
Once I figure out how to query this info from the other 20 columns, my goal is to build a form that have this query attached to a button that returns these values. I can either set it up to search each record or search all records and find the the top five values for all clients.
Again, thanks for any help. I am not hoping that someone will build me a solution, just get me a reference or some material to get me heading in a direction.
In Access, this will require a custom function that compares values of fields. Common requirement is to find the top 1 value from a record. Has been discussed many times in many sites. Google. Finding top 5 does add complication. If data structure were normalized, a TOP N nested query could probably provide desired output.
A workaround for the current structure could be to build a UNION query that rearranges your data to normalized structure (data is vertical instead of horizontal). Then use that query like a table as the source for TOP N nested query. Is there only 1 record for each client? UNION example:
SELECT Client, PGM AS Hrs, "PGM" AS Source FROM tablename
UNION SELECT Client, ASD, "ASD" FROM tablename
UNION SELECT Client, HFR, "HFR" FROM tablename
UNION SELECT Client, STE, "STE" FROM tablename
UNION SELECT Client, NHU, "NHU" FROM tablename
continue for 15 other fields;
Must type or copy/paste in SQLView of query builder. Limit of 50 SELECT lines.
For example of TOP N review http://allenbrowne.com/subquery-01.html#TopN. Unfortunately, this type of nested query can be slow performer. And basing it off a UNION instead of natural table can be even slower.

Creating a global variable in Talend to use as a filter in another component

I have job in Talend that is designed to bring together some data from different databases: one is a MySQL database and the other a MSSQL database.
What I want to do is match a selection of loan numbers from the MySQL database (about 82,000 loan numbers) to the corresponding information we have housed in the MSSQL database.
However, the tables in MSSQL to which I am joining the data from MySQL are much larger (~ 2 million rows), are quite wide, and thus cost much more time to query. Ideally I could perform an inner join between the two tables based on the loan number, but since they are in different databases this is not possible. The inner join that is performed inside a tMap occurs after the Lookup input has already returned its data set, which is quite large (especially since this particular MSSQL query will execute a user-defined function for each loan number).
Is there any way to create a global variable out of the output from the MySQL query (namely, the loan numbers selected by the MySQL query) and use that global variable as an IN clause in the MSSQL query?
This should be possible. I'm not working in MySQL but I have something roughly equivalent here that I think you should be able to adapt to your needs.
I've never actually answered a Stackoverflow question and while I was typing this the page started telling me I need at least 10 reputation to post more than 2 pictures/links here and I think I need 4 pics, so I'm just going to write it out in words here and post the whole thing complete with illustrations on my blog in case you need more info (quite likely, I should think!)
As you can see, I've got some data coming out of the table and getting filtered by tFilterRow_1 to only show the rows I'm interested in.
The next step is to limit it to just the field I want to use in the variable. I've used tMap_3 rather than a tFilterColumns because the field I'm using is a string and I wanted to be able to concatenate single quotes around it but if you're using an integer you might not need to do that. And of course if you have a lot of repetition you might also want to get a tUniqueRows in there as well to save a lot of unnecessary repetition
The next step is the one that does the magic. I've got a list like this:
'A1'
'A2'
'B1'
'B2'
etc, and I want to turn it into 'A1','A2','B1','B2' so I can slot it into my where clause. For this, I've used tAggregateRow_1, selecting "list" as the aggregate function to use.
Next up, we want to take this list and put it into a context variable (I've already created the context variable in the metadata - you know how to do that, right?). Use another tMap component, feeding into a tContextLoad widget. tContextLoad always has two columns in its schema, so map the output of the tAggregateRows to the "value" column and enter the name of the variable in the "key". In this example, my context variable is called MyList
Now your list is loaded as a text string and stored in the context variable ready for retrieval. So open up a new input and embed the variable in the sql code like this
"SELECT distinct MY_COLUMN
from MY_SECOND_TABLE where the_selected_row in ("+
context.MyList+")"
It should be as easy as that, and when I whipped it up it worked first time, but let me know if you have any trouble and I'll see what I can do.

How to create a flatfile from a series of tables in Access?

I have a series of tables in an Access 2007 database. I am trying to find a way of outputting a flat-file to an excel spreadsheet that combines all of the tables so that each row of the flatfile represents a unique combination of the table rows from each table.
For example, these tables:
Would combine to make this output table:
The challenges I'm facing are:
The 'input' tables can vary in number of rows and columns, as well as quantity
The total number of rows in the final output table can get quite large (200,000+ rows)
I know Excel and VBA (in Excel) well but almost nothing about Access
Is there a way to do this in Access? Is there some native functionality in Access that I'm completely overlooking? Any pointers (even if it's "you need to read into X and Y") would be greatly appreciated!
Thanks,
Adam
As noted above:
Create a new query. Select your 3 tables as the data sources. If desired, set up joins between tables by dragging a line between a field in one table to a field in another. Without joins you will get a Cartesian Product ... every from 1st table paired with every row of 2nd table, and then each of those combination paired with every row of 3rd table. Select the fields you want included in the result set. When the query returns what you need, save it and give it a name. Then you can export that named query to Excel.
If the table is large, you could hit Excel's row / column limit though.

Copying a record in VBA

I work with a product that comes in a 2000lb sack and placed on a pallet. When this product is made it has many different elements that are tested and each test has a field that the numerical data is placed in. Each of these records of tests are then assign a number, for example, L20444.
Now we have the ability to take that 2000lb sack and convert it into 80 20lb bags. Only 40 20lb bags can fit on one pallet, taking now the one pallet L20444 and making two pallets that have the number L20444. This causes a problem with inventory because the number L20444 can only be assign one warehouse location, not two.
So my bosses what to create a number that is almost the same, but different enough to place the second pallet in the warehouse. The second pallet will now be L20444B. It will still have all the same tested numbers and is a "copy" of the original L20444.
My question is can I take the record L20444 and copy all the data for that record and then save it as L20444B so that it can be placed in the warehouse.
So is it possible for VBA to copy a record, rename it, and then save it in the same database as a new record?
Any help would be appreciated, Thanks.
If I'm reading you right it sounds like you want a SQL statement to create a new record.
You're using Microsoft Access? I would recommend first creating a query that does this in the query editor. It will be an Append query, something along the lines of:
INSERT INTO TableA ( ID,col1, col2 )
SELECT [ID] & "B" AS NewName,col1, col2
FROM TableA
WHERE (([ID]="L20444"));
Test this first to make sure it's doing what you want, and make "L20444" into a parameter ([OldID], or something). Then add some code in your VBA script that executes this query. It should pop up asking you for OldID when you run it.
Then you'll need to learn how to execute parameterized queries from VBA.
Something like this:
Set qdf1 = CurrentDb.QueryDefs("myQuery")
qdf1.Parameters("OldID") = theOldID
qdf1.Execute
Not tested, search VBA help for QueryDefs if my syntax isn't quite right.
Why don't you create a new table, which tracks the location of the two pallets (and the new number(s)), which links back (with a foreign key) to the single record for the stock in the original table?
That should work, and will avoid what will otherwise become a nightmare of redundant data.