Results 1 to 5 of 5

Thread: [RESOLVED] Consufed on a Select Statement?

  1. #1

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Resolved [RESOLVED] Consufed on a Select Statement?

    I have the following query :
    Code:
    SELECT Personnel.PersonnelPK,Personnel.LastName + ', ' + Personnel.FirstName + ' ' + Personnel.MiddleName AS 'Personnel Name',WorkerTypes.CustumLevelName AS 'Worker Type',Location.Building + ' - ' + Location.LocationName As [Location]  FROM Location INNER JOIN PersonnelToLocation ON Location.LocationPK = PersonnelToLocation.LocationPK RIGHT OUTER JOIN Personnel INNER JOIN WorkerTypes ON Personnel.WorkerTypePK = WorkerTypes.WorkerTypePK ON PersonnelToLocation.PersonnelPK = Personnel.PersonnelPK Where Personnel.PersonnelPK > 1  And Personnel.PersonnelPK In (Select PersonnelPK From PersonnelToLocation Where PersonnelPK = 3)
    The query works, well kind of works. If a person is assigned to more then one location I get the persons name multiple times. Any sugestions as to getting the personns name only once with multile locations on one line?

    This is being returned as a dataset on .Net 2005 from and SQL Server 2005 backend.
    Last edited by GaryMazzone; May 29th, 2007 at 10:16 AM. Reason: Fixing the SQL statement
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  2. #2
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Consufed on a Select Statement?

    Quote Originally Posted by GaryMazzone
    The query works, well kind of works. If a person is assigned to more then one location I get the persons name multiple times. Any sugestions as to getting the personns name only once with multile locations on one line?
    Since the result from the query contains data from the Location table, what do you want to display when a person is assigned to more than one location? The first location? A concatenated string with all locations?

  3. #3

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Consufed on a Select Statement?

    All locations conatinated togther with the person's name in the result set once.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Consufed on a Select Statement?

    First of all you must clean up the query.

    This one makes no sense. You return the PersonnelPK from a table where PersonnelPK=3. Well, that will always return 3.
    Code:
    And Personnel.PersonnelPK In (Select PersonnelPK From PersonnelToLocation Where PersonnelPK = 3)
    Can you explain what you want the query to do. Do you want to return all persons for a specific location? Please explain; especially the conditions for what data to be returned.

  5. #5

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Consufed on a Select Statement?

    The code that generates is as follows:
    vb Code:
    1. Private Sub loadPersonnelGrid()
    2.         Dim strSQL As String = String.Empty
    3.         strSQL = "SELECT Personnel.PersonnelPK,Personnel.LastName + ', ' + Personnel.FirstName + ' ' + Personnel.MiddleName AS 'Personnel Name',"
    4.         strSQL &= "WorkerTypes.CustumLevelName AS 'Worker Type',Location.Building + ' - ' + Location.LocationName As [Location]  "
    5.         strSQL &= "FROM Location INNER JOIN "
    6.         strSQL &= "PersonnelToLocation ON Location.LocationPK = PersonnelToLocation.LocationPK RIGHT OUTER JOIN "
    7.         strSQL &= "Personnel INNER JOIN "
    8.         strSQL &= "WorkerTypes ON Personnel.WorkerTypePK = WorkerTypes.WorkerTypePK ON PersonnelToLocation.PersonnelPK = Personnel.PersonnelPK "
    9.         strSQL &= "Where Personnel.PersonnelPK > 1 "
    10.         If Me.optHideInact.Checked Then
    11.             strSQL &= "And Personnel.WorkerTypePK <> 2 "
    12.         End If
    13.         If Me.cboLocations.SelectedIndex > 0 Then
    14.             If Me.cboLocations.SelectedIndex = 1 Then
    15.                 strSQL &= " And Personnel.PersonnelPK Not In (Select PersonnelPK From PersonnelToLocation) "
    16.             Else
    17.                 strSQL &= " And Personnel.PersonnelPK In (Select PersonnelPK From PersonnelToLocation Where LocationPK = " & Me.arintLocationPKs(Me.cboLocations.SelectedIndex).ToString() & ") "
    18.             End If
    19.         End If
    20.         strSQL &= "Order By LastName,FirstName,MiddleName"
    21.  
    22.         Dim oDS As System.Data.DataSet
    23.         If Not mdlGeneral.currUser.bAccessToAdmin Then
    24.             Me.cmdNew.Enabled = False
    25.         End If
    26.  
    27.         da = New clsDataAccess
    28.         oDS = da.Return_DataSet(strSQL)
    29.  
    30.         If Not oDS Is Nothing Then
    31.             Me.dtGridPersonnel.DataSource = oDS.Tables(0)
    32.             Me.dtGridPersonnel.Columns(0).Visible = False
    33.             For i As Integer = 1 To Me.dtGridPersonnel.ColumnCount() - 1
    34.                 Me.dtGridPersonnel.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    35.             Next
    36.             Me.dtGridPersonnel.AllowUserToDeleteRows = False
    37.             Me.dtGridPersonnel.AllowUserToAddRows = False
    38.             oDS.Dispose()
    39.         Else
    40.             Me.dtGridPersonnel.Visible = False
    41.         End If
    42.         Me.dtGridPersonnel.Visible = True
    43.         If mdlGeneral.errMessage.intErrNum <> 0 Then
    44.             MessageBox.Show(mdlGeneral.errMessage.strMess, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    45.         End If
    46.         da.Dispose()
    47.     End Sub

    da is a class defined as private on the form and is a data access class

    The query will return results based on a copule of controls on the form. A radiobutton to remove inactive personnel, an Checkbox that displays a list of locations if you only want to see personnel from one location only.

    So if no location is selected then you get all personnel and the locations they are assigned in the dataset.

    So when the form first loads you get an SQL statement that looks like this:

    Code:
    SELECT Personnel.PersonnelPK,Personnel.LastName + ', ' + Personnel.FirstName + ' ' + Personnel.MiddleName AS 'Personnel Name',WorkerTypes.CustumLevelName AS 'Worker Type',Location.Building + ' - ' + Location.LocationName As [Location]  FROM Location INNER JOIN PersonnelToLocation ON Location.LocationPK = PersonnelToLocation.LocationPK RIGHT OUTER JOIN Personnel INNER JOIN WorkerTypes ON Personnel.WorkerTypePK = WorkerTypes.WorkerTypePK ON PersonnelToLocation.PersonnelPK = Personnel.PersonnelPK Where Personnel.PersonnelPK > 1 Order By LastName,FirstName,MiddleName
    Last edited by GaryMazzone; May 29th, 2007 at 11:43 AM. Reason: Continuing adding the genereated SQL Statement
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width