SQL TOP和Join挑战

时间:2015-06-26 10:59:22

标签: sql sql-server join

我在SQL中遇到TOPJOIN的问题。

我有两张表INVENTTABLEIKMTECHSPECPROD

INVENTTABLE包含我们拥有的所有产品。

IKMTECHSPECPROD包含产品的所有技术规格。

单个产品可能有许多技术规格。现在我想导出每个产品的所有产品和12个第一技术规格,我希望每个产品在一行上列出

示例:

ItemID, ItemName, spec1name, spec1value, spec2name, spec2value, spec3name, spec3 value.....

我在下面尝试过SQL查询,但它给了我:

ItemID, ItemName, spec1name, spec2value
ItemID, ItemName, spec2name, spec2value 
ItemID, ItemName, spec3name, spec3value

查询:

SELECT 
    invent.ITEMID, ITEMNAME, 
    [techspec].NAME, [techspec].VALUE
FROM 
    [INVENTTABLE] invent
CROSS APPLY 
    (SELECT TOP 12 
         [IKMTECHSPECPROD].NAME, IKMTECHSPECPROD.VALUE
     FROM [IKMTECHSPECPROD]
     WHERE [IKMTECHSPECPROD].ITEMID = invent.ITEMID) techspec

任何人都知道如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

您可以将查询编写为:

 SELECT DISTINCT
    T2.ItemID,
    STUFF
    (
        (
            SELECT TOP 12 ',' + specifications
            FROM @IKMTECHSPECPROD T1
            WHERE T1.ItemID = T2.ItemID
            ORDER BY specifications
            FOR XML PATH('')
        ), 1, 1, ''
    ) AS specs
FROM
    @INVENTTABLE T2

DEMO

答案 1 :(得分:0)

我认为你可以使用group by

SELECT 
    invent.ITEMID, ITEMNAME, 
    [techspec].NAME, [techspec].VALUE
FROM 
    [INVENTTABLE] invent
CROSS APPLY 
    (SELECT TOP 12 
         [IKMTECHSPECPROD].NAME, IKMTECHSPECPROD.VALUE
     FROM [IKMTECHSPECPROD]
     WHERE [IKMTECHSPECPROD].ITEMID = invent.ITEMID
     group by [IKMTECHSPECPROD].NAME, IKMTECHSPECPROD.VALUE
) techspec

答案 2 :(得分:0)

如果你想为每个12个规格名称和值分别使用列,那么这也可以做到:

IF OBJECT_ID('MyTable','U') IS NOT NULL 
    DROP TABLE MyTable
GO
CREATE TABLE MyTable
(
   ItemID    INT,
   ItemName  VARCHAR(30),
   SpecName  VARCHAR(30),
   SpecValue VARCHAR(30)
)
GO
INSERT INTO MyTable
            (ItemID, ItemName, SpecName, SpecValue)
VALUES      (1, 'Item1', 'Item1Spec1', 'Item1Value1'),
            (1, 'Item1', 'Item1Spec2', 'Item1Value2'),
            (1, 'Item1', 'Item1Spec3', 'Item1Value3'),
            (1, 'Item1', 'Item1Spec4', 'Item1Value4'),
            (2, 'Item2', 'Item2Spec1', 'Item2Value1'),
            (2, 'Item2', 'Item2Spec2', 'Item2Value2'),
            (3, 'Item3', 'Item3Spec1', 'Item3Value1'),
            (3, 'Item3', 'Item3Spec2', 'Item3Value2'),
            (3, 'Item3', 'Item3Spec3', 'Item3Value3'),
            (4, 'Item4', 'Item4Spec1', 'Item4Value1')


SELECT ItemID,ItemName, 
MAX(CASE WHEN ItemSpecNo = 1 THEN SpecName END) AS [spec1name], 
MAX(CASE WHEN ItemSpecNo = 1 THEN SpecValue END) AS [spec1value],
MAX(CASE WHEN ItemSpecNo = 2 THEN SpecName END) AS [spec2name], 
MAX(CASE WHEN ItemSpecNo = 2 THEN SpecValue END) AS [spec2value],
MAX(CASE WHEN ItemSpecNo = 3 THEN SpecName END) AS [spec3name], 
MAX(CASE WHEN ItemSpecNo = 3 THEN SpecValue END) AS [spec3value],
MAX(CASE WHEN ItemSpecNo = 4 THEN SpecName END) AS [spec4name], 
MAX(CASE WHEN ItemSpecNo = 4 THEN SpecValue END) AS [spec4value],
MAX(CASE WHEN ItemSpecNo = 5 THEN SpecName END) AS [spec5name], 
MAX(CASE WHEN ItemSpecNo = 5 THEN SpecValue END) AS [spec5value],
MAX(CASE WHEN ItemSpecNo = 6 THEN SpecName END) AS [spec6name], 
MAX(CASE WHEN ItemSpecNo = 6 THEN SpecValue END) AS [spec6value],
MAX(CASE WHEN ItemSpecNo = 7 THEN SpecName END) AS [spec7name], 
MAX(CASE WHEN ItemSpecNo = 7 THEN SpecValue END) AS [spec7value],
MAX(CASE WHEN ItemSpecNo = 8 THEN SpecName END) AS [spec8name], 
MAX(CASE WHEN ItemSpecNo = 8 THEN SpecValue END) AS [spec8value],
MAX(CASE WHEN ItemSpecNo = 9 THEN SpecName END) AS [spec9name], 
MAX(CASE WHEN ItemSpecNo = 9 THEN SpecValue END) AS [spec9value],
MAX(CASE WHEN ItemSpecNo = 10 THEN SpecName END) AS [spec10name], 
MAX(CASE WHEN ItemSpecNo = 10 THEN SpecValue END) AS [spec10value],
MAX(CASE WHEN ItemSpecNo = 11 THEN SpecName END) AS [spec11name], 
MAX(CASE WHEN ItemSpecNo = 11 THEN SpecValue END) AS [spec11value],
MAX(CASE WHEN ItemSpecNo = 12 THEN SpecName END) AS [spec12name], 
MAX(CASE WHEN ItemSpecNo = 12 THEN SpecValue END) AS [spec12value]
FROM
(
SELECT *,ROW_NUMBER()OVER (PARTITION BY itemID ORDER BY itemID) AS ItemSpecNo FROM MyTable
) t
GROUP  BY ItemID,ItemName