473,396 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

My query has duplicate rows and runs forever

I need to calculate which products were purchased by a specific customer in 2014, using Microsoft SQL Server Management Studio 2008.

I am trying to combine data from 3 semi-related tables:
Customer_List
Product_List
Product_Sales

The desired result is as follows:
5 Columns

Expand|Select|Wrap|Line Numbers
  1. Customer_Name Product_Name Product_IDNumber Sales Quantity
  2. ============= ============ ================ ===== ========
  3. Store 1           Product 1   Product ID #1    $0.00     5
  4. Store 1           Product 2   Product ID #2    $1.00     3
  5. Store 1           Product 3   Product ID #3    $2.00     8
  6. Store 2           Product 1   Product ID #1    $2.00     2
  7. Store 2           Product 2   Product ID #2    $1.00     4
  8. Store 2           Product 3   Product ID #3    $5.00     7
  9.  
The current query set-up:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. Product_List.Product_Name
  3. Product_List.Product_IDnumber
  4. Product_Sales.Sales
  5. Product_Sales.Quantity
  6. FROM Customer_List,Product_List,Product_Sales
  7. WHERE Customer_List.RandomID = Product_Sales.RandomID AND
  8. Product_Sales.Code00 = Product_List.Code00 AND
  9. Customer_List.Customer_Name like '%Acme%' AND
  10. Product Sales.Year = 2014
  11. Order by Customer_List.Customer_Name
The query results in the following:
Store 1 Product 1 Product ID #1 $0.00 5
Store 1 Product 2 Product ID #2 $0.00 5
Store 1 Product 3 Product ID #3 $0.00 5


I ran it last night at 5pm. This morning, the query was still executing.
There is not that much data for the query to run that long.

I am new to SQL.

Unfortunately, I cannot be more specific about the data; however, this example accurately describes what I am trying to accomplish.

Any help is greatly appreciated!
Jun 2 '15 #1
4 1820
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code or formatted data.

A few questions:
  • What is RandomID?
  • What is Code00?
  • Is that the actual query? Because it should error out. You have the table name spelled wrong on line 10. It would be better if you did a copy and paste of the SQL rather than retyping it.
  • Aside from the like operator, there's nothing that should slow it down too much. Even the like operator doesn't slow it down that much.
  • How much data is in each of those tables?
  • Are there any locks on those tables? If you are okay with dirty reads, use a with(nolock) so you can get the data even if the table is locked.
Jun 2 '15 #2
Thanks for the response, Rabbit. Sorry about editing errors.
RandomID and Code00 are columns I included to link the tables, but I don't want them in the query results. Was that unnecessary?
What are table locks? Do I insert the phrase "with(nolock)" into the query?
Jun 2 '15 #3
Rabbit
12,516 Expert Mod 8TB
They're necessary, just wanted to make sure they're correct. You put the with(nolock) after a table name if you want to bypass locks on that table and don't mind reading slightly older data.
Jun 2 '15 #4
Rabbit, I got help from a colleague. My script was using the wrong data. Once the correct tables were linked, correctly, the problem was solved. Thanks for your time.
Jun 3 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Russell | last post by:
I'm using MySQL 4.1.1 I've inherited a database which has some (almost) duplicate rows. The databse is like this. userID userPosition userDepartment
1
by: Asha | last post by:
greetings, does anyone have any idea how to delete duplicate rows in a dataset gracefully (the fast and easy way)
7
by: Jon Maz | last post by:
Hi, I have a MySql problem I hope someone can help me with. I'm trying to run an update on a linking table, the update is running into a Primary Key constraint violation, and in my workaround...
4
by: muzu1232004 | last post by:
Is there any query which will delete exactly one of the duplicate rows in a table and retain only one ?
5
jamesd0142
by: jamesd0142 | last post by:
My manager and I where looking at some complex code to eliminate duplicate records in a database table. then it hit me how its done easily... so i thought i'd share it... In English:...
1
by: Phil Latio | last post by:
I have a number of spreadsheets, each with between 1000-6000 rows (each row is a property) and they all need to be combined into a single database. Each spreadsheet contains slightly different...
13
by: Neil | last post by:
I'm running an update query in SQL 7 from QA, and it runs forever. Has been running for 20 minutes so far! The query is quite simple: update a single field in a table, based on a join with another...
4
by: chand basha | last post by:
Hi, how can i delete the duplicate rows and keep the original row in the mysql date. rs = "select count(email) as mail from import_address_book group by email having mail > 1 rs.each do...
1
by: lilmisspink07 | last post by:
Hello, I am trying to pull a list of sample counts by test. I only want 1 row per test result so that I can build a report in Cognos showing the rate of a particular result as a percentage of the...
13
by: chintan85 | last post by:
I have a table1 id col1 col2 1 NC_001A>T 5 2 NC_001C>G 4 3 NC_001G>C 3 4 NC_001_98_G>C 1 Now, I want to have max col2 value for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.