sql - 查找列的字符串值的组合

时间:2016-10-04 07:51:04

标签: sql sql-server

SQL Server代码,如果可能的话。

假设您有一个包含两列的表格。第1列名为Monster,第2列名为Level:

Monster | Level
_______________
Small Beast | 300
Large Beast | 700
Small Dragon | 350
Large Dragon | 800

如何查询此表以获取第1列:Monster的所有可能组合?请记住,表格中的怪物数量可能会有波动。

因此输出将是:

Small Beast, Large Beast
Small Beast, Small Dragon
Small Beast, Large Dragon
Large Beast, Small Dragon
Large Beast, Large Dragon
Small Dragon, Small Beast, Large Beast
Large Dragon, Small Beast, Large Beast

......等等。

然后我想为组合中的所有怪物添加第2列:Level的总和值并输出它们:

Small Beast, Large Beast: 1000
Small Beast, Small Dragon: 650
Large Dragon, Small Beast, Large Beast: 1800

2 个答案:

答案 0 :(得分:3)

您可以使用递归CTE:

;WITH cte AS (
SELECT  Monster, 
        [Level],
        1 as l
FROM YourTable
UNION ALL
SELECT  c1.Monster+','+c2.Monster,
        c1.[Level]+c2.[Level],
        c1.l+1
FROM cte c1
CROSS JOIN YourTable c2
WHERE c1.Monster NOT LIKE '%'+c2.Monster+'%'
)


SELECT *
FROM cte
ORDER BY l
OPTION (MAXRECURSION 0)

输出:

Monster                                             Level   l
Small Beast                                         300     1
Large Beast                                         700     1
Small Dragon                                        350     1
Large Dragon                                        800     1
Large Dragon,Small Beast                            1100    2
Large Dragon,Large Beast                            1500    2
Large Dragon,Small Dragon                           1150    2
Small Dragon,Small Beast                            650     2
Small Dragon,Large Beast                            1050    2
Small Dragon,Large Dragon                           1150    2
Large Beast,Small Beast                             1000    2
Large Beast,Small Dragon                            1050    2
Large Beast,Large Dragon                            1500    2
Small Beast,Large Beast                             1000    2
Small Beast,Small Dragon                            650     2
Small Beast,Large Dragon                            1100    2
Small Beast,Large Dragon,Large Beast                1800    3
Small Beast,Large Dragon,Small Dragon               1450    3
Small Beast,Small Dragon,Large Beast                1350    3
Small Beast,Small Dragon,Large Dragon               1450    3
...
Large Beast,Small Dragon,Large Dragon,Small Beast   2150    4
Large Beast,Small Dragon,Small Beast,Large Dragon   2150    4
Small Beast,Small Dragon,Large Dragon,Large Beast   2150    4
Small Beast,Small Dragon,Large Beast,Large Dragon   2150    4
Small Beast,Large Dragon,Small Dragon,Large Beast   2150    4
Small Beast,Large Dragon,Large Beast,Small Dragon   2150    4

答案 1 :(得分:0)

有一种方法可以做到:

/* SEARCH TOOL */

.search{
    width: 25%;
}

.fixed-table-toolbar .bs-bars,
.fixed-table-toolbar .search,
.fixed-table-toolbar .columns {
    position: relative;
    margin-top: 10px;
    margin-bottom: 10px;
    line-height: 34px;
}


<table class='table-bordered' id='tableprod'
                    data-toggle='table'
                    data-toolbar='#toolbar'
                    data-show-refresh='true'
                    data-show-toggle='true'
                    data-sort-name='name'
                    data-sort-order='desc'
                    data-show-columns='true'
                    data-pagination='true'
                    data-search='true'>

         <thead class='thead-inverse'>
           <tr>
              <th data-field='seleccion' data-switchable='false' data-checkbox='true'></th>
              <th data-field='estado' data-switchable='false'></th>   
              <th data-field='pagina' data-sortable='true'>PÀGINA</th>
              <th data-field='codigo' data-sortable='true' data-switchable='false'>CODI</th>
              <th data-field='descripcion' data-sortable='true' data-switchable='false'>DESCRIPCIÓ</th>
               <th data id='image' data-switchable='false'>imatge</th>
               <th data-field='pvp-cat' data-sortable='true'>PVP-CAT</th>
               <th data-field='pvp-lev' data-sortable='true'>PVP-LEV</th> 
               <th data-field='pvp-and' data-sortable='true'>PVP-AND</th>
               <th data-field='pvp-cen' data-sortable='true'>PVP-CEN</th>
               <th data-field='pvp-nor' data-sortable='true'>PVP-NOR</th>
               <th data-field='pvp-vas' data-sortable='true'>PVP-VAS</th>
               <th data-field='fecha-mod' data-sortable='true'>FECHA-MOD</th>
               <th data-field='user' data-sortable='true' data-visible='false'>USER</th>
               <th data-field='edit' data-sortable='false' data-switchable='false'>EDIT</th>
             </tr>
          </thead>
          <tbody>
             <tr>   
                <!— Function to load registres —>       
             </tr>  
          </tbody>
     </table> 


 if (this.options.search) {
            html = [];
            html.push(
                '<div class="pull-' + this.options.searchAlign + ' search">',
                sprintf('<input class="form-control' + 
                    sprintf(' input-%s', this.options.iconSize) +
                    '" type="text" placeholder="%s">',
                    this.options.formatSearch()),
                '</div>');

            this.$toolbar.append(html.join(''));
            $search = this.$toolbar.find('.search input');
            $search.off('keyup drop').on('keyup drop', function (event) {
                if (that.options.searchOnEnterKey && event.keyCode !== 13) {
                    return;
                }

                if ($.inArray(event.keyCode, [37, 38, 39, 40]) > -1) {
                    return;
                }

                clearTimeout(timeoutId); // doesn't matter if it's 0
                timeoutId = setTimeout(function () {
                    that.onSearch(event);
                }, that.options.searchTimeOut);
            });

            if (isIEBrowser()) {
                $search.off('mouseup').on('mouseup', function (event) {
                    clearTimeout(timeoutId); // doesn't matter if it's 0
                    timeoutId = setTimeout(function () {
                        that.onSearch(event);
                    }, that.options.searchTimeOut);
                });
            }
        } 

结果:

SELECT M1.monster,
    M2.monster,
    M1.level + M2.level
FROM
    monsters AS M1
CROSS JOIN
    monsters AS M2

祝福!