Solved

Pull data from two separate tables

Posted on 2010-11-08
7
332 Views
Last Modified: 2012-05-10
Ok, I  know there is a cleaner way to do this and I am just too new and too dumb to figure it out.  Time for some help from the experts.

I have a SQL Database with multiple tables.  Most of the tables are just lists in order to connect the two main data tables with like identifiers.  The problem I am having is that I can't seem to pull the data from both tables correctly to correlate one with another.

The first table pulls qty and sales by zip code, item number and thus are filtered by the lists in order to get the market that correlates with the zip code and variety that correlates with the item number along with the weight multiplier so as to convert qty to pounds.  I have that down and working well.

When I try and add the second table that already has the correct market, the correct pounds and correct variety in it - but I still need to pull the sales and pounds out to compare the two data sets is when I get into trouble.  I can't seem to get the two table to pull the correct information.

In the example it should basically return 52 rows - one for each market with the correct dollars and pounds from one table (MarketShare) and dollars and pounds from the other table (CognosData).

Using MS SQL Server 2005 and Visual Studio 2005

Can anyone help me get this right?
SELECT     Markets.[IRI Market], Varieties.Variety, SUM(MarketShare.[POS Qty] * Varieties.Weight) AS WMTlbs, SUM(MarketShare.[POS Sales]) AS WMTDollars, 

                      SUM(CognosData.Dollars) AS IRIDollars, SUM(CognosData.Pounds) AS IRIlbs, 

FROM         MarketShare INNER JOIN

                      Markets ON MarketShare.[Zip Code] = Markets.[Zip Code] INNER JOIN

                      CognosMonths ON MarketShare.[WM Week] = CognosMonths.[Walmart Weeks] INNER JOIN

                      Qualifier ON MarketShare.[Store Nbr] = Qualifier.Stores INNER JOIN

                      Varieties ON MarketShare.[Item Nbr] = Varieties.[Item No] INNER JOIN

                      CognosData ON Markets.[IRI Market] = CognosData.Market

WHERE     (Qualifier.Qualifier = N'Yes') AND (Varieties.Variety = N'Red') AND (CognosData.Variety = N'Red') AND (CognosData.Year = N'2009')

GROUP BY Varieties.Variety, Markets.[IRI Market], CognosData.Market, CognosData.Variety, CognosData.Year

ORDER BY Markets.[IRI Market]

Open in new window

0
Comment
Question by:austintre
  • 4
  • 3
7 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 34088421
rather than 52 rows what are you returning?
what is missing/added?
Need a bit more info on the results before being able to diagnose the sql
0
 

Author Comment

by:austintre
ID: 34088439
Basically it is pulling multiple rows from the 'CognosData' table for every one row of 'MarketShare' so I am getting hundreds of rows with the same numbers for one table and different numbers for the other data.

From what I can tell I have the first table rolling up correctly and the second row is not rolling up to match if that makes sense.
0
 
LVL 29

Expert Comment

by:QPR
ID: 34088503
basically this will mean that the join is being satisfied > once for the INNER JOIN part.
Can IRI Market have more than one market in the cognos table?
If so then you will receive a row for each "true"
One thing to do is to start your query from scratch adding one join at a time until you get the undesired results.
This way you know exactly where the problem lies and can focus your attention there
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:austintre
ID: 34088702
Believe me when I say that I have started from scratch more times than I want to admit to.  Asking a question on this site is my last resort.  Basically looking for someone who can look at the code and tell me a better and more correct way to write it.
0
 
LVL 29

Expert Comment

by:QPR
ID: 34088724
that would require an intimate knowledge of the data.
As I said... Can IRI Market have more than one market in the cognos table?
0
 

Author Comment

by:austintre
ID: 34088785
The IRI Market is a list of the markets by zipcode.  That way I can reference the market by zipcode for the 'marketshare' table.  Cognos table has five years by month of data that is broken down by market, variety, and year.

So columns in marketshare are
StoreNbr, State, Zipcode, Week, ItemNbr, Qty, Sales

Columns in cognosdata are
Market, Variety, Month, Year, Dollars and Pounds

So I need to combine the two tables so the output looks like:
Market, Variety, MarketShare.Pounds, MarketShare.Dollars, CognosData.Pounds, Cognosdata.Dollars

In order to compare apples to apples - I have one table that finds the market by zipcode, another table that finds the weight and the variety by the marketshare.itemnbr and the week rolls up to months.

Does that help at all or is that just causing more confusion?

0
 
LVL 29

Accepted Solution

by:
QPR earned 500 total points
ID: 34088816
so Markets.[IRI Market] = CognosData.Market
is returning too many records as the join criteria is being satisfied many times?
In your WHERE clause you specify red and 2009 on the cognos table, would this limit the returned rows if this criteria was part of the join rather than the where?

In other words
INNER JOIN CognosData
     ON Markets.[IRI Market] = CognosData.Market
    AND (CognosData.Variety = N'Red')
    AND (CognosData.Year = N'2009')
WHERE ......

you will need a where to further define the join (or use some other method such as subqueries) in order to remove the duplicates

0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now