Solved

How to get the join to work ?

Posted on 2013-01-04
3
338 Views
Last Modified: 2013-01-13
Hi!

Have this sql-code:

      SELECT distinct
           CUST.ABC_FORECASTGROUPID,
           FORCAST.DESCRIPTION,
           SALE.ITEMID,
           sum(SALE.QTYORDERED),
           DATACELLS.cellvalue,
           cast(year(SALE.CONFIRMEDDLV) AS varchar(4)) + cast(DATEPART(week,SALE.CONFIRMEDDLV) as varchar(2))
        FROM
            [hgf-sql01].[hfgh-axdb5-drift].[DBO].[SALESLINE] AS SALE
        INNER JOIN
            [hgf-sql01].[hfgh-axdb5-drift].[DBO].[CUSTTABLE] AS CUST
        ON
            (
                 ((SALE.CUSTACCOUNT = CUST.ACCOUNTNUM) AND
                 (SALE.DATAAREAID  COLLATE DATABASE_DEFAULT = CUST.DATAAREAID  COLLATE DATABASE_DEFAULT))
            )
        INNER JOIN
            [hgf-sql01].[bizview_hfs].[dat].[data_cells] AS DATACELLS
        ON
            (
                ((CUST.ABC_FORECASTGROUPID COLLATE DATABASE_DEFAULT = DATACELLS.ws_name COLLATE DATABASE_DEFAULT) OR
                (CUST.ABC_FORECASTGROUPID=' ')) AND (DATACELLS.version_id='PU4012') AND (DATACELLS.valtype_id='ANT') AND
                (DATACELLS.timeunit_id='ISOWEEK') AND (DATACELLS.timeval_id=cast(year(SALE.CONFIRMEDDLV) AS varchar(4)) + cast(DATEPART(week,SALE.CONFIRMEDDLV) as varchar(2)))
            )
            
        INNER JOIN
            [hgf-sql01].[bizview_hfs].[dat].[data_dimensions] AS DIMENTIONS
        ON
            (
                ((DATACELLS.dim_cluster_id  = DIMENTIONS.dim_cluster_id) AND
                (DIMENTIONS.obj_id='item') AND (DATACELLS.client_id  = DIMENTIONS.client_id))
                  
                   
            ) 
            
        INNER JOIN
            [hgf-sql01].[hfgh-axdb5-drift].[DBO].[ABC_FORECASTGROUP] AS FORCAST
        ON
            (
                 ((FORCAST.FORECASTGROUPID = CUST.ABC_FORECASTGROUPID) AND
                 (FORCAST.DATAAREAID = CUST.DATAAREAID))
                  
                   
            ) 
 
group by
    CUST.ABC_FORECASTGROUPID,
    FORCAST.DESCRIPTION,
    SALE.ITEMID,
    DATACELLS.cellvalue,
    cast(year(SALE.CONFIRMEDDLV) AS varchar(4)) + cast(DATEPART(week,SALE.CONFIRMEDDLV) as varchar(2))

Open in new window


When i run this query, it takes about 25 minits to complete ?

 [hgf-sql01].[hfgh-axdb5-drift].[DBO].[SALESLINE] AS SALE
-> contains of 1039137 records

   [hgf-sql01].[hfgh-axdb5-drift].[DBO].[CUSTTABLE] AS CUST
-> contains of 18045 records

But the strange ting is that query
always return more then 1 million records ???

Tryed to change this lines

       FROM
            [hgf-sql01].[hfgh-axdb5-drift].[DBO].[SALESLINE] AS SALE
        INNER JOIN
            [hgf-sql01].[hfgh-axdb5-drift].[DBO].[CUSTTABLE] AS CUST

Open in new window


TO:
       FROM
            [hgf-sql01].[hfgh-axdb5-drift].[DBO].[SALESLINE] AS SALE
       LEFT JOIN
            [hgf-sql01].[hfgh-axdb5-drift].[DBO].[CUSTTABLE] AS CUST

Open in new window


OR:
       FROM
            [hgf-sql01].[hfgh-axdb5-drift].[DBO].[SALESLINE] AS SALE
       right JOIN
            [hgf-sql01].[hfgh-axdb5-drift].[DBO].[CUSTTABLE] AS CUST

Open in new window



But dosent help....

I acnt return more records then the table:
 [hgf-sql01].[hfgh-axdb5-drift].[DBO].[CUSTTABLE]


What is wrong here ?
0
Comment
Question by:team2005
3 Comments
 
LVL 4

Expert Comment

by:krtyknmsql
Comment Utility
It is very hard to tell without the execution plan. Can you please let me know the index details for the tables SALESLINE and CUSTTABLE?

Is CUSTACCOUNT, ACCOUNTNUM and DATAAREAID are indexed columns?
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
It means that your INNER JOINs are yielding duplicates. Maybe you do not have the correct fields involved in the ON clause. In other words, check if multiple columns combine to form the unique identifier for the matches.

Note: if you use a LEFT JOIN from the SALESLINE table, you will get 1,039,137 (i.e., over a million) rows. Therefore, if your intent is to get all the CUSTTABLE records and only their matches, then use INNER JOIN or make CUSTTABLE the left-most table in the LEFT JOIN. If you get more rows than expected, then resolve the duplicates.

As a final thought, the issue may be that the duplicates are appropriate. A customer should have multiple matches in a sales order table, especially sales order lines—one sales order can have multiple items or delivery lines. Therefore, what you may need is a derived query to aggregate SALESLINE data by customer.

For example:
FROM CUSTTABLE CUST
/* INNER JOIN to return only customers with sales; OUTER JOIN for all customers. */
LEFT JOIN (
    SELECT  CUSTACCOUNT, sum(SALE.QTYORDERED)
    FROM SALESLINE
    GROUP BY CUSTACCOUNT
) SALE(CUSTACCOUNT, QTYORDERED) ON SALE.CUSTACCOUNT = CUST.ACCOUNTNUM

Open in new window


You can make this by customer, by year, by item. Just note that the number of rows will be more than what is in the customer table by the amount of variability in sales. The more items per customer, the more years per customer, et cetera, the more lines per customer in the final SELECT.

I hope that makes sense.

Best regards and happy coding,

Kevin
0
 
LVL 2

Author Closing Comment

by:team2005
Comment Utility
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

772 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

11 Experts available now in Live!

Get 1:1 Help Now