[Application Instance] displays data based on the display rate-[Leaf]

Source: Internet
Author: User

-- Data
/*
Occurrence Rate of serial number name category
-----------------------------------------------------------------
1 aa 1 0.5
2 BB 1 0.5
3 cc 2 0.3
4 DD 2 0.6
5 ff 2 0.1
*/
-- Requirements:
-- For example, the data in the table above is classified into two groups: 1 and 2.
-- Obtain a data entry for each category, so that the probability of Data occurrence matches the occurrence rate in the field.

Declare @ t table (serial number int, name varchar (2), classification int, occurrence rate numeric (0.5) insert into @ tselect 1, 'A', 1, Union allselect 2, 'bb ', 1, 0.5 Union allselect 3, 'cc', 2, 0.3 Union allselect 4, 'dd', 2, 0.6 Union allselect 5, 'ff', 2, 0.1; with MACO as (select. * From @ t aright join Master .. spt_values B on 1 = 1 where type = 'p' and. occurrence Rate * 10> B. number) Select category, (select top 1 name from MACO where category = T. CATEGORY order by newid () as name from maco t group by category/* category name --------- ---- 1 BB2 dd */

Example Source:

Http://social.microsoft.com/Forums/zh-CN/sqlserverzhchs/thread/33f20f9a-9750-409f-a1cc-99111dcbb5ff

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.