gpt4 book ai didi

sql-server-2008 - 向 SELECT 查询的结果添加一个计数器

转载 作者:行者123 更新时间:2023-12-02 00:22:11 24 4
gpt4 key购买 nike

我正在尝试向选择结果列表添加一个计数器。为简单起见,我的查询是这样的:

Select distinct '"'+cast((product_sku) as varchar(30))+'"' as product_sku,preco,'"tamanhoecor::' + Left(Main.preco,Len(Main.preco)-1) + '"' As "custom_price"
From(
Select distinct ST2.product_sku,

(Select cordesc + ' - ' + tamanho +':' + cast(price_dif as varchar) + ';'
From [mg_produtos_preco] ST1
Where ST1.product_sku = ST2.product_sku
ORDER BY ST1.product_sku
For XML PATH ('')) [Preco],
product_price, cordesc,tamanho
From [mg_produtos_preco] ST2) [Main]

这让我明白了:

product_sku     preco                                                custom_price
"340803 010" Preto - S:0;Preto - M:0;Preto - L:0;Preto - XL:0; "tamanhoecor::Preto - S:0;Preto - M:0;Preto - L:0;Preto - XL:0"
"340803 100" Branco - S:0;Branco - M:0;Branco - L:0; "tamanhoecor::Branco - S:0;Branco - M:0;Branco - L:0"

但是,我需要这个:

product_sku     preco                                                      custom_price
"340803 010" Preto - S:0:0;Preto - M:0:1;Preto - L:0:2;Preto - XL:0:3; "tamanhoecor::Preto - S:0:0;Preto - M:0:1;Preto - L:0:2;Preto - XL:0:3"
"340803 100" Branco - S:0:0;Branco - M:0:1;Branco - L:0:2; "tamanhoecor::Branco - S:0:0;Branco - M:0:1;Branco - L:0:2"

我尝试使用我在这里找到的内容: http://msdn.microsoft.com/en-us/library/ms187330%28v=sql.105%29.aspx

我试过 声明@pos nvarchar(30) 选择 .... (@pos + = 1) .... 来自 .... (SELECT @pos=0) ....

但我收到“‘DECLARE’附近的语法错误。需要‘(’、SELECT 或 WITH。”,以及“= 附近的语法错误”

我试过这段代码,它有效:

GO
DECLARE @var1 nvarchar(30)
SELECT @var1 = 'Generic Name'
SELECT @var1 = (
SELECT AppUserName
FROM [AppUsers]
WHERE AppUserID = 1000)
SELECT @var1 AS 'Company Name' ;

如有任何帮助,我将不胜感激。

谢谢。

最佳答案

计数器是什么意思?你想要每行的递增数字吗?如果是,试试这个:

select row_number() over (order by your_column), * 
from your_table

关于sql-server-2008 - 向 SELECT 查询的结果添加一个计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10446431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com