-1

I was looking but I don't see this exact issue with Distinct.

SETUP
4 tables

Users (UserId, FirstName, LastName, Email, Phone)
CuUsers (UserId, AccountId, PrimaryUser)
HrUsers (UserId, EmployeeId, DeptId)
VendUser (UserId, VendorId...)

PURPOSE OF DESIGN
A user may be any or multiple of these Employee, Customer, Vendor
A User may be an Owner of a Customer Account, or multiple Accounts
A User May be an owner in one account, and employee of another account

THIS QUERY
Is trying to get a list of Customer Users Only

DESIRED RESULT
I want an Alphabetical List of LastName so that I can use it as a button filter So I only get a result of Alphabet where Users are customers i.e. So if noone's last name begins with Z I don't get a z to filter

<asp:ListView ID="lvSelectCustomerAlpha" runat="server" DataSourceID="SqlSelectUserAlpha" 
         DataKeyNames="UserId" EnableModelValidation="True">
    <ItemTemplate>
        <asp:Button Text='<%# Eval("LastName") %>' runat="server" ID="btnAlpha"
              CommandArgument='<%# Eval("UserId") %>' OnClick="btnSelectAnAccount_Click" 
              CssClass="small" />
    </ItemTemplate>
    <LayoutTemplate>
        <div runat="server" id="itemPlaceholderContainer" class="pager">                        
            <div class="clr"></div>
                <span class="title">Last Name:</span>
                <div runat="server" id="itemPlaceholder"></div>
            </div>
    </LayoutTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlSelectUserAlpha" 
     ConnectionString='<%$ ConnectionStrings:MySolutionsCnSt %>' 
     SelectCommand="SELECT DISTINCT(SUBSTRING(Users.LastName,1,1)) AS LastName, CuAcctsUsr.UserId FROM CuAcctsUsr LEFT OUTER JOIN Users ON CuAcctsUsr.UserId = Users.UserId" />

The sql command

SELECT DISTINCT(SUBSTRING(Users.LastName,1,1)) AS LastName, 
       CuAcctsUsr.UserId FROM CuAcctsUsr LEFT OUTER JOIN Users ON 
       CuAcctsUsr.UserId = Users.UserId

My list returns 4 instances of L when I only need one, and I need the userId to get user Info from one table and check for Customer in the other, otherwise I have to duplicate all user info into both tables.

Is there a way to return only a single instance of each letter used in the LastName field if UserId's are different? I apologize if this has been posted before, but I didn't see an exact work for this type of request !(http://mycleaningsolution.com/desiredresult.jpg)

3
  • This is a badly written question. Please format the question in a more readable fashion, rather than a long paragraph without punctuation. Also, just include the SQL itself. What does all the rest of the XML text have to do with your question? See stackoverflow.com/help/mcve
    – Andreas
    Aug 19, 2015 at 2:33
  • First Time User, didn't format like I wanted, sorry. I guess my question is simply WHY does Sql not just DISTINCT the one column I want and not the others, why does it apply distinct to all, and is there a workaround without having to duplicate tables columns and data? Aug 19, 2015 at 2:52
  • 1
    DISTINCT applies to all the result columns. That's how SQL DISTINCT works.
    – Andreas
    Aug 19, 2015 at 2:54

2 Answers 2

1

DISTINCT is not a function, it's a keyword applied to the SELECT statement. Statement syntax is:

SELECT [DISTINCT] expr, expr, ... FROM table ...

Adding the DISTINCT keyword will ensure that duplicate rows are eliminated, hence result rows becoming "distinct". The entire row is compared.

For doing something like that on subset of columns, look at GROUP BY.

Your syntax looks confusing:

SELECT DISTINCT(SUBSTRING(Users.LastName,1,1)) AS LastName,
       CuAcctsUsr.UserId

-- Same as above
SELECT  DISTINCT  (SUBSTRING(Users.LastName,1,1)) AS LastName,
                  CuAcctsUsr.UserId

-- Same as above
SELECT  DISTINCT  SUBSTRING(Users.LastName,1,1) AS LastName,
                  CuAcctsUsr.UserId
0
select lastname , min(id) from 
(SELECT DISTINCT(SUBSTRING(name,1,1)) AS LastName, id from tbl1) as myusers
group by lastname

this code return just one userid for each letter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.