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;








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.










share|improve this question

















  • 3





    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











  • 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

















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.










share|improve this question

















  • 3





    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











  • 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













0












0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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











  • 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





    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











  • 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












2 Answers
2






active

oldest

votes


















2














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.






share|improve this answer























  • I had to tweak few things because Sybase is "great". Thanks for your help!

    – espresso_coffee
    Mar 25 at 19:55


















0














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






share|improve this answer



























    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
    );



    );













    draft saved

    draft discarded


















    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









    2














    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.






    share|improve this answer























    • I had to tweak few things because Sybase is "great". Thanks for your help!

      – espresso_coffee
      Mar 25 at 19:55















    2














    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.






    share|improve this answer























    • I had to tweak few things because Sybase is "great". Thanks for your help!

      – espresso_coffee
      Mar 25 at 19:55













    2












    2








    2







    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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

















    • 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













    0














    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






    share|improve this answer





























      0














      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






      share|improve this answer



























        0












        0








        0







        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






        share|improve this answer















        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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 25 at 19:26

























        answered Mar 25 at 19:15









        lypskeelypskee

        1517 bronze badges




        1517 bronze badges



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현