How to return same number of row from both queries with UNION?How to return only the Date from a SQL Server DateTime datatypeHow to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?How do I limit the number of rows returned by an Oracle query after ordering?How do I UPDATE from a SELECT in SQL Server?How to query MongoDB with “like”?SELECT TOP … FROM UNIONMYSQL select same column name as alias in union not workingSQL Union From a Single RowDuplicate Rows - SQL Server Groupby and Union
PhD: When to quit and move on?
What can a novel do that film and TV cannot?
How to deal with administrative duties killing the research spirit?
What happens if the limit of 4 billion files was exceeded in an ext4 partition?
Is it possible to spoof an IP address to an exact number?
Should I increase my 401k contributions, or increase my mortgage payments
Can 4 Joy cons connect to the same Switch?
Using Sed to add counter to keyword
What do you call the angle of the direction of an airplane?
Has there ever been a cold war other than between the U.S. and the U.S.S.R.?
How should I present a resort brochure in my general fiction?
What is the addition in the re-released version of Avengers: Endgame?
Convenience stores in India
Do the 26 richest billionaires own as much wealth as the poorest 3.8 billion people?
Why do we need a bootloader separate than our application program in MCU's?
Has chattel slavery ever been used as a criminal punishment in the USA since the passage of the Thirteenth Amendment?
Why did moving the mouse cursor cause Windows 95 to run more quickly?
How can solar sailed ships be protected from space debris?
Is it bad to suddenly introduce another element to your fantasy world a good ways into the story?
Term for a character that only exists to be talked to
Should I cheat if the majority does it?
What/Where usage English vs Japanese
Why do Klingons use cloaking devices?
Does a multiclassed wizard start with a spellbook?
How to return same number of row from both queries with UNION?
How to return only the Date from a SQL Server DateTime datatypeHow to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?How do I limit the number of rows returned by an Oracle query after ordering?How do I UPDATE from a SELECT in SQL Server?How to query MongoDB with “like”?SELECT TOP … FROM UNIONMYSQL select same column name as alias in union not workingSQL Union From a Single RowDuplicate Rows - SQL Server Groupby and Union
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have two queries that use SUM()
with GROUP BY
. Each query should return same number or rows. In this case my SQL in Sybase returns the rows separate instead on the same line. Here is my query:
SELECT type_id, category_id, category_name, type_code, amount, awarded
FROM (
SELECT
type_id,
category_id,
category_name,
type_code,
CASE
WHEN category_id = 1 THEN SUM(amount)
WHEN category_id = 2 THEN SUM(amount)
WHEN category_id = 3 THEN SUM(amount)
WHEN category_id = 4 THEN SUM(amount)
END AS amount,
0 AS awarded
FROM Table 1
GROUP BY category_id, type_id, category_id, type_code
UNION
SELECT
null AS type_id,
ga.grant_category_id,
'' AS category_name,
null AS type_code,
0 AS amount,
CASE
WHEN t2.category_id = 1 THEN SUM(t2.awarded)
WHEN t2.category_id = 2 THEN SUM(t2.awarded)
WHEN t2.category_id = 3 THEN SUM(t2.awarded)
WHEN t2.category_id = 4 THEN SUM(t2.awarded)
END AS awarded
FROM Table2 t2
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY t2.category_id
) x
GROUP BY x.category_id
Query result looks like this:
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 0.00
1 4 TEST 4 REST 39398.00 0.00
1 3 TEST 3 REST 79922.00 0.00
1 1 TEST 1 REST 70927.00 0.00
null 1 null null 0.00 96013.00
null 2 null null 0.00 78759.00
null 3 null null 0.00 21436.00
null 4 null null 0.00 74602.00
I would like the output to look like this:
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 96013.00
1 4 TEST 4 REST 39398.00 78759.00
1 3 TEST 3 REST 79922.00 21436.00
1 1 TEST 1 REST 70927.00 74602.00
How to achieve this output? Thank you.
sql group-by union sybase sybase-ase
add a comment |
I have two queries that use SUM()
with GROUP BY
. Each query should return same number or rows. In this case my SQL in Sybase returns the rows separate instead on the same line. Here is my query:
SELECT type_id, category_id, category_name, type_code, amount, awarded
FROM (
SELECT
type_id,
category_id,
category_name,
type_code,
CASE
WHEN category_id = 1 THEN SUM(amount)
WHEN category_id = 2 THEN SUM(amount)
WHEN category_id = 3 THEN SUM(amount)
WHEN category_id = 4 THEN SUM(amount)
END AS amount,
0 AS awarded
FROM Table 1
GROUP BY category_id, type_id, category_id, type_code
UNION
SELECT
null AS type_id,
ga.grant_category_id,
'' AS category_name,
null AS type_code,
0 AS amount,
CASE
WHEN t2.category_id = 1 THEN SUM(t2.awarded)
WHEN t2.category_id = 2 THEN SUM(t2.awarded)
WHEN t2.category_id = 3 THEN SUM(t2.awarded)
WHEN t2.category_id = 4 THEN SUM(t2.awarded)
END AS awarded
FROM Table2 t2
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY t2.category_id
) x
GROUP BY x.category_id
Query result looks like this:
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 0.00
1 4 TEST 4 REST 39398.00 0.00
1 3 TEST 3 REST 79922.00 0.00
1 1 TEST 1 REST 70927.00 0.00
null 1 null null 0.00 96013.00
null 2 null null 0.00 78759.00
null 3 null null 0.00 21436.00
null 4 null null 0.00 74602.00
I would like the output to look like this:
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 96013.00
1 4 TEST 4 REST 39398.00 78759.00
1 3 TEST 3 REST 79922.00 21436.00
1 1 TEST 1 REST 70927.00 74602.00
How to achieve this output? Thank you.
sql group-by union sybase sybase-ase
3
I think you want aJOIN
, not aUNION
.
– Gordon Linoff
Mar 25 at 18:58
@GordonLinoff Join doesn't work for this case. There is more complex data manipulation behind the screen.
– espresso_coffee
Mar 25 at 18:59
How do you tell which lines from either side of the UNION are to be combined into one line?
– Philip Kelley
Mar 25 at 18:59
@PhilipKelley All of them should be combined. The category_id is the column that should be joined.
– espresso_coffee
Mar 25 at 19:00
add a comment |
I have two queries that use SUM()
with GROUP BY
. Each query should return same number or rows. In this case my SQL in Sybase returns the rows separate instead on the same line. Here is my query:
SELECT type_id, category_id, category_name, type_code, amount, awarded
FROM (
SELECT
type_id,
category_id,
category_name,
type_code,
CASE
WHEN category_id = 1 THEN SUM(amount)
WHEN category_id = 2 THEN SUM(amount)
WHEN category_id = 3 THEN SUM(amount)
WHEN category_id = 4 THEN SUM(amount)
END AS amount,
0 AS awarded
FROM Table 1
GROUP BY category_id, type_id, category_id, type_code
UNION
SELECT
null AS type_id,
ga.grant_category_id,
'' AS category_name,
null AS type_code,
0 AS amount,
CASE
WHEN t2.category_id = 1 THEN SUM(t2.awarded)
WHEN t2.category_id = 2 THEN SUM(t2.awarded)
WHEN t2.category_id = 3 THEN SUM(t2.awarded)
WHEN t2.category_id = 4 THEN SUM(t2.awarded)
END AS awarded
FROM Table2 t2
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY t2.category_id
) x
GROUP BY x.category_id
Query result looks like this:
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 0.00
1 4 TEST 4 REST 39398.00 0.00
1 3 TEST 3 REST 79922.00 0.00
1 1 TEST 1 REST 70927.00 0.00
null 1 null null 0.00 96013.00
null 2 null null 0.00 78759.00
null 3 null null 0.00 21436.00
null 4 null null 0.00 74602.00
I would like the output to look like this:
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 96013.00
1 4 TEST 4 REST 39398.00 78759.00
1 3 TEST 3 REST 79922.00 21436.00
1 1 TEST 1 REST 70927.00 74602.00
How to achieve this output? Thank you.
sql group-by union sybase sybase-ase
I have two queries that use SUM()
with GROUP BY
. Each query should return same number or rows. In this case my SQL in Sybase returns the rows separate instead on the same line. Here is my query:
SELECT type_id, category_id, category_name, type_code, amount, awarded
FROM (
SELECT
type_id,
category_id,
category_name,
type_code,
CASE
WHEN category_id = 1 THEN SUM(amount)
WHEN category_id = 2 THEN SUM(amount)
WHEN category_id = 3 THEN SUM(amount)
WHEN category_id = 4 THEN SUM(amount)
END AS amount,
0 AS awarded
FROM Table 1
GROUP BY category_id, type_id, category_id, type_code
UNION
SELECT
null AS type_id,
ga.grant_category_id,
'' AS category_name,
null AS type_code,
0 AS amount,
CASE
WHEN t2.category_id = 1 THEN SUM(t2.awarded)
WHEN t2.category_id = 2 THEN SUM(t2.awarded)
WHEN t2.category_id = 3 THEN SUM(t2.awarded)
WHEN t2.category_id = 4 THEN SUM(t2.awarded)
END AS awarded
FROM Table2 t2
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY t2.category_id
) x
GROUP BY x.category_id
Query result looks like this:
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 0.00
1 4 TEST 4 REST 39398.00 0.00
1 3 TEST 3 REST 79922.00 0.00
1 1 TEST 1 REST 70927.00 0.00
null 1 null null 0.00 96013.00
null 2 null null 0.00 78759.00
null 3 null null 0.00 21436.00
null 4 null null 0.00 74602.00
I would like the output to look like this:
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 96013.00
1 4 TEST 4 REST 39398.00 78759.00
1 3 TEST 3 REST 79922.00 21436.00
1 1 TEST 1 REST 70927.00 74602.00
How to achieve this output? Thank you.
sql group-by union sybase sybase-ase
sql group-by union sybase sybase-ase
asked Mar 25 at 18:56
espresso_coffeeespresso_coffee
2,1765 gold badges22 silver badges56 bronze badges
2,1765 gold badges22 silver badges56 bronze badges
3
I think you want aJOIN
, not aUNION
.
– Gordon Linoff
Mar 25 at 18:58
@GordonLinoff Join doesn't work for this case. There is more complex data manipulation behind the screen.
– espresso_coffee
Mar 25 at 18:59
How do you tell which lines from either side of the UNION are to be combined into one line?
– Philip Kelley
Mar 25 at 18:59
@PhilipKelley All of them should be combined. The category_id is the column that should be joined.
– espresso_coffee
Mar 25 at 19:00
add a comment |
3
I think you want aJOIN
, not aUNION
.
– Gordon Linoff
Mar 25 at 18:58
@GordonLinoff Join doesn't work for this case. There is more complex data manipulation behind the screen.
– espresso_coffee
Mar 25 at 18:59
How do you tell which lines from either side of the UNION are to be combined into one line?
– Philip Kelley
Mar 25 at 18:59
@PhilipKelley All of them should be combined. The category_id is the column that should be joined.
– espresso_coffee
Mar 25 at 19:00
3
3
I think you want a
JOIN
, not a UNION
.– Gordon Linoff
Mar 25 at 18:58
I think you want a
JOIN
, not a UNION
.– Gordon Linoff
Mar 25 at 18:58
@GordonLinoff Join doesn't work for this case. There is more complex data manipulation behind the screen.
– espresso_coffee
Mar 25 at 18:59
@GordonLinoff Join doesn't work for this case. There is more complex data manipulation behind the screen.
– espresso_coffee
Mar 25 at 18:59
How do you tell which lines from either side of the UNION are to be combined into one line?
– Philip Kelley
Mar 25 at 18:59
How do you tell which lines from either side of the UNION are to be combined into one line?
– Philip Kelley
Mar 25 at 18:59
@PhilipKelley All of them should be combined. The category_id is the column that should be joined.
– espresso_coffee
Mar 25 at 19:00
@PhilipKelley All of them should be combined. The category_id is the column that should be joined.
– espresso_coffee
Mar 25 at 19:00
add a comment |
2 Answers
2
active
oldest
votes
When @Gordon talked about JOINs, he meant make them subqueries and join them. The following presumse that categories may or may not be returned by either query:
SELECT
Set1.type_id -- Where this is not found in Set1, you specified null in Set2
,ISNULL(Set1.category_id, Set2.grant_category_id) AS category_id
,ISNULL(Set1.category_name, '') -- Where this is not found in Set1, you specified <emptyString> in Set2
,Set1.type_code -- Where this is not found in Set1, you specified null in Set2
,ISNULL(Set1.amount, 0) -- Where this is not found in Set1, you specified 0 in Set2
,ISNULL(Set2.awarded, 0) -- Where this is not found in Set2, you specified 0 in Set1
FROM (
SELECT
type_id,
category_id,
category_name,
type_code,
SUM(CASE
WHEN category_id between 1 and 4 THEN amount
ELSE 0
END) AS amount
FROM Table1
GROUP BY
type_id,
category_id,
category_name,
type_code,
) Set1
FULL OUTER JOIN (
SELECT
t2.grant_category_id,
SUM(CASE
WHEN t2.category_id between 1 and 4 THEN t2.awarded
END) AS awarded
FROM Table2 t2
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY
t2.grant_category_id,
) Set2
ON Set2.grant_category_Id = Set1.category_id
Disclaimer: I could not check the syntax on this, so some minor debugging may be required.
I had to tweak few things because Sybase is "great". Thanks for your help!
– espresso_coffee
Mar 25 at 19:55
add a comment |
Is that what You want?
SELECT type_id, category_id, category_name, type_code, amount, awarded
FROM (
SELECT
t1.type_id,
t1.category_id,
t1.category_name,
t1.type_code,
SUM(t1.amount) amount,
SUM(t2.awarded) awarded
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.category_id = t2.category_id
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY
t1.type_id,
t1.category_id,
t1.category_name,
t1.type_code
) x
Moreover, in your example, it seems to me that there is an error.
Shouldn't it will be like that? Where key for Table1 and Table2 is category_id
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 78759.00
1 4 TEST 4 REST 39398.00 74602.00
1 3 TEST 3 REST 79922.00 21436.00
1 1 TEST 1 REST 70927.00 96013.00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55344743%2fhow-to-return-same-number-of-row-from-both-queries-with-union%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
When @Gordon talked about JOINs, he meant make them subqueries and join them. The following presumse that categories may or may not be returned by either query:
SELECT
Set1.type_id -- Where this is not found in Set1, you specified null in Set2
,ISNULL(Set1.category_id, Set2.grant_category_id) AS category_id
,ISNULL(Set1.category_name, '') -- Where this is not found in Set1, you specified <emptyString> in Set2
,Set1.type_code -- Where this is not found in Set1, you specified null in Set2
,ISNULL(Set1.amount, 0) -- Where this is not found in Set1, you specified 0 in Set2
,ISNULL(Set2.awarded, 0) -- Where this is not found in Set2, you specified 0 in Set1
FROM (
SELECT
type_id,
category_id,
category_name,
type_code,
SUM(CASE
WHEN category_id between 1 and 4 THEN amount
ELSE 0
END) AS amount
FROM Table1
GROUP BY
type_id,
category_id,
category_name,
type_code,
) Set1
FULL OUTER JOIN (
SELECT
t2.grant_category_id,
SUM(CASE
WHEN t2.category_id between 1 and 4 THEN t2.awarded
END) AS awarded
FROM Table2 t2
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY
t2.grant_category_id,
) Set2
ON Set2.grant_category_Id = Set1.category_id
Disclaimer: I could not check the syntax on this, so some minor debugging may be required.
I had to tweak few things because Sybase is "great". Thanks for your help!
– espresso_coffee
Mar 25 at 19:55
add a comment |
When @Gordon talked about JOINs, he meant make them subqueries and join them. The following presumse that categories may or may not be returned by either query:
SELECT
Set1.type_id -- Where this is not found in Set1, you specified null in Set2
,ISNULL(Set1.category_id, Set2.grant_category_id) AS category_id
,ISNULL(Set1.category_name, '') -- Where this is not found in Set1, you specified <emptyString> in Set2
,Set1.type_code -- Where this is not found in Set1, you specified null in Set2
,ISNULL(Set1.amount, 0) -- Where this is not found in Set1, you specified 0 in Set2
,ISNULL(Set2.awarded, 0) -- Where this is not found in Set2, you specified 0 in Set1
FROM (
SELECT
type_id,
category_id,
category_name,
type_code,
SUM(CASE
WHEN category_id between 1 and 4 THEN amount
ELSE 0
END) AS amount
FROM Table1
GROUP BY
type_id,
category_id,
category_name,
type_code,
) Set1
FULL OUTER JOIN (
SELECT
t2.grant_category_id,
SUM(CASE
WHEN t2.category_id between 1 and 4 THEN t2.awarded
END) AS awarded
FROM Table2 t2
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY
t2.grant_category_id,
) Set2
ON Set2.grant_category_Id = Set1.category_id
Disclaimer: I could not check the syntax on this, so some minor debugging may be required.
I had to tweak few things because Sybase is "great". Thanks for your help!
– espresso_coffee
Mar 25 at 19:55
add a comment |
When @Gordon talked about JOINs, he meant make them subqueries and join them. The following presumse that categories may or may not be returned by either query:
SELECT
Set1.type_id -- Where this is not found in Set1, you specified null in Set2
,ISNULL(Set1.category_id, Set2.grant_category_id) AS category_id
,ISNULL(Set1.category_name, '') -- Where this is not found in Set1, you specified <emptyString> in Set2
,Set1.type_code -- Where this is not found in Set1, you specified null in Set2
,ISNULL(Set1.amount, 0) -- Where this is not found in Set1, you specified 0 in Set2
,ISNULL(Set2.awarded, 0) -- Where this is not found in Set2, you specified 0 in Set1
FROM (
SELECT
type_id,
category_id,
category_name,
type_code,
SUM(CASE
WHEN category_id between 1 and 4 THEN amount
ELSE 0
END) AS amount
FROM Table1
GROUP BY
type_id,
category_id,
category_name,
type_code,
) Set1
FULL OUTER JOIN (
SELECT
t2.grant_category_id,
SUM(CASE
WHEN t2.category_id between 1 and 4 THEN t2.awarded
END) AS awarded
FROM Table2 t2
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY
t2.grant_category_id,
) Set2
ON Set2.grant_category_Id = Set1.category_id
Disclaimer: I could not check the syntax on this, so some minor debugging may be required.
When @Gordon talked about JOINs, he meant make them subqueries and join them. The following presumse that categories may or may not be returned by either query:
SELECT
Set1.type_id -- Where this is not found in Set1, you specified null in Set2
,ISNULL(Set1.category_id, Set2.grant_category_id) AS category_id
,ISNULL(Set1.category_name, '') -- Where this is not found in Set1, you specified <emptyString> in Set2
,Set1.type_code -- Where this is not found in Set1, you specified null in Set2
,ISNULL(Set1.amount, 0) -- Where this is not found in Set1, you specified 0 in Set2
,ISNULL(Set2.awarded, 0) -- Where this is not found in Set2, you specified 0 in Set1
FROM (
SELECT
type_id,
category_id,
category_name,
type_code,
SUM(CASE
WHEN category_id between 1 and 4 THEN amount
ELSE 0
END) AS amount
FROM Table1
GROUP BY
type_id,
category_id,
category_name,
type_code,
) Set1
FULL OUTER JOIN (
SELECT
t2.grant_category_id,
SUM(CASE
WHEN t2.category_id between 1 and 4 THEN t2.awarded
END) AS awarded
FROM Table2 t2
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY
t2.grant_category_id,
) Set2
ON Set2.grant_category_Id = Set1.category_id
Disclaimer: I could not check the syntax on this, so some minor debugging may be required.
answered Mar 25 at 19:24
Philip KelleyPhilip Kelley
33.8k9 gold badges46 silver badges82 bronze badges
33.8k9 gold badges46 silver badges82 bronze badges
I had to tweak few things because Sybase is "great". Thanks for your help!
– espresso_coffee
Mar 25 at 19:55
add a comment |
I had to tweak few things because Sybase is "great". Thanks for your help!
– espresso_coffee
Mar 25 at 19:55
I had to tweak few things because Sybase is "great". Thanks for your help!
– espresso_coffee
Mar 25 at 19:55
I had to tweak few things because Sybase is "great". Thanks for your help!
– espresso_coffee
Mar 25 at 19:55
add a comment |
Is that what You want?
SELECT type_id, category_id, category_name, type_code, amount, awarded
FROM (
SELECT
t1.type_id,
t1.category_id,
t1.category_name,
t1.type_code,
SUM(t1.amount) amount,
SUM(t2.awarded) awarded
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.category_id = t2.category_id
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY
t1.type_id,
t1.category_id,
t1.category_name,
t1.type_code
) x
Moreover, in your example, it seems to me that there is an error.
Shouldn't it will be like that? Where key for Table1 and Table2 is category_id
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 78759.00
1 4 TEST 4 REST 39398.00 74602.00
1 3 TEST 3 REST 79922.00 21436.00
1 1 TEST 1 REST 70927.00 96013.00
add a comment |
Is that what You want?
SELECT type_id, category_id, category_name, type_code, amount, awarded
FROM (
SELECT
t1.type_id,
t1.category_id,
t1.category_name,
t1.type_code,
SUM(t1.amount) amount,
SUM(t2.awarded) awarded
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.category_id = t2.category_id
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY
t1.type_id,
t1.category_id,
t1.category_name,
t1.type_code
) x
Moreover, in your example, it seems to me that there is an error.
Shouldn't it will be like that? Where key for Table1 and Table2 is category_id
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 78759.00
1 4 TEST 4 REST 39398.00 74602.00
1 3 TEST 3 REST 79922.00 21436.00
1 1 TEST 1 REST 70927.00 96013.00
add a comment |
Is that what You want?
SELECT type_id, category_id, category_name, type_code, amount, awarded
FROM (
SELECT
t1.type_id,
t1.category_id,
t1.category_name,
t1.type_code,
SUM(t1.amount) amount,
SUM(t2.awarded) awarded
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.category_id = t2.category_id
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY
t1.type_id,
t1.category_id,
t1.category_name,
t1.type_code
) x
Moreover, in your example, it seems to me that there is an error.
Shouldn't it will be like that? Where key for Table1 and Table2 is category_id
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 78759.00
1 4 TEST 4 REST 39398.00 74602.00
1 3 TEST 3 REST 79922.00 21436.00
1 1 TEST 1 REST 70927.00 96013.00
Is that what You want?
SELECT type_id, category_id, category_name, type_code, amount, awarded
FROM (
SELECT
t1.type_id,
t1.category_id,
t1.category_name,
t1.type_code,
SUM(t1.amount) amount,
SUM(t2.awarded) awarded
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.category_id = t2.category_id
INNER JOIN Table3 t3
ON t2.rec_id = t3.rec_id
GROUP BY
t1.type_id,
t1.category_id,
t1.category_name,
t1.type_code
) x
Moreover, in your example, it seems to me that there is an error.
Shouldn't it will be like that? Where key for Table1 and Table2 is category_id
type_id category_id category_name type_code amount awarded
1 2 TEST 2 REST 51804.00 78759.00
1 4 TEST 4 REST 39398.00 74602.00
1 3 TEST 3 REST 79922.00 21436.00
1 1 TEST 1 REST 70927.00 96013.00
edited Mar 25 at 19:26
answered Mar 25 at 19:15
lypskeelypskee
1517 bronze badges
1517 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55344743%2fhow-to-return-same-number-of-row-from-both-queries-with-union%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
I think you want a
JOIN
, not aUNION
.– Gordon Linoff
Mar 25 at 18:58
@GordonLinoff Join doesn't work for this case. There is more complex data manipulation behind the screen.
– espresso_coffee
Mar 25 at 18:59
How do you tell which lines from either side of the UNION are to be combined into one line?
– Philip Kelley
Mar 25 at 18:59
@PhilipKelley All of them should be combined. The category_id is the column that should be joined.
– espresso_coffee
Mar 25 at 19:00