Follow along with the video below to see how to install our site as a web app on your home screen.
Note: this_feature_currently_requires_accessing_site_using_safari
@Cespian
The cross join query i gave does not take very long on oracle.
On one on data conversions i previously did, doing select on on the customer table took 18s. When running the crossjoin with a numbers table of 20 numbers the time taken to retrieve 85s. running it with 10 numbers took 44s.
Please provide table 2.Hey Guys
Hoping there are some SQL Gurus here. I need some assistance (MySQL specifically):
Context; I have 2 tables:
Table 1
--------
Customer_id | Entries
1 | 3
2 | 4
3 | 2
Basically, I need to duplicate the Customer_id in Table 2 for each Entry they have. Hence Table 2 should look like result set below after the insert:
Customer_id
1
1
1
2
2
2
2
3
3
How would I go about scripting this? Unfortunately I do not have the luxury of using another language (Like creating a Loop in PHP would have been perfect) as I have to do this via command line. Considering that there are 300K+ rows in Table1, manually typing INSERT INTO table2 VALUES X entries is impossible. And yes, I am aware that Table2 will most likely exceed a billion rows after the insert is complete (I have over 40Gb available).
Another issue I may have is that I will be doing this on a production server and may lock up Table1, but I will create a backup of the table first and work from the backup.
Thanks in advance
Please provide table 2.
If table 2 has 3 lines for customerID 1 then it should be a simple left outer join.
can you do:Hey Chistos
There is no data in Table2 bud. My problem is that I am trying to populate Table2 with data effectively. Example of how Table2 should look after a viable INSERT mechanism is written, is provided in the OP.
As explained to KimVapeDashian: Table1 holds a unique customer id and a count of items bought per customer id. I need to insert the customer id as many times as the quantity of items/entries as stated in Table1 into Table2 (which is on average about 22 times). The reason I cannot simply write individual INSERT statements is because Table1 holds 300k+ records.
Hope that makes sense.
Thanks
Your headache is my bread and butter and vape gearMy head hurts
Good luck there @Cespian !
Hope you don't fry any components
I thought I had Excel issues - until I read this thread...
can you do:
select Entries from Table 1
group by entries
If your entries go from 1 to 20(I.E Not too many of these) I have an idea that will save you buttloads of time but requires a "setup" of a table.
My head hurts
Good luck there @Cespian !
Hope you don't fry any components
I thought I had Excel issues - until I read this thread...
Sure... that ill have to do tomorrow. I only brought a 5k sample home. Thanks bud.
Should take a few seconds to try it on your test data provided the data is randomised and not the first 5k recordsSure... that ill have to do tomorrow. I only brought a 5k sample home. Thanks bud.
@Cespian, the logical approach here will get you a query that runs for hours.
If
Should take a few seconds to try it on your test data provided the data is randomised and not the first 5k records
Sounds promising.
I'll give you assistance once junior is sleeping. Can't really do much without him jumping on me right now.
Create a table with the entries I. E entry amount.No urgency bud. Im leaving for the airport as soon as I get my juniors to bed as well. So will be out of commission until at least 11ish. Got until Friday next week to come up with a solution
Create a table with the entries I. E entry amount.
So it will look like
ENTRY AMOUNT
1
2
2
3
3
3
4
4
4
4
You can create a loop to do this for you.
Should run in under 5 mins to create the table.
Then do a left join in the table with Table1 on entry amount = client entries
This should give you what you are looking for and 300K records in a select join should take under a minute if not 10 seconds.
If you want help writing the query let me know.
Btw here is my adapted excel macro, to replicate the numbers
Sub KimCopy()
Dim lRow As Long
Dim RepeatFactor As Variant
lRow = 1
Do While (Cells(lRow, "A") <> "")
RepeatFactor = Cells(lRow, "A")
If ((RepeatFactor > 1) And IsNumeric(RepeatFactor)) Then
Range(Cells(lRow, "A"), Cells(lRow, "A")).Copy
Range(Cells(lRow + 1, "A"), Cells(lRow + RepeatFactor - 1, "A")).Select
Selection.Insert Shift:=xlDown
lRow = lRow + RepeatFactor - 1
End If
lRow = lRow + 1
Loop
End Sub
Populating column A, with numbers 1 - 6 as a start
View attachment 53919
This is then the outcome of the macro data
View attachment 53920
Create a table with the entries I. E entry amount.
So it will look like
ENTRY AMOUNT
1
2
2
3
3
3
4
4
4
4
You can create a loop to do this for you.
Should run in under 5 mins to create the table.
Then do a left join in the table with Table1 on entry amount = client entries
This should give you what you are looking for and 300K records in a select join should take under a minute if not 10 seconds.
If you want help writing the query let me know.
Simple select from customer first and then left outer join to entries.Hey guys,
Sorry for the late response... been stuck in meetings
sorry for my doffness, but how would I script the loop in SQL? I am very much familiar on how to do this in VBA and PHP... If I understand correctly:
1. Create "entry_amount" table
2. Insert each "entries" value into "entry_amount" table multiplied by itself. eg. If customer_id 1 has 3 entries, and customer_id 16 has 3 entries, the number 3 needs to appear 6 times in the "entry_amount" table
3. LEFT OUTER JOIN table1.entries on entry_amount.entries
The entries in Table1 are not unique, only the customer_id is unique. If the entries were unique, this would work seemlessly. Going to paint a clearer picture below:
Snippet of Table1 below (data not real but accurate representation):
View attachment 53971
As you can see, both Customer 1 and Customer 4 has 4 entries, and Customer 3 and Customer 5 has 2 entries each.
Even if I expand the entries to look like this:
View attachment 53972
And do a LEFT JOIN with TABLE 1, SQL will bring only the first found match, meaning that; if there were 10 customers with 4 entries, the first Customer will be selected for each unique entry. Whereas I need to see the following:
View attachment 53973
Please slap me if I am reading your suggestion incorrectly.
Thanks
@Christos
I hear you 100%!!
In my line of work, I work far more in Excel and often pull data from my DB. Excel is bae for me!
Over the years, I noticed that Excel is very misunderstood. First mistake is that people use it as a DB (I have honestly discovered one with 15 sheets, holding roughly 600K rows and at least 5 columns each)... It is honestly one of the most versatile tools I've ever discovered if used correctly. I do design work, presentations, change delimiters in txt files, schedule reports (to name a few) from Excel. I love it. Every single one of the objects below is created in Excel:
View attachment 53977
So yeah, Excel is Bae for me too!
Next time im in CT or you come up to JHB
Just saw location.