Exclude one column from group by in a joinHow can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?MySQL - UPDATE query based on SELECT QueryRetrieving the last record in each group - MySQLSelect first row in each GROUP BY group?What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?SQL select only rows with max value on a columnSelect a Column in SQL not in Group ByHow to left join 2 tables with SUM() and MAX() grouped by dateSelecting last record from INNER JOIN and groupingRetireve max/min row with left join trick for a group, when group is on another table

What do I need to see before Spider-Man: Far From Home?

Was the 45.9°C temperature in France in June 2019 the highest ever recorded in France?

Taking advantage when HR forgets to communicate the rules

Why does mean tend be more stable in different samples than median?

Wearing special clothes in public while in niddah- isn't this a lack of tznius?

Why would "dead languages" be the only languages that spells could be written in?

Machine Learning Golf: Multiplication

What is the fundamental difference between catching whales and hunting other animals?

Why no parachutes in the Orion AA2 abort test?

Multi-user CRUD: Valid, Problem, or Error?

SIunitx error when using lighter weight

Find max number you can create from an array of numbers

How do I check that users don't write down their passwords?

How to delete multiple process id of a single process?

How would a sea turtle end up on its back?

Any way to meet code with 40.7% or 40.44% conduit fill?

I'm feeling like my character doesn't fit the campaign

How to deal with a Murder Hobo Paladin?

How to factor a fourth degree polynomial

Howto display unicode character u2026 in terminal mode in emacs

Is reasonable to assume that the 食 in 月食/日食 can be interpreted as the sun/moon being "eaten" during an eclipse?

Chilling juice in copper vessel

Why do most airliners have underwing engines, while business jets have rear-mounted engines?

Why do we need a bootloader separate from our application program in microcontrollers?



Exclude one column from group by in a join


How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?MySQL - UPDATE query based on SELECT QueryRetrieving the last record in each group - MySQLSelect first row in each GROUP BY group?What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?SQL select only rows with max value on a columnSelect a Column in SQL not in Group ByHow to left join 2 tables with SUM() and MAX() grouped by dateSelecting last record from INNER JOIN and groupingRetireve max/min row with left join trick for a group, when group is on another table






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have an aggregated query:



SELECT SERIAL, pos_id, MIN(fecha)
FROM pvs
WHERE pos_id = 50
GROUP BY SERIAL, pos_id
ORDER BY MIN(fecha) DESC;


First query



and it works quite well to get the information I need, except one piece of data which is the id of that serial number in another table. This happens when I make a join.



SELECT d.id, p.SERIAL, p.pos_id, MIN(fecha)
FROM pvs p
JOIN devices d
ON d.SERIAL=p.SERIAL
WHERE p.pos_id = 50
GROUP BY p.SERIAL, p.pos_id, d.id
ORDER BY MIN(fecha) DESC;


Second query, lots of duplicates



I wanna show the id of every row in the other table but I can't exclude the Id from the group by (I just need the first date and only the first appearance of that serial number). I.e. i want the first query but adding the id from the other table.










