question

denizbalkan81 avatar image
denizbalkan81 asked

Table percentage over each branch code ?

we have a customers table. columns are branch_code , customer_number.... we need to create a new table that contains ; %10 percent of each branhces's customers . for example ; Branch_code : A and in this branch we have 2500 customers. the result table should have 250 customer from A. Same all for all branches
tablecounting
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image
Kev Riley answered
You could use NTILE to partition each branch into 10 chunks, and then simply take one of those 'chunks'. The newid() just randomizes the selection select x.branch_code, x.customer_number from ( select *, ntile(10)over(partition by branch_code order by newid()) as chunk from dbo.customers ) x where x.chunk = 1
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

denizbalkan81 avatar image
denizbalkan81 answered
Thank you soo much. Very practical solution.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.