Hi,

How can i make query for this:

Table1
Column1 Column2
LS11111 0
LS22222 100
LS33333 10
LS44444 0
LS55555 25

Table2
Column1 Column3
LS11111 100
LS22222 80
LS33333 25

Result i need is
Column1 Column2
LS11111 100
LS22222 100
LS33333 25
LS44444 0
LS55555 25

Column1 is same on both tables,
Column2 is pieces number in table1, and column3 is pieces number in Table2

I need maximum piece numbers.

If table2 doesnt exist column1 value then shows table1 column2, and if it exist and is larger than table1 column2 then shows number from table2

Recommended Answers

All 4 Replies

The fact this post hasn't just been deleted is a bad sign. Burn it with fire.

commented: Kind of leaves a bad taste in your eyes, doesn't it? +15

Please post, what you tried in your sql statement.

Clue: just Join and select the max value.

He's clearly not tried anything Shark_1, it's just a homework question he's pasted.

Giving him the answer will encourage this kind of behaviour in the future and further pollute our industry with qualified idiots. Not giving him the answer pollutes the forum with shitposts.

The only sensible thing to do is just delete these posts, but for some reason that doesn't happen. That's why StackOverflow has taken over.

Try this query!!It works!

SELECT  col1,
        MAX(col2) AS [col2] FROM ( SELECT  col1,col2  FROM    Table1 UNION
      SELECT  col1, col2   FROM    Table2 ) u
GROUP BY col1
ORDER BY col1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.