Test query

0

Please login or register to vote for this query.

(click on this box to dismiss)

Robotics Meta

Q&A about the site for professional robotic engineers, hobbyists, researchers and students

CREATE TABLE #customers(
customer_id INT,
name VARCHAR(100));

CREATE TABLE #orders(
order_id INT,
quantity INT,
order_date DATETIME, customer_id INT);

INSERT INTO #customers(customer_id, name) VALUES (1, 'COMPANY_A')

INSERT INTO #customers(customer_id, name) VALUES (2, 'COMPANY_B')

INSERT INTO #orders(order_id, quantity, order_date, customer_id) VALUES (1, 5, '2019-01-01', 1)

INSERT INTO #orders(order_id, quantity, order_date, customer_id) VALUES (2, 2, '2019-01-05', 1)


INSERT INTO #orders(order_id, quantity, order_date, customer_id) VALUES (2, 4, '2019-01-05', 2)

INSERT INTO #orders(order_id, quantity, order_date, customer_id) VALUES (2, 9, '2019-01-10', 2)


SELECT c.name, o.order_date
    FROM #customers c
    INNER JOIN #orders o
    ON c.customer_id = o.customer_id
    WHERE o.order_date = 
    (SELECT max(order_date) FROM #orders where order_id = o.order_id)

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results