share|improve this question






























    0















    I have an aggregated query:



    SELECT SERIAL, pos_id, MIN(fecha)
    FROM pvs
    WHERE pos_id = 50
    GROUP BY SERIAL, pos_id
    ORDER BY MIN(fecha) DESC;


    First query



    and it works quite well to get the information I need, except one piece of data which is the id of that serial number in another table. This happens when I make a join.



    SELECT d.id, p.SERIAL, p.pos_id, MIN(fecha)
    FROM pvs p
    JOIN devices d
    ON d.SERIAL=p.SERIAL
    WHERE p.pos_id = 50
    GROUP BY p.SERIAL, p.pos_id, d.id
    ORDER BY MIN(fecha) DESC;


    Second query, lots of duplicates



    I wanna show the id of every row in the other table but I can't exclude the Id from the group by (I just need the first date and only the first appearance of that serial number). I.e. i want the first query but adding the id from the other table.










    share|improve this question


























      0












      0








      0








      I have an aggregated query:



      SELECT SERIAL, pos_id, MIN(fecha)
      FROM pvs
      WHERE pos_id = 50
      GROUP BY SERIAL, pos_id
      ORDER BY MIN(fecha) DESC;


      First query



      and it works quite well to get the information I need, except one piece of data which is the id of that serial number in another table. This happens when I make a join.



      SELECT d.id, p.SERIAL, p.pos_id, MIN(fecha)
      FROM pvs p
      JOIN devices d
      ON d.SERIAL=p.SERIAL
      WHERE p.pos_id = 50
      GROUP BY p.SERIAL, p.pos_id, d.id
      ORDER BY MIN(fecha) DESC;


      Second query, lots of duplicates



      I wanna show the id of every row in the other table but I can't exclude the Id from the group by (I just need the first date and only the first appearance of that serial number). I.e. i want the first query but adding the id from the other table.










      share|improve this question
















      I have an aggregated query:



      SELECT SERIAL, pos_id, MIN(fecha)
      FROM pvs
      WHERE pos_id = 50
      GROUP BY SERIAL, pos_id
      ORDER BY MIN(fecha) DESC;


      First query



      and it works quite well to get the information I need, except one piece of data which is the id of that serial number in another table. This happens when I make a join.



      SELECT d.id, p.SERIAL, p.pos_id, MIN(fecha)
      FROM pvs p
      JOIN devices d
      ON d.SERIAL=p.SERIAL
      WHERE p.pos_id = 50
      GROUP BY p.SERIAL, p.pos_id, d.id
      ORDER BY MIN(fecha) DESC;


      Second query, lots of duplicates



      I wanna show the id of every row in the other table but I can't exclude the Id from the group by (I just need the first date and only the first appearance of that serial number). I.e. i want the first query but adding the id from the other table.







      mysql greatest-n-per-group






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 20:25









      Bill Karwin

      394k67 gold badges531 silver badges686 bronze badges




      394k67 gold badges531 silver badges686 bronze badges










      asked Mar 25 at 20:07









      ffuentesffuentes

      3441 gold badge7 silver badges17 bronze badges




      3441 gold badge7 silver badges17 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You could join the subquery



          select d.id, t.SERIAL, t.pos_id, t.min_fecha
          from devices d
          INNER JOIN (
          SELECT SERIAL, pos_id, MIN(fecha) min_fecha
          FROM pvs
          WHERE pos_id = 50
          GROUP BY SERIAL, pos_id
          ORDER BY MIN(fecha) DESC;

          ) t on t.SERIAL = d.SERIAL
          ORDER BY min_fecha DESC


          or if you need just one id apply an aggreation funtion



          SELECT min(d.id), p.SERIAL, p.pos_id, MIN(fecha)
          FROM pvs p
          JOIN devices d
          ON d.SERIAL=p.SERIAL
          WHERE p.pos_id = 50
          GROUP BY p.SERIAL, p.pos_id
          ORDER BY MIN(fecha) DESC;





          share|improve this answer

























          • I get the same result from the second query.

            – ffuentes
            Mar 25 at 20:24











          • Do you need to add AND d.fecha = t.min_fecha to the join conditions?

            – Bill Karwin
            Mar 25 at 20:26











          • @ffuentes answer updated ..

            – scaisEdge
            Mar 25 at 20:29












          • The second one works.

            – ffuentes
            Mar 25 at 20:34










          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%2f55345640%2fexclude-one-column-from-group-by-in-a-join%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You could join the subquery



          select d.id, t.SERIAL, t.pos_id, t.min_fecha
          from devices d
          INNER JOIN (
          SELECT SERIAL, pos_id, MIN(fecha) min_fecha
          FROM pvs
          WHERE pos_id = 50
          GROUP BY SERIAL, pos_id
          ORDER BY MIN(fecha) DESC;

          ) t on t.SERIAL = d.SERIAL
          ORDER BY min_fecha DESC


          or if you need just one id apply an aggreation funtion



          SELECT min(d.id), p.SERIAL, p.pos_id, MIN(fecha)
          FROM pvs p
          JOIN devices d
          ON d.SERIAL=p.SERIAL
          WHERE p.pos_id = 50
          GROUP BY p.SERIAL, p.pos_id
          ORDER BY MIN(fecha) DESC;





          share|improve this answer

























          • I get the same result from the second query.

            – ffuentes
            Mar 25 at 20:24











          • Do you need to add AND d.fecha = t.min_fecha to the join conditions?

            – Bill Karwin
            Mar 25 at 20:26











          • @ffuentes answer updated ..

            – scaisEdge
            Mar 25 at 20:29












          • The second one works.

            – ffuentes
            Mar 25 at 20:34















          1














          You could join the subquery



          select d.id, t.SERIAL, t.pos_id, t.min_fecha
          from devices d
          INNER JOIN (
          SELECT SERIAL, pos_id, MIN(fecha) min_fecha
          FROM pvs
          WHERE pos_id = 50
          GROUP BY SERIAL, pos_id
          ORDER BY MIN(fecha) DESC;

          ) t on t.SERIAL = d.SERIAL
          ORDER BY min_fecha DESC


          or if you need just one id apply an aggreation funtion



          SELECT min(d.id), p.SERIAL, p.pos_id, MIN(fecha)
          FROM pvs p
          JOIN devices d
          ON d.SERIAL=p.SERIAL
          WHERE p.pos_id = 50
          GROUP BY p.SERIAL, p.pos_id
          ORDER BY MIN(fecha) DESC;





          share|improve this answer

























          • I get the same result from the second query.

            – ffuentes
            Mar 25 at 20:24











          • Do you need to add AND d.fecha = t.min_fecha to the join conditions?

            – Bill Karwin
            Mar 25 at 20:26











          • @ffuentes answer updated ..

            – scaisEdge
            Mar 25 at 20:29












          • The second one works.

            – ffuentes
            Mar 25 at 20:34













          1












          1








          1







          You could join the subquery



          select d.id, t.SERIAL, t.pos_id, t.min_fecha
          from devices d
          INNER JOIN (
          SELECT SERIAL, pos_id, MIN(fecha) min_fecha
          FROM pvs
          WHERE pos_id = 50
          GROUP BY SERIAL, pos_id
          ORDER BY MIN(fecha) DESC;

          ) t on t.SERIAL = d.SERIAL
          ORDER BY min_fecha DESC


          or if you need just one id apply an aggreation funtion



          SELECT min(d.id), p.SERIAL, p.pos_id, MIN(fecha)
          FROM pvs p
          JOIN devices d
          ON d.SERIAL=p.SERIAL
          WHERE p.pos_id = 50
          GROUP BY p.SERIAL, p.pos_id
          ORDER BY MIN(fecha) DESC;





          share|improve this answer















          You could join the subquery



          select d.id, t.SERIAL, t.pos_id, t.min_fecha
          from devices d
          INNER JOIN (
          SELECT SERIAL, pos_id, MIN(fecha) min_fecha
          FROM pvs
          WHERE pos_id = 50
          GROUP BY SERIAL, pos_id
          ORDER BY MIN(fecha) DESC;

          ) t on t.SERIAL = d.SERIAL
          ORDER BY min_fecha DESC


          or if you need just one id apply an aggreation funtion



          SELECT min(d.id), p.SERIAL, p.pos_id, MIN(fecha)
          FROM pvs p
          JOIN devices d
          ON d.SERIAL=p.SERIAL
          WHERE p.pos_id = 50
          GROUP BY p.SERIAL, p.pos_id
          ORDER BY MIN(fecha) DESC;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 26 at 6:05

























          answered Mar 25 at 20:13









          scaisEdgescaisEdge

          104k10 gold badges55 silver badges73 bronze badges




          104k10 gold badges55 silver badges73 bronze badges












          • I get the same result from the second query.

            – ffuentes
            Mar 25 at 20:24











          • Do you need to add AND d.fecha = t.min_fecha to the join conditions?

            – Bill Karwin
            Mar 25 at 20:26











          • @ffuentes answer updated ..

            – scaisEdge
            Mar 25 at 20:29












          • The second one works.

            – ffuentes
            Mar 25 at 20:34

















          • I get the same result from the second query.

            – ffuentes
            Mar 25 at 20:24











          • Do you need to add AND d.fecha = t.min_fecha to the join conditions?

            – Bill Karwin
            Mar 25 at 20:26











          • @ffuentes answer updated ..

            – scaisEdge
            Mar 25 at 20:29












          • The second one works.

            – ffuentes
            Mar 25 at 20:34
















          I get the same result from the second query.

          – ffuentes
          Mar 25 at 20:24





          I get the same result from the second query.

          – ffuentes
          Mar 25 at 20:24













          Do you need to add AND d.fecha = t.min_fecha to the join conditions?

          – Bill Karwin
          Mar 25 at 20:26





          Do you need to add AND d.fecha = t.min_fecha to the join conditions?

          – Bill Karwin
          Mar 25 at 20:26













          @ffuentes answer updated ..

          – scaisEdge
          Mar 25 at 20:29






          @ffuentes answer updated ..

          – scaisEdge
          Mar 25 at 20:29














          The second one works.

          – ffuentes
          Mar 25 at 20:34





          The second one works.

          – ffuentes
          Mar 25 at 20:34








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          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%2f55345640%2fexclude-one-column-from-group-by-in-a-join%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권, 지리지 충청도 공주목 은진현