Efficient cross-table mySQL queries in Visual Basic - mysql

I am currently working on a report-generating application (in Visual Basic) for a pre-existing database (in mySQL), in which I need to submit queries across multiple tables to access all of the information needed; however, my experience with this sort of project is limited. I am hoping someone can point me toward an efficient method of achieving this.
In the initial report, I need data from 3 tables.
Table 1) 'invoice' - table structure contains a date, an invoice number, and a customer number (and other non-pertinent columns)
Table 2) 'transaction_data' - table structure contains an invoice number, a billing code, and an item description (and other non-pertinent columns). Each row of the table contains a single line-item from a single invoice (so there can be several rows in this table containing the same invoice number).
Table 3) 'customers' - table structure contains customer number, name, address, phone (and other non-pertinent columns).
I need to be able to search 'invoice' based on dates, to get a list of all invoice numbers within the specified time frame (and their corresponding customer number). I then need to take that list of invoice numbers, and search 'transaction_data' for each row that contains one of the invoice numbers, and check for a specific billing code. If the billing code does not exist, I need to use the customer number (obtained during the invoice search) to put together a list of invoice number, customer name, address, phone number.
This can be accomplished fairly easily by populating an array variable utilizing for/while loops, but will require multiple queries across separate tables... of which 'invoice' and 'customers' have 20k+ entries and 'transaction_data' table has over 100k entries. Surely, this is not the most efficient manner of compiling said data.
Can someone please direct me as to how the query SHOULD be structured efficiently? Thanks in advance for helping a database noobie!

Look up JOINS in the MySQL manual. You would join the three tables together and with a WHERE clause get the specific rows you need that match the criteria you are searching for. No loops needed.

Related

how to use multiple tables without duplication in tableau

