SQL按记录集的第一个数字分组

时间:2017-02-03 06:08:36

标签: sql sql-server

我需要你的SQL帮助 我在下面有一组成本中心ID记录。 我想要做的是通过插入列来区分类别来隔离/分组它们。

你可以看到7中所有数字都是粗体数字。 我的预期出现在下面的图像上。

image

2 个答案:

答案 0 :(得分:0)

您可以如下所示:

DECLARE @Tbl TABLE (ID INT)
INSERT INTO @Tbl
VALUES
(735121201),
(735120001),
(5442244),
(735141094),
(735141097),
(4008060),
(735117603),
(40100000),
(735142902),
(735151199),
(4010070)

;WITH TableWithRowId
AS
(
    SELECT 
        ROW_NUMBER() OVER (ORDER BY(SELECT NULL)) RowId,
        ID
    FROM 
        @Tbl    
), TempTable
AS
(
    SELECT T.RowId + 1 AS RowId  FROM TableWithRowId T
    WHERE
        LEFT(T.ID, 1) != 7
), ResultTable
AS
(
    SELECT
        T.RowId ,
        T.ID,
        DENSE_RANK() OVER (ORDER BY (SELECT TOP 1 A.RowId FROM TempTable A WHERE A.RowId > T.RowId ORDER BY A.RowId)) AS Flag
    FROM TableWithRowId T
)

SELECT * FROM ResultTable

结果:

RowId       ID          Flag
----------- ----------- ----------
1           735121201   1
2           735120001   1
3           5442244     1
4           735141094   2
5           735141097   2
6           4008060     2
7           735117603   3
8           40100000    3
9           735142902   4
10          735151199   4
11          4010070     4

答案 1 :(得分:0)

以下查询是NEER&#39>的similer

<asp:Content ID="UserContent" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    <script type="text/javascript" src="../Scripts/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="../Scripts/jquery-ui-1.12.1.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#accordion").accordion();
        });

        function BtnTrigger() {
            alert("button pressed");
        }
    </script>
    <div id="accordion">
        <h3>Change Password</h3>
        <div>
            <div>
                <div>
                    <b>Previous Password: :</b>
                    <asp:TextBox ID="txt_prev_pwd" TextMode="Password" runat="server" AutoCompleteType="Disabled"  MaxLength="15" />
                    <asp:RequiredFieldValidator ID="rqd_txtpwd" runat="server" ErrorMessage="Please enter the password" ControlToValidate="txt_prev_pwd" />
                </div>
                <div>
                    <b>New Password :</b>
                    <asp:TextBox ID="txt_new_pwd" TextMode="Password" runat="server" AutoCompleteType="Disabled"  MaxLength="15" />
                    <asp:RequiredFieldValidator ID="rqd_txt_new_pwd" runat="server" ErrorMessage="Please enter the new password" ControlToValidate="txt_new_pwd" />
                </div>
            </div>
            <asp:Button ID="btn_pwd" runat="server" Text="Update" OnClick="btn_pwd_Click" />
        </div>
        <h3>Change Department</h3>
        <div>
            <div>
                <div>
            <b>Department :</b>
            <asp:ListBox ID="listBoxDepartment" runat="server" Rows="1" Width="300px"></asp:ListBox>
        </div>
            </div>
            <asp:Button ID="btn_chng_depart" runat="server" Text="Update" OnClick="btn_chng_depart_Click" OnClientClick="BtnTrigger();" />
        </div>
    </div>
</asp:Content>

// code behind
protected void btn_chng_depart_Click(object sender, EventArgs e)
        {
            try
            {
                // code
            }
            catch (Exception ex) { }
        }
CenterID  toFilter
--------- --------------------
735121201 1
735120001 1
5442244   1
735141094 2
735141097 2
4008060   2
735117603 3
40100000  3
735142902 4
735151199 4
4010070   4