无法在sql查询中应用join

时间:2017-12-27 07:09:18

标签: sql oracle outsystems

我想添加加入"加入{用户} 在{Deck}。[CreatedBy] = {用户}。[Id]" 在以下查询中。我尝试了很多组合但没有成功。我想从用户表中获取名称

    SELECT  @CampaignQueryFilterString AS [Selected],
    {Deck}.[Id], {Deck}.[Name],
    {User}.[Name],
    {Deck}.[TableOfContentId], {Deck}.[CreatedBy],
    {Deck}.[LastModifiedOn], {Deck}.[ExpiryDate],s1.[Count]      
    FROM  {Deck}
    JOIN
    (
        SELECT {Deck}.[Id],
        LISTAGG( {TagValue}.[Id], ',' ) WITHIN GROUP (ORDER BY {TagValue}.[TagCategoryId] ) AS Tags
        FROM {Deck} 
        JOIN {AssociatedDeckTags}
        ON {AssociatedDeckTags}.[DeckId] = {Deck}.[Id]
        JOIN {TagValue}
        ON {AssociatedDeckTags}.[TagValueId] = {TagValue}.[Id]
        WHERE {Deck}.[IsPublished] = 1
                AND {Deck}.[IsActive] = 1 AND {Deck}.[ReplacedByDeckId] IS NULL
                AND {Deck}.[TableOfContentId] IN @TableOfContentIdFilterString
                AND {Deck}.[ContentFileTypeId] IN @AllowedContentType
                AND {Deck}.[ExpiryDate] > SYSDATE
        GROUP BY {Deck}.[Id]       
    ) DeckView
    ON {Deck}.[Id] = DeckView.Id  
    JOIN(
        SELECT COUNT(*) AS [Count], {DeckGroup}.[DeckId] AS [S1DeckId]
        FROM {DeckGroup}
        JOIN {Slide} ON {Slide}.[DeckGroupId] = {DeckGroup}.[Id]
        GROUP BY {DeckGroup}.[DeckId]
    ) s1 ON s1.[S1DeckId] = {Deck}.[Id]
    @RegexString
    @SearchFilter
    @CreatedBy
    GROUP BY 
    {Deck}.[Id], {Deck}.[Name], {Deck}.[TableOfContentId], 
    {Deck}.[CreatedOn], {Deck}.[CreatedBy],
    {Deck}.[LastModifiedOn], {Deck}.[ExpiryDate], 
    {Deck}.[NumOfPreviews], {Deck}.[NumOfDownloads],s1.[Count]        
    ORDER BY @Orderby

1 个答案:

答案 0 :(得分:1)

看起来只是加入,请尝试以下查询(使用您的表示法)。我添加了left joingroup by子句 - {User}.[Name]。您可以选择使用{User}.[Name] - max()listagg()的某些聚合函数,并将其从group by子句中删除。

SELECT @CampaignQueryFilterString AS [Selected],
       {Deck}.[Id], {Deck}.[Name], {User}.[Name], {Deck}.[TableOfContentId], 
       {Deck}.[CreatedBy], {Deck}.[LastModifiedOn], {Deck}.[ExpiryDate], s1.[Count]
  FROM {Deck}
  JOIN DeckView ON {Deck}.[Id] = DeckView.Id            -- subquery1 
  JOIN s1 ON s1.[S1DeckId] = {Deck}.[Id]                -- subquery2 
  LEFT JOIN {User} ON {Deck}.[CreatedBy] = {User}.[Id]  -- <-- add join here
  @RegexString
  @SearchFilter
  @CreatedBy
  GROUP BY 
    {Deck}.[Id], {Deck}.[Name], {User}.[Name],          -- <-- add column here 
    {Deck}.[TableOfContentId], {Deck}.[CreatedOn], 
    {Deck}.[CreatedBy], {Deck}.[LastModifiedOn], 
    {Deck}.[ExpiryDate], {Deck}.[NumOfPreviews], 
    {Deck}.[NumOfDownloads], s1.[Count]
  ORDER BY @Orderby

您没有显示您的尝试,因此我们不知道是否存在错误或不良结果。有了这种形式的问题,我可以提供帮助。另外USEROracle Reserved Words中的一个,最好避免将其用作别名,变量名称等。