如何连接表,连接一些数据

时间:2016-09-23 08:50:08

标签: sql sql-server join

我有两张桌子:
1.指数和指数数量 2.具有指定框码的索引和索引数量。 Boxcode是一个框,其中包含索引。

.foo, .foo > p{text-align:center;}

我想得到结果:

<div class="foo">
  <p class="foo2">
    <span>
      lorem ipsum dolor sit amet
    </span>
  </p>
</div>

表1中的记录必须采用相同的顺序 我不知道怎么做。
有什么建议吗?

1. input table 1

item_id quantity
1       10
2       15
3       5
1       5
1       5
2       5
3       5

sum:
1 - 20
2 - 20
3 - 10



2. input table 2

item_id quantity    boxcode
1       3           abc
2       2           abc
1       8           def
3       10          ghi
1       9           ghi
2       9           def
2       8           ghi             !!!!!!!

1 item_id once on 1 boxcode

result

谢谢,

2 个答案:

答案 0 :(得分:2)

很奇怪,但它确实有效:

;WITH rec1 AS (
    SELECT  rownum,
            item_id,
            1 as q,
            1 as [Level],
            quantity
    from #input1
    UNION ALL
    SELECT  r.rownum,
            r.item_id,
            1,
            [Level] + 1,
            i.quantity
    FROM rec1 r
    INNER JOIN #input1 i
        ON r.rownum = i.rownum AND r.item_id = i.item_id
    WHERE [Level] < i.quantity
), rec2 AS (
    SELECT  boxcode,
            item_id,
            1 as q,
            1 as [Level],
            quantity
    from #input2
    UNION ALL
    SELECT  r.boxcode,
            r.item_id,
            1,
            [Level] + 1,
            i.quantity
    FROM rec2 r
    INNER JOIN #input2 i
        ON r.boxcode = i.boxcode AND r.item_id = i.item_id
    WHERE [Level] < i.quantity
), cte1 AS (
    SELECT  *,
            ROW_NUMBER() OVER (PARTITION BY item_id ORDER BY item_id, rownum) as rn
    FROM rec1
), cte2 AS (
    SELECT  *,
            ROW_NUMBER() OVER (PARTITION BY item_id ORDER BY item_id, boxcode) as rn
    FROM rec2
), final AS (
    SELECT  c1.rownum,
            c1.item_id,
            c1.quantity,
            c2.boxcode+'/'+CAST(SUM(c2.q) as nvarchar(10)) as boxcodes
    FROM cte1 c1
    INNER JOIN cte2 c2
        ON c1.item_id = c2.item_id and c1.rn = c2.rn
    GROUP BY c1.rownum, c1.item_id, c1.quantity, c2.boxcode
)

SELECT DISTINCT 
                f.rownum,
                f.item_id,
                f.quantity,
                STUFF((
                    SELECT ', '+f1.boxcodes
                    FROM final f1
                    WHERE f1.rownum = f.rownum 
                        AND f1.item_id = f.item_id 
                        AND f1.quantity = f.quantity
                    FOR XML PATH('')
                ),1,2,'') boxcodes
FROM final f

您提供的数据集的输出:

rownum  item_id quantity    boxcodes
1       1       10          abc/3, def/7
2       2       15          abc/2, def/9, ghi/4
3       3       5           ghi/5
4       1       5           def/1, ghi/4
5       1       5           ghi/5
6       2       5           ghi/4
7       3       5           ghi/5

主要思想是在两个表中为小部分1分配数量。然后添加行号,然后加入并获得结果。

答案 1 :(得分:1)

一个解决方案(但是它完全基于gofr1的答案,说实话!),为了简化,可以创建一个Numbers表,其中包含任意数量的数字。

CREATE TABLE Numbers(Number INT PRIMARY KEY);

INSERT Numbers 
SELECT TOP 1000 ROW_NUMBER() OVER (ORDER BY name)
FROM sys.all_columns;

这样可以避免2个递归CTE。

然后您可以使用与gofr1相同的逻辑:

with rec1 AS (
    SELECT  
            ROW_NUMBER() OVER (PARTITION BY item_id ORDER BY item_id, rownum) as rn,
            rownum,
            item_id,
            case when quantity = 0 then 0 else 1 end as q,
            quantity
    from #input1
    join Numbers n on n.Number <= quantity
)

, rec2 AS (
    SELECT  
    ROW_NUMBER() OVER (PARTITION BY item_id ORDER BY item_id, boxcode) as rn,
            boxcode,
            item_id,
            case when quantity = 0 then 0 else 1 end as q,
            quantity
    from #input2
   join Numbers n on n.Number <= quantity
),

 final AS (
    SELECT  c1.rownum,
            c1.item_id,
            c1.quantity,
            c2.boxcode+'/'+CAST(SUM(c2.q) as nvarchar(10)) as boxcodes
    FROM rec1 c1
    INNER JOIN rec2 c2
        ON c1.item_id = c2.item_id and c1.rn = c2.rn
    GROUP BY c1.rownum, c1.item_id, c1.quantity, c2.boxcode
),
stuffed as (
SELECT   
       distinct rownum,       
       f.item_id,
       f.quantity,
       STUFF((
           SELECT ', '+f1.boxcodes
           FROM final f1
           WHERE f1.rownum = f.rownum 
                 AND f1.item_id = f.item_id 
                 AND f1.quantity = f.quantity
           FOR XML PATH('')
       ),1,2,'') boxcodes
FROM final f
group by item_id, quantity, boxcodes, rownum)

select * 
from stuffed
order by rownum