4

I have the following SQL table:

CarId | CustomerId | Price  
 1    |     1      | 5000  
 2    |     3      | 6000  
 3    |     4      | 6000  
 4    |     1      | 6000  
 5    |     3      | 6000  
 6    |     6      | 6000  

I want to create a new column:

CarId | CustomerId | Price | CarPerCustomer
 1    |     1      | 5000  |       1/2
 2    |     3      | 6000  |       1/3
 3    |     4      | 6000  |       1/1
 4    |     1      | 6000  |       2/2
 5    |     3      | 6000  |       2/3
 6    |     3      | 6000  |       3/3

So basically CarPerCustomer = number of row/(Total number of cars owned by this customer). Anyone?

8
  • Is it supposed to be a query result or persistent table with persistent column? Commented Jun 10, 2016 at 10:37
  • How to get 'Number of Row' and 'Total number of cars owned' ? Commented Jun 10, 2016 at 10:38
  • You appear to have a typo in the numbering scheme of your desired output, but I won't edit it. Commented Jun 10, 2016 at 10:42
  • @IvanStarostin I want to add it to a stored procedure Commented Jun 10, 2016 at 10:57
  • @TimBiegeleisen Fixed! Thanks Commented Jun 10, 2016 at 10:58

3 Answers 3

6

use window functions

select CarId, CustomerId, Price
    , cast(row_number() over (partition by CustomerId order by CustomerId) as varchar)
        + '/' + cast(count(*) over (partition by CustomerId ) as varchar)
    from t
Sign up to request clarification or add additional context in comments.

1 Comment

Nice one! Just add cast to nvarchar for row_number() and count
0
SELECT  y.*, 
        CAST(DENSE_RANK() OVER (PARTITION BY CustomerId ORDER BY CarId) AS nvarchar(10)) 
        + '/' + 
        CAST(cars_per_cust AS nvarchar(10)) as CarPerCustomer
FROM YourTable y
OUTER APPLY (
    SELECT COUNT(DISTINCT CarId) as cars_per_cust 
    FROM YourTable 
    WHERE CustomerId = y.CustomerId
    ) p
ORDER BY CarId, CarPerCustomer

Output:

CarId       CustomerId  Price       CarPerCustomer
----------- ----------- ----------- ---------------------
1           1           5000        1/2
2           3           6000        1/3
3           4           6000        1/1
4           1           6000        2/2
5           3           6000        2/3
6           3           6000        3/3

Comments

0

This query is a little bit tricky because you need to number the cars for each customer and this data does not exist in your table. You can generate the car numbers using a subquery. This gives the numerator of the CarPerCustomer column. For the denominator, the second part of my query computes the total number of cars per customer.

SELECT t1.CarId, t1.CustomerId, t1.Price,
    CONCAT((SELECT COUNT(*) + 1 FROM customers
            WHERE CarId < t1.CarId AND CustomerId = t1.CustomerId),
    '/', t2.numCars) AS CarPerCustomer
FROM customers t1
INNER JOIN
(
    SELECT CustomerId, COUNT(*) AS numCars
    FROM customers
    GROUP BY CustomerId
) t2
    ON t1.CustomerId = t2.CustomerId

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.