如何使用内部联接将数字1,2,3设置为子表数据

时间:2018-02-12 18:14:42

标签: sql sql-server sql-server-2012

我的查询看起来像这样

        select 
        SKU+'_'+'Attribute' as AttributeName,
        SKU as AttributeCode,
        SKU as AttributeKeyName,
        'Select Colors' as AttributeDisplayName,
        sc.PRIMARYOMSCODE as  AttributeValueName,
        cl.PMSCOLOR as AttributeValueCode,
        cl.RGBCOLOR as AttributeValueKeyValue,
        scl.NAME as AttributeValueDisplayName,
        '' as AttributeValueDisplayOrder,



        SKU+'_'+sc.PRIMARYOMSCODE+'.png' as AttributeValueImageName,
        'All' as AttributeValueCategory,
        'FALSE' as IsHideInpreview

         from con.style s 
        inner join con.styleColor sc on s.STYLEID = sc.STYLEID
        inner join con.color cl on sc.PRIMARYCOLORID = cl.COLORID
        inner join con.styleColorLanguage scl on scl.STYLECOLORID = sc.STYLECOLORID
        where s.SKU in ('805B3','804DK','802ZW') and sc.DELETEIND= 0 

预期结果 我期待一个像下面突出显示的列的结果。你能帮我么?

enter image description here

3 个答案:

答案 0 :(得分:3)

如果您尝试在MSSQLSERVER中执行此操作,则可以使用ROW_NUMBER函数。

enter image description here

在你的情况下,

SELECT ROW_NUMBER() OVER(PARTITION BY AttributeCode ORDER BY AttributeName (or anything else)) AS AttributeValueDisplayOrder...

答案 1 :(得分:1)

您可以使用ROW_NUMBER()窗口函数。

添加新列:

ROW_NUMBER() OVER (PARTITION BY SKU ORDER BY SKU) AS AttributeValueDisplay

您的查询中没有订单,如果您需要在ORDER BY SKU, AnotherField

之后订购结果添加相应的字段

答案 2 :(得分:0)

我想标签上写的是SQL_Server,但对于未来的读者来说,如果使用MySQL,这个解决方案应该可行:

来自http://www.mysqltutorial.org/mysql-row_number/

您可以尝试以下方式:

SET @row_number = 0;

SELECT
  ...
  (((@row_number:=@row_number + 1) mod 7) + 1) AS AttributeValueDisplayOrder,
  ...

@row_number正在递增,然后除以7并取余数,然后加1(所以你有1..7而不是0..6)

P.S。请注意第一行中的;,这很重要。