获取给定规则的表的特定行 SQL Server 2008

发布于 2024-11-30 10:40:42 字数 1028 浏览 1 评论 0原文

我有一个像这样的表:

ID   NAME     VAL
----------------------
1   a1*a1   90052
2   a1*a2   236
3   a1*a3   56
4   a1*a4   6072
5   a1*a5   1004
6   a2*a2   4576
7   a2*a3   724
8   a2*a4   230
9   a2*a5   679
10  a3*a3   5
11  a3*a4   644
12  a3*a5   23423
13  a4*a4   42354
14  a4*a5   10199
15  a5*a5   10279

给定一个数字 S = 5,我想查询 id 的行:1,6,10,13,15 它们是 a1*a1,a2*a2,a3*a3,a4*a4 和 a5*a5

我想要类似的东西:

INSERT #NEW_TABLE (ID,NAME,Value) (
SELECT ordinal, NAME, VAL FROM myTable where id = 1,6,10,13,15)

  ID   NAME     VAL
    ----------------------
    1   a1*a1   90052
    2   a2*a2   4576
    3   a3*a3   5
    4   a4*a4   42354
    5   a5*a5   10279

没有办法对任何给定的 S 执行此操作,也许有动态sql?

我得到了公式,得到了这个:

S=5

ID     formula
1      1
6      1+S
10     1+S+ (S-1)
13     1+S+ (S-1) + (S-2)
15     1+S+ (S-1) + (S-2) + (S-3)

有没有办法在 case 或 while 循环内执行此操作?

I have a table like:

ID   NAME     VAL
----------------------
1   a1*a1   90052
2   a1*a2   236
3   a1*a3   56
4   a1*a4   6072
5   a1*a5   1004
6   a2*a2   4576
7   a2*a3   724
8   a2*a4   230
9   a2*a5   679
10  a3*a3   5
11  a3*a4   644
12  a3*a5   23423
13  a4*a4   42354
14  a4*a5   10199
15  a5*a5   10279

Given a number given S = 5, I want to query
the rows wth id: 1,6,10,13,15
they are a1*a1,a2*a2,a3*a3,a4*a4 and a5*a5

I would like something like:

INSERT #NEW_TABLE (ID,NAME,Value) (
SELECT ordinal, NAME, VAL FROM myTable where id = 1,6,10,13,15)

to get

  ID   NAME     VAL
    ----------------------
    1   a1*a1   90052
    2   a2*a2   4576
    3   a3*a3   5
    4   a4*a4   42354
    5   a5*a5   10279

Is there a way to do this for any given S, Maybe wth dynamic sql?

I was getting the formula and I got this:

S=5

ID     formula
1      1
6      1+S
10     1+S+ (S-1)
13     1+S+ (S-1) + (S-2)
15     1+S+ (S-1) + (S-2) + (S-3)

Is there a way to do this inside a case or a while loop?

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

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

发布评论

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

评论(1

柳若烟 2024-12-07 10:40:42

这在测试中起作用了。

您可以仅在 @Tab 上进行内部联接来限制您的结果。您可能还想为低于 3 的值添加一些陷阱,但我还没有这样做。

基本过程是

  • 声明您的 @s 值
  • 插入前两行,因为它们始终相同
  • 在循环中,一次插入一行,差异递增
  • 循环运行后退出 @s-2 次

尝试:

DECLARE @Tab Table (id INT)

DECLARE @S int = 5,
        @ct int

DECLARE @cur int = (1 + @S)

INSERT INTO @Tab SELECT 1
INSERT INTO @Tab SELECT (1 + @S)

SET @ct = 1

WHILE @ct <= @S - 2
BEGIN

    SET @cur = @cur + (@S - @ct)

    INSERT INTO @Tab SELECT @cur

    SET @ct = @ct + 1

END

SELECT * FROM @Tab
ORDER BY id

要在查询中使用它,您可以执行以下任一操作:

SELECT ordinal, NAME, VAL 
FROM myTable 
WHERE id IN (SELECT id FROM @Tab)

-- OR

SELECT ordinal, NAME, VAL 
FROM myTable t
INNER JOIN @tab t2
    ON t2.id = t.id

This worked in testing.

You can just inner join on @Tab to limit your results. You probably also want to add some traps for values below 3, which I haven't done.

The basic process is

  • Declare your @s value
  • Insert the first two rows since they will always be the same
  • In a loop, insert one row at a time with an incrementing difference
  • Loop exits once it has run @s-2 times

Try:

DECLARE @Tab Table (id INT)

DECLARE @S int = 5,
        @ct int

DECLARE @cur int = (1 + @S)

INSERT INTO @Tab SELECT 1
INSERT INTO @Tab SELECT (1 + @S)

SET @ct = 1

WHILE @ct <= @S - 2
BEGIN

    SET @cur = @cur + (@S - @ct)

    INSERT INTO @Tab SELECT @cur

    SET @ct = @ct + 1

END

SELECT * FROM @Tab
ORDER BY id

To use this in your query, you can do either:

SELECT ordinal, NAME, VAL 
FROM myTable 
WHERE id IN (SELECT id FROM @Tab)

-- OR

SELECT ordinal, NAME, VAL 
FROM myTable t
INNER JOIN @tab t2
    ON t2.id = t.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文