返回外部的存储过程选择与内部选择相同的次数

时间:2016-10-12 09:01:11

标签: xml tsql stored-procedures

运行存储过程以返回数据库中的所有记录(本例中为手机)及其颜色选项。

问题是当手机有4种颜色时,它也会返回ModelGroup 4次。

我知道这可能是显而易见的事情,或者我是否需要先把它们放到桌子上?

存储过程:

SELECT 

PB.ModelGroup as '@group',

    (SELECT

        PB1.UID as '@pid',
        PB1.Brand as '@brand',
        PB1.Title as '@title',
        PB1.Friendly_URL_Name as '@url',
        PB1.Camera as '@camera',
        PB1.Storage as '@storage',
        PB1.Screen_Size as '@screensize',
        PB1.OS as '@os',
        PB1.Processor as '@chip',
        PB1.Image1 as '@image',
        CB.Colour as '@colour'

    FROM dbo.Phone_Base PB1
    INNER JOIN dbo.Colour_Base CB ON CB.UID = PB1.Colour
    WHERE PB1.Active = 1 AND PB.Title = PB1.Title
    FOR XML PATH('colours'), TYPE
    )

FROM dbo.Phone_Base PB
WHERE PB.Active = 1
ORDER BY PB.ModelGroup

FOR XML PATH('phone'), TYPE

XML输出:

<phone group="iphone6s16gb">
<colours pid="16" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Space-Grey" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-space-grey.png" colour="Space Grey" />
<colours pid="17" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Gold" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-gold.png" colour="Gold" />
<colours pid="18" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Rose-Gold" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-rose-gold.png" colour="Rose Gold" />
<colours pid="19" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Silver" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-silver.png" colour="Silver" />
</phone>
<phone group="iphone6s16gb">
<colours pid="16" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Space-Grey" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-space-grey.png" colour="Space Grey" />
<colours pid="17" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Gold" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-gold.png" colour="Gold" />
<colours pid="18" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Rose-Gold" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-rose-gold.png" colour="Rose Gold" />
<colours pid="19" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Silver" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-silver.png" colour="Silver" />
</phone>
<phone group="iphone6s16gb">
<colours pid="16" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Space-Grey" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-space-grey.png" colour="Space Grey" />
<colours pid="17" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Gold" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-gold.png" colour="Gold" />
<colours pid="18" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Rose-Gold" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-rose-gold.png" colour="Rose Gold" />
<colours pid="19" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Silver" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-silver.png" colour="Silver" />
</phone>
<phone group="iphone6s16gb">
<colours pid="16" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Space-Grey" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-space-grey.png" colour="Space Grey" />
<colours pid="17" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Gold" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-gold.png" colour="Gold" />
<colours pid="18" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Rose-Gold" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-rose-gold.png" colour="Rose Gold" />
<colours pid="19" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Silver" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-silver.png" colour="Silver" />
</phone>

预期的XML结果:

<phone group="iphone6s16gb">
<colours pid="16" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Space-Grey" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-space-grey.png" colour="Space Grey" />
<colours pid="17" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Gold" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-gold.png" colour="Gold" />
<colours pid="18" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Rose-Gold" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-rose-gold.png" colour="Rose Gold" />
<colours pid="19" brand="1" title="iPhone 6S 16GB" url="iPhone-6S-16GB-Silver" camera="12" storage="16GB" screensize="4.7" os="iOS" chip="A9" image="iphone6s-silver.png" colour="Silver" />
</phone>

表数据:

UID Product_Code    SKU           Brand ModelGroup  Title   Short_Title Friendly_URL_Name   Colour  Description Storage Camera  Processor   Screen_Size OS  Extra_Feature   Image1  Image2  Image3  Image4  Trade_Price Network Active  Featured    Created_By  Created Edited_By   Edited  Model
16  IPHONE6S16GBSPACEGREY   NULL    1   iphone6s16gb    iPhone 6S 16GB  iPhone 6S 16GB Space Grey   iPhone-6S-16GB-Space-Grey   3   he moment you use iPhone 6s, you know you’ve never felt anything like it. With just a single press, 3D Touch lets you do more than ever before. Live Photos bring your memories to life in a powerfully vivid way. And that’s just the beginning. Take a deeper look at iPhone 6s, and you’ll find innovation on every level.   16GB    12  A9  4.7 iOS     iphone6s-space-grey.png NULL    NULL    NULL    420.00  1   1   0   1   2016-08-24 14:52:00 NULL    NULL    iPhone6
17  IPHONE6S16GBGOLD    NULL    1   iphone6s16gb    iPhone 6S 16GB  iPhone 6S 16GB Gold iPhone-6S-16GB-Gold 5   he moment you use iPhone 6s, you know you’ve never felt anything like it. With just a single press, 3D Touch lets you do more than ever before. Live Photos bring your memories to life in a powerfully vivid way. And that’s just the beginning. Take a deeper look at iPhone 6s, and you’ll find innovation on every level.   16GB    12  A9  4.7 iOS     iphone6s-gold.png   NULL    NULL    NULL    420.00  1   1   0   1   2016-08-24 14:58:00 NULL    NULL    iPhone6
18  IPHONE6S16GBROSEGOLD    NULL    1   iphone6s16gb    iPhone 6S 16GB  iPhone 6S 16GB Rose Gold    iPhone-6S-16GB-Rose-Gold    6   The moment you use iPhone 6s, you know you’ve never felt anything like it. With just a single press, 3D Touch lets you do more than ever before. Live Photos bring your memories to life in a powerfully vivid way. And that’s just the beginning. Take a deeper look at iPhone 6s, and you’ll find innovation on every level.  16GB    12  A9  4.7 iOS     iphone6s-rose-gold.png  NULL    NULL    NULL    420.00  1   1   0   1   2016-08-24 15:05:00 NULL    NULL    iPhone6
19  IPHONE6S16GBSILVER  NULL    1   iphone6s16gb    iPhone 6S 16GB  iPhone 6S 16GB Silver   iPhone-6S-16GB-Silver   4   The moment you use iPhone 6s, you know you’ve never felt anything like it. With just a single press, 3D Touch lets you do more than ever before. Live Photos bring your memories to life in a powerfully vivid way. And that’s just the beginning. Take a deeper look at iPhone 6s, and you’ll find innovation on every level.  16GB    12  A9  4.7 iOS     iphone6s-silver.png NULL    NULL    NULL    420.00  1   1   0   1   2016-08-24 15:06:00 NULL    NULL    iPhone6

2 个答案:

答案 0 :(得分:1)

如果没有实际的桌子,这就是阅读魔术玻璃灯泡,但它可能是这样的:

SELECT 

PB.ModelGroup as '@group',

    (SELECT

        PB1.UID as '@pid',
        PB1.Brand as '@brand',
        PB1.Title as '@title',
        PB1.Friendly_URL_Name as '@url',
        PB1.Camera as '@camera',
        PB1.Storage as '@storage',
        PB1.Screen_Size as '@screensize',
        PB1.OS as '@os',
        PB1.Processor as '@chip',
        PB1.Image1 as '@image',
        CB.Colour as '@colour'

    FROM dbo.Phone_Base PB1
    INNER JOIN dbo.Colour_Base CB ON CB.UID = PB1.Colour
    WHERE PB1.ModelGroup=PB.ModelGroup
      AND PB1.Active = 1 
      AND PB.Title = PB1.Title
    FOR XML PATH('colours'), TYPE
    )

FROM dbo.Phone_Base PB
WHERE PB.Active = 1
GROUP BY PB.ModelGroup

FOR XML PATH('phone'), TYPE

GROUP BY会将每个ModelGroup的外部SELECT减少到一行。子选择将ModelGroup添加到WHERE - 子句,因此仅返回相关的行...

答案 1 :(得分:0)

最终和工作存储过程感谢@Shnugo指针!

SELECT 
    PB.Title as '@group',
    (SELECT
        PB1.UID as '@pid',
        PB1.Brand as '@brand',
        PB1.Title as '@title',
        PB1.Friendly_URL_Name as '@url',
        PB1.Camera as '@camera',
        PB1.Storage as '@storage',
        PB1.Screen_Size as '@screensize',
        PB1.OS as '@os',
        PB1.Processor as '@chip',
        PB1.Image1 as '@image',
        CB.Colour as '@colour'
    FROM 
        dbo.Phone_Base PB1
    INNER JOIN 
        dbo.Colour_Base CB ON CB.UID = PB1.Colour
    WHERE 
        PB1.Active = 1 AND PB.Title = PB1.Title
    FOR XML PATH('colours'), TYPE
    )
FROM 
    dbo.Phone_Base PB
WHERE 
    PB.Active = 1
GROUP BY 
    PB.Title
FOR XML PATH('phone'), TYPE