Thread: how to write querey return the unique CustomerId with the first
CustomerName ?
Started 1 month, 2 weeks ago by Member 3018046
hii , i have question and i wont ask this question in form of the example. if we have table named CUSTOMER: CUSTOMER TABLE: CustomerID : Integer (Not a primary Key) CustomerName : TEXT And This is the table ' s data: CustomerId | CustomerName 12 | Jone 25 | Mark 25 | Marc 10 | AJ we note that ...
Maybe it ins't the best solution but you can use a group by (CustomerId), then you have to choose wich CustomerName is the choosen (min(CustomerName), max(CustomerName...). Sql: > select CustomerId, max(CustomerName) from Customer group by CustomerId.
SELECT @customerId as customerId, ( SELECT TOP 1 customerName FROM Customer WHERE customerId=@customerID ORDER BY customerName) AS customerName Whenever your data looks this bad, its a sure-fire signal to clean your data, and ensure your apps only insert clean data.
hii ,sorry what do you mean by sure-fire signal ? yes i agree with you that the data is look bad , but i work on a huge data that which is extracted by the Customers them self so i can not edit any thing. many thanks
You can even try this declare @tblcustomer table(customerid int,customername varchar(50)) insert into @tblcustomer select 12,'Jone' union all select 25,'Mark' union all select 25,'Marc' union all select 10,'AJ' select x.customerid ,x.customername from ( select ROW_NUMBER() over (partition by customerid order by customerid) as rn ,customerid ,customername from @tblcustomer)...
Member 3018046 wrote: hii ,sorry what do you mean by sure-fire signal It means that it is time to examine why you have 2 customer records with the same Id, but different names.
hii ,sorry what do you mean by sure-fire signal ? yes i agree with you that the data is look bad , but i work on a huge data that which is extracted by the Customers them self so i can not edit any thing. many thanks
Member 3018046 wrote: hii ,sorry what do you mean by sure-fire signal It means that it is time to examine why you have 2 customer records with the same Id, but different names.
Maybe it ins't the best solution but you can use a group by (CustomerId), then you have to choose wich CustomerName is the choosen (min(CustomerName), max(CustomerName...). Sql: > select CustomerId, max(CustomerName) from Customer group by CustomerId.
You can even try this declare @tblcustomer table(customerid int,customername varchar(50)) insert into @tblcustomer select 12,'Jone' union all select 25,'Mark' union all select 25,'Marc' union all select 10,'AJ' select x.customerid ,x.customername from ( select ROW_NUMBER() over (partition by customerid order by customerid) as rn ,customerid ,customername from @tblcustomer)X(rn,customerid,customername)...
Thread profile page for "how to write querey return the unique CustomerId with the first
CustomerName ?" on http://www.codeproject.com.
This report page is a snippet summary view from a single thread "how to write querey return the unique CustomerId with the first
CustomerName ?", located on the Message Board at http://www.codeproject.com.
This thread profile page shows the thread statistics for: Total Authors, Total Thread Posts, and Thread Activity