0

Can someone help me join these to tables and extract the PRODUCT_ID, PRODUCT_CODE, PRODUCT_NAME and the LAST_ORDER_DATE as not had much experience with SQL only MYSQL.

I'm not sure if I even need the second table or is it possible to use MAX in in just the ODBC_ORDER_LINE_ALL.

Table 1

Name: ODBC_ORDER_LINE_ALL

ORDER_LINE_ID

ORDER_LINE_DATE

ORDER_LINE_PRODUCT_ID

ORDER_LINE_PRODUCT_CODE

ORDER_LINE_PRODUCT_NAME

Table 2

Name: ODBC_PRODUCT_ALL

PRODUCT_ID

PRODUCT_CODE

PRODUCT_NAME

Thanks in advance.

Roy

1
  • 1
    What do you mean by saying ‘not had much experience with SQL only MYSQL’? MySQL is the name of a product, SQL is the name of a language, which is used in MySQL too (well, a specific flavour of SQL, but generally it is still referred to as 'SQL'). Did you actually mean to say 'SQL Server' by any chance (which is not the same as to say 'SQL', although people do confuse these two)?
    – Andriy M
    Oct 3, 2011 at 9:47

2 Answers 2

0

Have you tried simple grouping like :

SELECT ORDER_LINE_PRODUCT_ID,ORDER_LINE_PRODUCT_CODE,ORDER_LINE_PRODUCT_NAME, MAX(ORDER_LINE_DATE)
FROM ODBC_ORDER_LINE_ALL
GROUP BY ORDER_LINE_PRODUCT_ID,ORDER_LINE_PRODUCT_CODE,ORDER_LINE_PRODUCT_NAME
0

You don't require the second table.

With your current implementation the query would be

SELECT ORDER_LINE_PRODUCT_ID, 
       ORDER_LINE_PRODUCT_CODE, 
       ORDER_LINE_PRODUCT_NAME, 
       MAX(ORDER_LINE_DATE)
FROM ODBC_ORDER_LINE_ALL
GROUP BY ORDER_LINE_PRODUCT_ID

However, you need to normalize your first table. Assuming that your product id, product code and product name in your order line table reference the same values in the product table,
(And that a single product id corresponds to only one product code and name),
Your table structure for Table 1: ODBC_ORDER_LINE_ALL should be

ORDER_LINE_ID
ORDER_LINE_DATE
Foreign key (ORDER_LINE_PRODUCT_ID) references ODBC_PRODUCT_ALL(PRODUCT_ID)

In this case the query would be

SELECT ORDER_LINE_PRODUCT_ID, PRODUCT_CODE, PRODUCT_NAME,  MAX(ORDER_LINE_DATE), 
FROM ODBC_ORDER_LINE_ALL 
     JOIN ODBC_PRODUCT_ALL
     ON  ORDER_LINE_PRODUCT_ID = PRODUCT_ID
GROUP BY ORDER_LINE_PRODUCT_ID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.