Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Problem in select

Problem in select

Scheduled Pinned Locked Moved Database
helpdatabasetutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    ksaw123
    wrote on last edited by
    #1

    I have this query that suppose to show the first name and last name of users (workers) in a milestone where the names of the users should not be repeated For example if john smith is repeated two times it should be selected once in the display. I have written this statement but it has an error. SELECT Profile.FirstName, Profile.LastName FROM AssignedTo INNER JOIN Profile ON AssignedTo.Username = Profile.Username CROSS JOIN Milestone WHERE (Milestone.MilestoneID = @MilestoneID) AND (AssignedTo.ProjectID = @ProjectID) GROUP BY Profile.Username Note: we can distinguish between users by using Username Thank you in advance fro your corporation

    N 1 Reply Last reply
    0
    • K ksaw123

      I have this query that suppose to show the first name and last name of users (workers) in a milestone where the names of the users should not be repeated For example if john smith is repeated two times it should be selected once in the display. I have written this statement but it has an error. SELECT Profile.FirstName, Profile.LastName FROM AssignedTo INNER JOIN Profile ON AssignedTo.Username = Profile.Username CROSS JOIN Milestone WHERE (Milestone.MilestoneID = @MilestoneID) AND (AssignedTo.ProjectID = @ProjectID) GROUP BY Profile.Username Note: we can distinguish between users by using Username Thank you in advance fro your corporation

      N Offline
      N Offline
      Nic Rowan
      wrote on last edited by
      #2

      The problem there is the Group By clause. A Group by is for aggregation (e.g. Averages, Sums, etc.) and your first name and last name aren't part of an aggregation. Try adding a DISTINCT clause after your select. See if that helps.

      SELECT DISTINCT
      Profile.FirstName,
      Profile.LastName
      FROM
      AssignedTo
      INNER JOIN Profile ON AssignedTo.Username = Profile.Username
      CROSS JOIN Milestone
      WHERE
      (Milestone.MilestoneID = @MilestoneID)
      AND (AssignedTo.ProjectID = @ProjectID)


      Dad always thought laughter was the best medicine, which I guess is why several of us died of tuberculosis. I can picture in my mind a world without war, a world without hate. And I can picture us attacking that world, because they'd never expect it.


      M 1 Reply Last reply
      0
      • N Nic Rowan

        The problem there is the Group By clause. A Group by is for aggregation (e.g. Averages, Sums, etc.) and your first name and last name aren't part of an aggregation. Try adding a DISTINCT clause after your select. See if that helps.

        SELECT DISTINCT
        Profile.FirstName,
        Profile.LastName
        FROM
        AssignedTo
        INNER JOIN Profile ON AssignedTo.Username = Profile.Username
        CROSS JOIN Milestone
        WHERE
        (Milestone.MilestoneID = @MilestoneID)
        AND (AssignedTo.ProjectID = @ProjectID)


        Dad always thought laughter was the best medicine, which I guess is why several of us died of tuberculosis. I can picture in my mind a world without war, a world without hate. And I can picture us attacking that world, because they'd never expect it.


        M Offline
        M Offline
        mr_lasseter
        wrote on last edited by
        #3

        Just so you know the following is no different than the query you wrote. In fact, using a group by in some instances is faster than using a distinct (atleast I read that somewhere about Oracle, although it could have changed with a new release of the database).

        SELECT Profile.FirstName, Profile.LastName
        FROM AssignedTo
        INNER JOIN Profile ON AssignedTo.Username = Profile.Username
        CROSS JOIN Milestone
        WHERE (Milestone.MilestoneID = @MilestoneID)
        AND (AssignedTo.ProjectID = @ProjectID)
        GROUP BY Profile.FirstName, Profile.LastName

        Mike Lasseter

        N 1 Reply Last reply
        0
        • M mr_lasseter

          Just so you know the following is no different than the query you wrote. In fact, using a group by in some instances is faster than using a distinct (atleast I read that somewhere about Oracle, although it could have changed with a new release of the database).

          SELECT Profile.FirstName, Profile.LastName
          FROM AssignedTo
          INNER JOIN Profile ON AssignedTo.Username = Profile.Username
          CROSS JOIN Milestone
          WHERE (Milestone.MilestoneID = @MilestoneID)
          AND (AssignedTo.ProjectID = @ProjectID)
          GROUP BY Profile.FirstName, Profile.LastName

          Mike Lasseter

          N Offline
          N Offline
          Nic Rowan
          wrote on last edited by
          #4

          Ya, I'm not sure about Oracle but as far as I know with Microsoft SQL there's no performance difference as the execution plan is normally identical. It's purely personal syntax preference. I've always found DISTINCT easier to read than a heap of groups.


          Dad always thought laughter was the best medicine, which I guess is why several of us died of tuberculosis. I can picture in my mind a world without war, a world without hate. And I can picture us attacking that world, because they'd never expect it.


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups