在 TSQL 中生成组合最有效、最灵活的方法是什么?

发布于 2024-08-31 14:52:08 字数 597 浏览 6 评论 0原文

在 TSQL 中生成组合最有效、最灵活的方法是什么? 对于“灵活”,我的意思是您应该能够轻松添加组合规则。例如:生成“n”个元素的组合、排序、删除重复项、获取每个奖品属于不同彩票的组合等。

例如,拥有一组代表彩票奖品的数字。

Number | Position | Lottery
---------------------------
 12    | 01       | 67
 12    | 02       | 67
 34    | 03       | 67
 43    | 01       | 89
 72    | 02       | 89
 33    | 03       | 89

(我包含位置列,因为不同彩票的奖品之间可能会重复一个数字)

我想生成如下组合:

Numbers | Lotteries
-------------------
 12 12  | 67 67
 12 34  | 67 67
 12 34  | 67 67
 12 43  | 67 89
 12 72  | 67 89
 12 33  | 67 89

        .
        .
        .

What is the most effective and flexible way to generate combinations in TSQL?
With 'Flexible', I mean you should be able to add easily combination rules. e.g.: to generate combinatories of 'n' elements, sorting, remove duplicates, get combinatories where each prize belongs to a different lottery, etc.

For example, Having a set of numbers representing lottery prizes.

Number | Position | Lottery
---------------------------
 12    | 01       | 67
 12    | 02       | 67
 34    | 03       | 67
 43    | 01       | 89
 72    | 02       | 89
 33    | 03       | 89

(I include the position column because, a number could be repeated among different lottery's prizes)

I would like to generate combinatories like:

Numbers | Lotteries
-------------------
 12 12  | 67 67
 12 34  | 67 67
 12 34  | 67 67
 12 43  | 67 89
 12 72  | 67 89
 12 33  | 67 89

        .
        .
        .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隐诗 2024-09-07 14:52:08

这称为交叉连接:

SELECT
    CAST(T1.Number AS VARCHAR) + ' ' + CAST(T2.Number AS VARCHAR) AS Numbers,
    CAST(T1.Lottery AS VARCHAR) + ' ' + CAST(T2.Lottery AS VARCHAR) AS Lottery
FROM table1 T1
CROSS JOIN table1 T2
ORDER BY Numbers

This is called a CROSS JOIN:

SELECT
    CAST(T1.Number AS VARCHAR) + ' ' + CAST(T2.Number AS VARCHAR) AS Numbers,
    CAST(T1.Lottery AS VARCHAR) + ' ' + CAST(T2.Lottery AS VARCHAR) AS Lottery
FROM table1 T1
CROSS JOIN table1 T2
ORDER BY Numbers
静若繁花 2024-09-07 14:52:08

更复杂的方法是使用 F#,以便用户可以使用领域特定语言编写方程。

有关如何使用 F# 的示例,您可以查看此博客中的最​​终评论:

http: //cs.hubfs.net/forums/thread/4496.aspx

原因是因为您需要想出一种由用户编写的简单方法来进行计算,但是,另一种选择是使用 C# 和实体属性值结构 (http://en.wikipedia.org /wiki/Entity-attribute-value_model),这样您就可以拥有一个灵活的系统,用户可以在其中为不同类型的彩票或不同的规则创建方程式。

但所有这些对于您的需求来说可能有点过大,具体取决于您真正需要多少灵活性。

The more complicated approach would be to use F# so that the user can write equations using a Domain Specific Language.

For an example of how to use F# you can see the final comment in this blog:

http://cs.hubfs.net/forums/thread/4496.aspx

The reason being because you will need to come up with an easy way to do the calculations, written by the user, but, the other option is to use C# and an Entity-Attribute-Value structure (http://en.wikipedia.org/wiki/Entity-attribute-value_model), so that you can have a flexible system where the user can create equations for different types of lotteries, or differing rules.

But all of these may be more overkill for what you need, depending on how much flexibility you really need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文