首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将行转换为列Server 2012

将行转换为列Server 2012
EN

Stack Overflow用户
提问于 2015-02-05 21:44:04
回答 1查看 46关注 0票数 0

我有一个查询,它返回一组记录,这是食谱信息。我的疑问是

代码语言:javascript
复制
    SELECT distinct RIG.RecipeNumber,  RIG.Serving_Size_Yield_Quantity as 'Recipe Yield', RCS.Description as 'Serving Size', rcs.Quantity as 'Quantity to Serve',
mri.MenuCountType_Description
FROM RecipeIngredientInfo RIG JOIN RECIPE_SERVINGSIZE RCS ON
RIG.RECIPE_RECID = RCS.RecipeID  
JOIN MenuRecipeInfo MRI ON MRI.RecipeID =  RIG.RECIPE_RECID
WHERE MenuCountType_Description is NOT NULL AND RIG.RecipeNumber ='___  900324'
Order By RecipeNumber

查询返回的格式如下:

我希望数据的格式如下:按RecipeNumber分组,Servig,然后MenuCountType_Description应该是列。

获取MenuCountType_Descritions的查询是:

代码语言:javascript
复制
select DISTINCT MenuCountType_Description
from MenuRecipeInfo WHERE MenuCountType_Description IS NOT NULL

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-05 22:17:19

我想我已经接近这个查询了

代码语言:javascript
复制
SELECT 
    *
    from
    (
    SELECT distinct RIG.RecipeNumber,  RIG.Serving_Size_Yield_Quantity as 'Recipe Yield', RCS.Description as 'ServingSize', rcs.Quantity as 'Quantity to Serve', 'X' flag,
    mri.MenuCountType_Description
    FROM RecipeIngredientInfo RIG JOIN RECIPE_SERVINGSIZE RCS ON
    RIG.RECIPE_RECID = RCS.RecipeID  
    JOIN MenuRecipeInfo MRI ON MRI.RecipeID =  RIG.RECIPE_RECID
    WHERE MenuCountType_Description is NOT NULL AND RIG.RecipeNumber ='___  900324'
    ) SRC
    pivot
    (
    max(flag)
    for MenuCountType_Description in ([LYFE], [Grades 6-8], Supper,Snack, Employees, [Adults/Visitors], [Grades K-8], [Grades 9-12])
    ) piv;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28354651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档