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)