I've trouble understanding how this should work...basically I've 2 main tables, in one I've Revenues, in another Costs.
Revenues table has fields as: P&L (string), Category (string), Products (string), Sold (int), invoiced (int), delivered (int), date (date).
Costs table has: P&L (string), Category (string), Products (string), Costs (int), date (date).
I'd like to use tables together to perform various calcs like margin, for example, at any level (total margin, which means total revenues - total costs, or at Category level for which I should be able to filter any category I have and perform the calc and so on).
Problem is, any tentative I've made to use relations or join, resulted in duplications.
The only workaround I was able to perform so far is to leave revenues table as it is, and create many Costs table, 1 for field basically (table1 with category and costs plus date, table2 with products, costs and date etc.). Joining Revenues with one of these tables seems to work but, in this way, I'm not able to create a wider view (one goal is to make a big table in the viz where we could read at once all the data). Plus, another problem I 've seen it appear doing this workaround is that, if I want to split by date costs, but I use the date column from the revenues table, even if the date is the same (I've done a copy/paste between tables basically), tableau doesn't recognize the date correctly, so to split costs, I've to use costs'date column, and to split revenues, I've to use revenues' date columns, which is frankly a pain...
So my question: how could I merge the 2 tables in one, or anyway how could I put all the data together in a working table to perform any kind of calcs,and also how could I use just 1 column for date that works for all the date altogether?
I've upload a file here to understand better what I'm trying to combine. Thank you guys
Data file
ps.: seems that tableau is using sql behind for these tasks so probably someone skilled in this kind of problem in sql could also help...for this I 've tagged sql as well, thanks
You need to UNION those 2 tables together, but are they really in Google or you just did that to demo it here?
If you're using Excel - both Revenue & Cost must be different sheets in the same XLS file
If you're using CSV - both Revenue & Cost must be different files (hopefully in the same folder)
I would really hope that you're using a database (some form of SQL), but either of the above options, UNION the data and it will work the way you expect :)

Power Pivot 2010: DAX, Dynamic Groups with Overlap

How do I use Power Pivot to summarize data in groups which are defined in a separate, non-relatable table
I'm analyzing a database that has the following tables:
Sales
Store
Category
Units
Sales
Stores
Store
address
etc
StoreGroups
Store
Group
A store can be in multiple groups (i.e. store B762 is in NW group & control_group) hence the StoreGroup table - where the two fields together make the primary key. Therefore, I can't relate StoreGroups to my Sales table, because both have duplicate Store values.
Right now all stores are being reported in each group:
PivotTableScreenshot
to confirm, if a store is in two groups, its sales should get counted for BOTH groups i.e. control group and NWRegion.
I've tried to adapt this DAX example mentioned below but have not been successful:
http://www.daxpatterns.com/dynamic-segmentation/
You have a many-to-many relationship between stores and groups.
You should be able to create a relationship from the store in StoreGroups and the Store in Stores (StoreGroups is a bridge table).
If you can post a link to some sample data, that would be helpful.
After doing that, you can read start to read about writing DAX formulas for many-to-many scenarios here. Be sure to also read in the comments, especially the one from Marco Russo.

MySQL find lowest price of multiple products in multiple tables

I have a database called Pricelist. In it there are several tables eg. Store1, store2, etc. Each has a list of the products. Most products exist in each store but some only exits in one or two stores. I want to have something that I can run within SQL (stored procedure?) That will find the lowest price of a product by checking the price from each store and then when it finds the lowest price, it will get that price, along with the UPC,Description, and other columns and put all that information in a final table called BestPrices. Then it do it all again for each product in every store. So when it's done. every product from all the stores should be listed once in the BestPrices table with the lowest price and the additional information from the other columns I chose. Is this possible? I know it's a lot. Thanks in advance.
You can create temporary/buffer table to collect data from tables store, then you can query from that table to get the lowest price.
If all you want is display the best prices, then an SQL View should do the trick. A View is enough to
find the lowest price of a product by checking the price from each
store ... along with other columns and ... do it all again for each
product in every store.
However, an SQL View is rendered at runtime and hence, you do not have the values stored in a table, per se. That is, the best prices are calculated as and when you invoke the view
If you want to have the values stored for future viewing, then you shall need a Stored Procedure which shall insert the values into a physical table.
In the case of using a procedure, you should take care as to what should happen to the existing data when you insert into the table.
Also, your post doesn't include details regarding the actual table structure or the common identifier for the product across all the tables. Those details can greatly influence the design and performance of the View/Procedure.

Removing the entries in a comma separated field based on the data of another table

First off I am using MySQL 5.0.96. (always forget to mention that)
I have two tables(TMP_VIN_STOCK and newvehicles) I load TMP_VIN_STOCK with our current inventory of vehicles daily.
newvehicles is our currently listed vehicles on our website table.
I want to find results in newvehicles that are NOT in TMP_VIN_STOCK. (So I can remove them from website because we sold them)
TMP_VIN_STOCK.name and newvehicles.stocknum are each tables stock number column.
TMP_VIN_STOCK.name has only single values of a stock number
newvehicles.stocknum can have multiple values separated by commas in it. (Same exact vehicle just multiple stock numbers)
Example:
TMP_VIN_STOCK table
- K12049
- S12040
- T12020
newvehicles table
- S12018, S12039, S12040
- Y13068, Y13093
- T12020
How can I find out what stock numbers are not in TMP_VIN_STOCK but are in newvehicles?
Your problem beautifully demonstrates why tables in relational databases should be normalized.
If you want to keep this design then you have to do something like this:
loop over the newvehicles rows:
loop over the items in the comma separated field
check if the item does not exist in TMP_VIN_STOCK
Not to mention that mysql does not have a function to split a comma separated list and
let alone the pain you should go through to remove the ones you find from the comma separated list.
You should definitely redesign your database. Have a table for vehicle data and another one for stock data and link them with foreign keys. That way you don't have to go through all this pain. You can retrieve the data you want with a simple query.
Also if an entry is not in the stock, its data should not be deleted but the count should be zero or it should be flagged as out of stock.
This question has some details that would let you split the values in newvehicles table and let you populate a (temp) table with the results then a not exists or not in query would give the results you need.
Alternatively you could keep the previous days load of TMP_VIN_STOCK (maybe add a date field into the table - then you could compare what you loaded today with what you loaded yesterday

Making a query that combines 1 table and data from another associated with it on same line

Current:
Table 1
Dbase(table with account information)
-id……
Table 2
Phonenumbers(table with phone numbers associated with accounts.
Id, maindbaseid, phone type, phone number
Current Rusult
Id1,phonetype1,phonenumber1
Id1,phonetype2,phonenumber2
Id1phonetype3,phonenumber3
Wanted result
Id1,phonetype1,phonenumber1,phonetype2,phonenumber2,phonetype3,phonenumber3.
I am trying to export data from our MySQL database based on 2 tables. One is in relation to the other based in the id column and main database id column. The one table is information on an account, and the other is phone number associated with that account. The data I am trying to export needs to be the information from the account table with each phone number associated with that account in rows next to each other I tried an inner join where maindatabaseid and the column maindatabaseid from the second table matched but it only shows the account duplicated as many times that there is a different phone number. Any suggestions would be appreciated, I am a beginner at MySQL so if you could explain things simpler it would help.
This is one of the most simple relationships. All you need todo is setup a "Foreign Key" relationship, from say the "phone numbers" table to the "accounts table" That way when you query the phone number table, you will then be able to access all the information from the account related to that phone number. (or the other way round)
Please see this documentation on Foreign Keys and how to use them, depending on whether you are using a raw sql connection or an ORM would depend on the best approach on performing your query, the documentation link provides information on using the MySQL console.