General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 27th, 2004, 10:43 AM
MrBullwinkle MrBullwinkle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Wild and Wonderful, West Virginia
Posts: 27 MrBullwinkle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to MrBullwinkle Send a message via AIM to MrBullwinkle Send a message via MSN to MrBullwinkle Send a message via Yahoo to MrBullwinkle
Unhappy Select Top (Subquery)

I worked with some developers awhile back for a company and recently found a problem with an update button, which caused data to be copied as many as 250 times. A table (measurements) holding data should have around 25,000 data points now has well over 500,000.

One table [Tasks] contains info about a block to be tested, such as an ID,desc, quantity to test.
(taskID,Name,description,dimension1,quantity1, dim2,q2,d3,q3)

There is a linking table [Pieces] which contains the fields [taskID] and [pieceID], because each taskID might have multiple pieces tested with many data points per test.

Measurements table contains: pieceID, measurementID(AutoNum to make unique),dimNum, dataPoint.

Each block can be tested in 3 dimensions thus there are 3 different quantity to test fields, but generally its just on the first, so I'm assuming the query just gets joined to another for each dimension.

The question
Basically I want to Select Top (quantity to test from tasks) for each pieceID in Measurements. Select All from measurements.

Is this enough info/clear enough?
Thanks in advance

Reply With Quote
  #2  
Old December 27th, 2004, 01:57 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Perhaps something like this would work?

Code:
SELECT * 
FROM tasks, pieces, measurements
WHERE tasks.taskid=pieces.taskid
    AND measurements.pieceid=pieces.pieceid
LIMIT 1
ORDER BY tasks.quantity DESC

Reply With Quote
  #3  
Old December 28th, 2004, 07:45 AM
MrBullwinkle MrBullwinkle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Wild and Wonderful, West Virginia
Posts: 27 MrBullwinkle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to MrBullwinkle Send a message via AIM to MrBullwinkle Send a message via MSN to MrBullwinkle Send a message via Yahoo to MrBullwinkle
The problem I have is that there are "duplicates" in the measurements table. Like I should have 6 measurements. 6 were taken, then an error in my code caused copies to be made. So now I have a series of 6 data points repeating every 6 rows. The data points MIGHT be the same though (I might have 2 of the same data, but are not copies). So Top 6 would work, but sometimes its 10,12,24 measurements. The number of measurements is stored in the tasks.quantity1 field, but I can't get how to get that dynamic number out, I think in a subquery?

Reply With Quote
  #4  
Old December 28th, 2004, 10:26 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
so you're more interested in deleting the wrong data, than displaying the right data...

I assume you don't have a backup of the database?
or a script to re-write the codes that is in the database?

If you're using a programming language to interact with the database, you might want to make a simple program to interact with database. Perhaps display every row in the table, with chekcboxes, then you can quickly check the fields you want deleted...

depending on your database system, you might already have a program that will connect and present data in this fashion already.



On second thought, are you positive there's 10,000+ measurements in the table?
Perhaps you just aren't joining the tables properly...

try a simple SELECT COUNT(*) FROM measurements [and each other table]...

If you were to use multiple tablse in the FROM clause, but not join the tables proplery in the WHERE clause, it will display a LOT more results than you expect.

Reply With Quote
  #5  
Old December 28th, 2004, 10:38 AM
MrBullwinkle MrBullwinkle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Wild and Wonderful, West Virginia
Posts: 27 MrBullwinkle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to MrBullwinkle Send a message via AIM to MrBullwinkle Send a message via MSN to MrBullwinkle Send a message via Yahoo to MrBullwinkle
Post

  • I have full access to the database. I can make backup copies. (the problem wasn't discovered for a few weeks when the database went from 2meg to 10meg. The problem with the program that adds data was fixed, but its not possible to restore a backup copy of the database)
  • The database is in MSAccess2003.
  • Yes, I want to delete the bad data ( I was going to make a query just like the table, so I can empty the table, then copy the query into it, that way I can see and make sure the query didn't delete anything incorrectly)
  • I was hoping to just write the query in Access, but I have VB6 and All the Office2003 programs at my disposal(might be fun to do it though Excel)
  • I just did a count, and theres 378,492 records in the measurements table(no joining here required). I would over-estimate there should be no more then 10,000 at this point.
  • I can actually see the data points, in most cases there should be 6 measurements per pieceID, and theres between 350-500.
I wish I could be more clear on my explaination of the tables and relationships, I'm trying to condense the problem and consequently leaving out info.

Reply With Quote
  #6  
Old December 30th, 2004, 03:02 PM
MrBullwinkle MrBullwinkle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Wild and Wonderful, West Virginia
Posts: 27 MrBullwinkle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to MrBullwinkle Send a message via AIM to MrBullwinkle Send a message via MSN to MrBullwinkle Send a message via Yahoo to MrBullwinkle
Would a relationship chart help?
Attached Images
File Type: gif RelationshipChart.gif (26.3 KB, 1652 views)

Reply With Quote
  #7  
Old January 5th, 2005, 07:17 PM
MrSnrub MrSnrub is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 3 MrSnrub User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 56 sec
Reputation Power: 0
I'm not sure if this will help you, but from what I understand your problem to be, this is the approach I would take to resolve it.
  • Back up the database
  • Write some code (perhaps a macro inside Access would be easiest) that does the following:
    • Creates a multi-dimensioned array for storing the details of each Measurement. It would need to be of size 10,000 x 3 for each of the 'unique' records. The 3 is to store the pieceID, dimNum and dataPoint for each record.
    • Reads through every record in the Measurement table.
    • For each record in the Measurement table, checks to see if it is present in the array (ie. compare pieceID, dimNum and dataPoint, not measurementID as this is your primary key, with what is currently in your array). If not, add it to the array. If it is present, ignore it and then move to your next record. When you have finished looping through every Measurement record you will be left with an array of all the unique records that you should have in the database without the duplicates.
    • Deletes every record in the Measurement table.
    • Loops through your array adding every record to your Measurement table. This will populate your Measurement table with all of the 'correct' records.
  • Run the code and this should update your database with the 'correct' data.
The only problem I can potentially see with my solution is if the MeasurementID is referenced by any of your other tables. My proposed solution will involve generating new measurementID's when you rewrite the correct records back to the table. Thus, if any other tables reference the original measurementID's then these references will be invalid and you should approach the problem differently.


I hope this helps.

Reply With Quote
  #8  
Old January 6th, 2005, 08:54 AM
MrBullwinkle MrBullwinkle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Wild and Wonderful, West Virginia
Posts: 27 MrBullwinkle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to MrBullwinkle Send a message via AIM to MrBullwinkle Send a message via MSN to MrBullwinkle Send a message via Yahoo to MrBullwinkle
I thought about running code to solve the problem, I was just know that a query would do the trick so nicely. I just don't know how to get the subquery to check how many measurements to take for each pieceID and then to run that query for the SELECT TOP for each record in the tasks.

Reply With Quote
  #9  
Old January 6th, 2005, 12:15 PM
MrBullwinkle MrBullwinkle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Wild and Wonderful, West Virginia
Posts: 27 MrBullwinkle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to MrBullwinkle Send a message via AIM to MrBullwinkle Send a message via MSN to MrBullwinkle Send a message via Yahoo to MrBullwinkle
I finally figured it out. There is a help file in Access relating to "Delete Duplicates" that explains how to perfectly. I just never realized that my timestamp was the key to finding the duplicates. I didn't need much of a query at all, except for a simple append query mentioned in the help file. Its nice when F1 works.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > Select Top (Subquery)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT