0

我已经准备好因为在 SO 上提出我的第一个问题以及什么是可能重复的问题而被钉死在十字架上,但我一生都找不到它。

我有三个表,一个产品表、一个链接表和一个带有名称的子表。预装在SQLFiddle >>如果我仍然引起你的注意。

CREATE TABLE Product (iProductID int NOT NULL PRIMARY KEY
                    , sProductName varchar(50) NOT NULL
                    , iPartGroupID int NOT NULL)
INSERT INTO Product VALUES
(10001, 'Avionic Tackle', '1'),
(10002, 'Eigenspout', '2'),
(10003, 'Impulse Polycatalyst', '3'),
(10004, 'O-webbing', '2'),
(10005, 'Ultraservo', '3'),
(10006, 'Yttrium Coil', '5')

CREATE TABLE PartGroup (iPartGroupID int NOT NULL
                      , iChildID int NOT NULL)
INSERT INTO PartGroup VALUES
(1, 1),
(2, 2),
(3, 1),
(3, 2),
(3, 3),
(3, 4),
(4, 5),
(4, 6),
(5, 1)

CREATE TABLE PartNames (iChildID int NOT NULL PRIMARY KEY
                      , sPartNameText varchar(50) NOT NULL)
INSERT INTO PartNames VALUES
(1, 'Bulbcap Lube'),
(2, 'Chromium Deltaquartz'),
(3, 'Dilation Gyrosphere'),
(4, 'Fliphose'),
(5, 'G-tightener Bypass'),
(6, 'Heisenberg Shuttle')

我试图找出如何列出所有部件组(可能属于也可能不属于产品),并翻译他们的孩子名字。也就是如何只用链接表和子名表列出链接表的所有翻译后的元素。我正在努力寻找孤儿。

我有两个疑问:

SELECT P.iPartGroupID
    ,STUFF(
        (SELECT
            CONCAT(', ', PN.sPartNameText)
            FROM PartGroup PG
            INNER JOIN PartNames PN ON PN.iChildID = PG.iChildID
         WHERE PG.iPartGroupID = P.iPartGroupID
         FOR XML PATH(''), TYPE
        ).value('.', 'VARCHAR(MAX)')
        , 1, 2, ''
        ) AS [Child Elements]
FROM Product P
GROUP BY P.iPartGroupID

这会按名称列出属于产品的所有部件组及其子元素。iPartGroupID = 4 不在这里。

我也有:

SELECT PG.iPartGroupID
    ,STUFF(
        (SELECT
            CONCAT(', ', PGList.iChildID)
            FROM PartGroup PGList
            WHERE PGList.iPartGroupID = PG.iPartGroupID
            FOR XML PATH(''), TYPE
        ).value('.', 'VARCHAR(MAX)')
        , 1, 2, ''
        ) AS [Child Elements]
FROM PartGroup PG
GROUP BY PG.iPartGroupID

This lists all the part groups, and their child elements by code. iPartGroupID = 4 is covered here, but the names aren't translated.

What query can I use to list the orphan part groups (and also the orphan parts):

 4     G-tightener Bypass, Heisenberg Shuttle

Ideally it is included in a list of all the other part groups, but if not, I can union the results.

Every other SO question I've looked up uses either 3 tables, or only 1 table, self joining with aliases. Does anyone have any ideas?

No XML in the part names, no particular preference for CONCAT or SELECT '+'.

I would link to other posts, but I can't without points :(

4

2 回答 2

0

I'm not entirely sure what do you mean, exactly, when you use the word "translate". And your required output seems to contradict your sample data (if I'm not lost something).

Nevertheless, try this query, maybe it's what you need:

select sq.iPartGroupID, cast((
    select pn.sPartNameText + ',' as [data()] from @PartNames pn
        inner join @PartGroup p on pn.iChildID = p.iChildID
    where p.iPartGroupID = sq.iPartGroupID
    order by pn.iChildID
    for xml path('')
    ) as varchar(max)) as [GroupList]
from (select distinct pg.iPartGroupID from @PartGroup pg) sq
    left join @Product pr on sq.iPartGroupID = pr.iPartGroupID
where pr.iProductID is null;
于 2014-08-13T07:00:25.133 回答
0

Following way you can use to get the answer you want

SELECT pg.iPartGroupID,
   CASE COUNT(pg.iPartGroupID)
        WHEN 1 THEN (
                 SELECT pn2.sPartNameText
                 FROM   PartNames pn2
                 WHERE  pn2.iChildID = pg.iPartGroupID
             )
        ELSE (
                 SELECT CASE ROW_NUMBER() OVER(ORDER BY(SELECT 1))
                             WHEN 1 THEN ''
                             ELSE ','
                        END + pn2.sPartNameText
                 FROM   PartNames pn2
                        INNER JOIN PartGroup pg2
                             ON  pg2.iChildID = pn2.iChildID
                 WHERE  pg2.iPartGroupID = pg.iPartGroupID
                        FOR XML PATH('')
             )
   END
FROM   PartGroup pg
GROUP BY
   pg.iPartGroupID
于 2014-08-13T13:07:22.080 回答