从单个列中选择多个值

时间:2013-03-15 10:32:38

标签: sql

如果现在这个查询已经整整一天了,我似乎无法理解它。如果也许你可以指出我正确的方向或告诉我我想要寻找的功能将意味着很多。 我试过通过SQL中的函数来查看Pivot,Case,Union和Group,但没有运气......但它看起来很简单

我从多个表中选择。 其中一个表包含一行,其中包含我需要在select中返回的多个值。

SQL:

(SELECT        ONYAK_SIGMAPRO_Issues.IssueId, 
                           ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldValue AS [Work Ticket], 
                           ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldValue AS [Customer Name],
                           ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldValue AS [Job Description],
                           ONYAK_SIGMAPRO_ProjectCategories.CategoryName AS [Printer], 
                           ONYAK_SIGMAPRO_Issues.IssueTitle AS [Task Description], 
                ONYAK_SIGMAPRO_Issues.Progress, 
                           ONYAK_SIGMAPRO_ProjectStatus.StatusName AS [Status], 
                           ONYAK_SIGMAPRO_ProjectPriorities.PriorityName AS [Priority], 
                ONYAK_SIGMAPRO_Issues.DateDue
FROM            (ONYAK_SIGMAPRO_Issues INNER JOIN
                         ONYAK_SIGMAPRO_ProjectCustomFieldValues ON ONYAK_SIGMAPRO_Issues.IssueId = ONYAK_SIGMAPRO_ProjectCustomFieldValues.IssueId INNER JOIN
                         ONYAK_SIGMAPRO_ProjectCustomFields ON 
                         ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldId = ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId AND 
                         ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectCustomFields.ProjectId INNER JOIN
                         ONYAK_SIGMAPRO_ProjectStatus ON ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectStatus.ProjectId AND 
                         ONYAK_SIGMAPRO_Issues.IssueStatusId = ONYAK_SIGMAPRO_ProjectStatus.StatusId INNER JOIN
                         ONYAK_SIGMAPRO_ProjectPriorities ON ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectPriorities.ProjectId AND 
                         ONYAK_SIGMAPRO_Issues.IssuePriorityId = ONYAK_SIGMAPRO_ProjectPriorities.PriorityId INNER JOIN
                         ONYAK_SIGMAPRO_ProjectCategories ON ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectCategories.ProjectId AND 
                         ONYAK_SIGMAPRO_Issues.IssueCategoryId = ONYAK_SIGMAPRO_ProjectCategories.CategoryId
                                         ) 
WHERE        ONYAK_SIGMAPRO_Issues.ProjectId = 2
AND ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId = 1

UNION all

SELECT        ONYAK_SIGMAPRO_Issues.IssueId, 
                           ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldValue AS [Work Ticket], 
                           ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldValue AS [Customer Name],
                           ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldValue AS [Job Description],
                           ONYAK_SIGMAPRO_ProjectCategories.CategoryName AS [Printer], 
                           ONYAK_SIGMAPRO_Issues.IssueTitle AS [Task Description], 
                ONYAK_SIGMAPRO_Issues.Progress, 
                           ONYAK_SIGMAPRO_ProjectStatus.StatusName AS [Status], 
                           ONYAK_SIGMAPRO_ProjectPriorities.PriorityName AS [Priority], 
                ONYAK_SIGMAPRO_Issues.DateDue
FROM            ONYAK_SIGMAPRO_Issues INNER JOIN
                         ONYAK_SIGMAPRO_ProjectCustomFieldValues ON ONYAK_SIGMAPRO_Issues.IssueId = ONYAK_SIGMAPRO_ProjectCustomFieldValues.IssueId INNER JOIN
                         ONYAK_SIGMAPRO_ProjectCustomFields ON 
                         ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldId = ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId AND 
                         ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectCustomFields.ProjectId INNER JOIN
                         ONYAK_SIGMAPRO_ProjectStatus ON ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectStatus.ProjectId AND 
                         ONYAK_SIGMAPRO_Issues.IssueStatusId = ONYAK_SIGMAPRO_ProjectStatus.StatusId INNER JOIN
                         ONYAK_SIGMAPRO_ProjectPriorities ON ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectPriorities.ProjectId AND 
                         ONYAK_SIGMAPRO_Issues.IssuePriorityId = ONYAK_SIGMAPRO_ProjectPriorities.PriorityId INNER JOIN
                         ONYAK_SIGMAPRO_ProjectCategories ON ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectCategories.ProjectId AND 
                         ONYAK_SIGMAPRO_Issues.IssueCategoryId = ONYAK_SIGMAPRO_ProjectCategories.CategoryId
WHERE        ONYAK_SIGMAPRO_Issues.ProjectId = 2
AND ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId = 4

UNION all

SELECT        ONYAK_SIGMAPRO_Issues.IssueId, 
                           ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldValue AS [Work Ticket], 
                           ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldValue AS [Customer Name],
                           ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldValue AS [Job Description],
                           ONYAK_SIGMAPRO_ProjectCategories.CategoryName AS [Printer], 
                           ONYAK_SIGMAPRO_Issues.IssueTitle AS [Task Description], 
                ONYAK_SIGMAPRO_Issues.Progress, 
                           ONYAK_SIGMAPRO_ProjectStatus.StatusName AS [Status], 
                           ONYAK_SIGMAPRO_ProjectPriorities.PriorityName AS [Priority], 
                ONYAK_SIGMAPRO_Issues.DateDue
FROM            ONYAK_SIGMAPRO_Issues INNER JOIN
                         ONYAK_SIGMAPRO_ProjectCustomFieldValues ON ONYAK_SIGMAPRO_Issues.IssueId = ONYAK_SIGMAPRO_ProjectCustomFieldValues.IssueId INNER JOIN
                         ONYAK_SIGMAPRO_ProjectCustomFields ON 
                         ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldId = ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId AND 
                         ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectCustomFields.ProjectId INNER JOIN
                         ONYAK_SIGMAPRO_ProjectStatus ON ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectStatus.ProjectId AND 
                         ONYAK_SIGMAPRO_Issues.IssueStatusId = ONYAK_SIGMAPRO_ProjectStatus.StatusId INNER JOIN
                         ONYAK_SIGMAPRO_ProjectPriorities ON ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectPriorities.ProjectId AND 
                         ONYAK_SIGMAPRO_Issues.IssuePriorityId = ONYAK_SIGMAPRO_ProjectPriorities.PriorityId INNER JOIN
                         ONYAK_SIGMAPRO_ProjectCategories ON ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectCategories.ProjectId AND 
                         ONYAK_SIGMAPRO_Issues.IssueCategoryId = ONYAK_SIGMAPRO_ProjectCategories.CategoryId
WHERE        ONYAK_SIGMAPRO_Issues.ProjectId = 2
AND ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId = 9)
ORDER BY ONYAK_SIGMAPRO_Issues.IssueId

结果:

IssueId Work Ticket  Customer Name   Job Description Printer Task Description    Progress    Status  Priority    DateDue
1070    8828    8828    8828    Heidelberg  brochure    0   Art Work    High    00:00.0
1070    brochures   brochures   brochures   Heidelberg  brochure    0   Art Work    High    00:00.0
1070    Liberty Life    Liberty Life    Liberty Life    Heidelberg  brochure    0   Art Work    High    00:00.0
1082    Liberty Life    Liberty Life    Liberty Life    Heidelberg  (brochures  0   Art Work    High    00:00.0
1082    brochures   brochures   brochures   Heidelberg  (brochures  0   Art Work    High    00:00.0
1082    8294    8294    8294    Heidelberg  (brochures  0   Art Work    High    00:00.0
1086    8295    8295    8295    Heidelberg  (brochures  0   Art Work    High    00:00.0
1086    stable growth brochures     stable growth brochures     stable growth brochures     Heidelberg  (brochures  0   Art Work    High    00:00.0
1086    Liberty Life    Liberty Life    Liberty Life    Heidelberg  (brochures  0   Art Work    High    00:00.0
1090    Liberty Life    Liberty Life    Liberty Life    Kamori  (VAS  Card  0   Art Work    High    00:00.0
1090    VAS  cards  VAS  cards  VAS  cards  Kamori  (VAS  Card  0   Art Work    High    00:00.0
1090    8296    8296    8296    Kamori  (VAS  Card  0   Art Work    High    00:00.0
1104    8300    8300    8300    Heidelberg  Menu text   0   Art Work    High    00:00.0
1104    menu text   menu text   menu text   Heidelberg  Menu text   0   Art Work    High    00:00.0
1104    South African Airways   South African Airways   South African Airways   Heidelberg  Menu text   0   Art Work    High    00:00.0

Bold专栏给我带来了麻烦。

我希望select返回以下内容:

IssueId Work Ticket Customer Name   Job Description Printer Task Description    Progress    Status  Priority    DateDue
1070    8828    Liberty Life    brochures   Heidelberg  brochure    0   Art Work    High    00:00.0
1082    8294    8294    brochures   Heidelberg  (brochures  0   Art Work    High    00:00.0
1086    8295    Liberty Life    Liberty Life    Heidelberg  (brochures  0   Art Work    High    00:00.0
1090    8296    Liberty Life    VAS  cards  Kamori  (VAS  Card  0   Art Work    High    00:00.0
1104    8300    South African Airways   menu text   Heidelberg  Menu text   0   Art Work    High    00:00.0

我可以使用哪种功能或类型的选择?

2 个答案:

答案 0 :(得分:0)

我还在努力,但我要说的第一件事就是在这种情况下你不需要继续使用UNION ALL(除非我错过了一些东西)...只需使用一个SELECT查询(没有联合)并将WHERE子句更改为如下所示:

WHERE ONYAK_SIGMAPRO_Issues.ProjectId = 2
AND ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId IN (1,4,9)

先尝试一下然后看看你得到了什么......然后告诉我们

修改

根据您在指定链接上发布的屏幕截图(使问题更容易查看),我发现您需要使用DISTINCT关键字。基本上:SELECT DISTINCT等...

答案 1 :(得分:0)

@Matt简单地使用DISTINCT返回与之前相同的结果。这是因为我在select中选择“CustomFieldId”列3次,而不是“IssueId”。但是当我在选择中使用子查询时,我得到了我想要的结果。

SELECT DISTINCT(ONYAK_SIGMAPRO_Issues.IssueId), 
    (SELECT ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldValue
    FROM ONYAK_SIGMAPRO_ProjectCustomFieldValues INNER JOIN
        ONYAK_SIGMAPRO_Issues ON ONYAK_SIGMAPRO_ProjectCustomFieldValues.IssueId = ONYAK_SIGMAPRO_Issues.IssueId INNER JOIN
        ONYAK_SIGMAPRO_ProjectCustomFields ON 
        ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldId = ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId
            WHERE (ONYAK_SIGMAPRO_Issues.ProjectId = 2) AND (ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId = 1) AND ONYAK_SIGMAPRO_Issues.IssueId = 1070)  AS [Work Ticket], 
    (SELECT ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldValue
    FROM ONYAK_SIGMAPRO_ProjectCustomFieldValues INNER JOIN
        ONYAK_SIGMAPRO_Issues ON ONYAK_SIGMAPRO_ProjectCustomFieldValues.IssueId = ONYAK_SIGMAPRO_Issues.IssueId INNER JOIN
        ONYAK_SIGMAPRO_ProjectCustomFields ON 
        ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldId = ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId
            WHERE (ONYAK_SIGMAPRO_Issues.ProjectId = 2) AND (ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId = 4) AND ONYAK_SIGMAPRO_Issues.IssueId = 1070) AS [Customer Name],
    (SELECT ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldValue
    FROM ONYAK_SIGMAPRO_ProjectCustomFieldValues INNER JOIN
        ONYAK_SIGMAPRO_Issues ON ONYAK_SIGMAPRO_ProjectCustomFieldValues.IssueId = ONYAK_SIGMAPRO_Issues.IssueId INNER JOIN
        ONYAK_SIGMAPRO_ProjectCustomFields ON 
        ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldId = ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId
            WHERE (ONYAK_SIGMAPRO_Issues.ProjectId = 2) AND (ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId = 9) AND ONYAK_SIGMAPRO_Issues.IssueId = 1070) AS [Job Description],
    ONYAK_SIGMAPRO_ProjectCategories.CategoryName AS [Printer], 
    ONYAK_SIGMAPRO_Issues.IssueTitle AS [Task Description], 
    ONYAK_SIGMAPRO_Issues.Progress, 
    ONYAK_SIGMAPRO_ProjectStatus.StatusName AS [Status], 
    ONYAK_SIGMAPRO_ProjectPriorities.PriorityName AS [Priority], 
    ONYAK_SIGMAPRO_Issues.DateDue
        FROM ONYAK_SIGMAPRO_Issues INNER JOIN
        ONYAK_SIGMAPRO_ProjectCustomFieldValues ON 
        ONYAK_SIGMAPRO_Issues.IssueId = ONYAK_SIGMAPRO_ProjectCustomFieldValues.IssueId INNER JOIN
        ONYAK_SIGMAPRO_ProjectCustomFields ON 
        ONYAK_SIGMAPRO_ProjectCustomFieldValues.CustomFieldId = ONYAK_SIGMAPRO_ProjectCustomFields.CustomFieldId AND 
        ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectCustomFields.ProjectId INNER JOIN
        ONYAK_SIGMAPRO_ProjectStatus ON 
        ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectStatus.ProjectId AND 
        ONYAK_SIGMAPRO_Issues.IssueStatusId = ONYAK_SIGMAPRO_ProjectStatus.StatusId INNER JOIN
        ONYAK_SIGMAPRO_ProjectPriorities ON 
        ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectPriorities.ProjectId AND 
        ONYAK_SIGMAPRO_Issues.IssuePriorityId = ONYAK_SIGMAPRO_ProjectPriorities.PriorityId INNER JOIN
        ONYAK_SIGMAPRO_ProjectCategories ON 
        ONYAK_SIGMAPRO_Issues.ProjectId = ONYAK_SIGMAPRO_ProjectCategories.ProjectId AND 
        ONYAK_SIGMAPRO_Issues.IssueCategoryId = ONYAK_SIGMAPRO_ProjectCategories.CategoryId  
            WHERE ONYAK_SIGMAPRO_Issues.ProjectId = 2
            AND ONYAK_SIGMAPRO_Issues.IssueId = 1070

我怎么需要显示所有数据而不只是一个ID。

我确实通过在3个不同的临时表中选择“CustomFieldId”解决了这个问题,然后使用最终选择我将所有表结合起来...这正是我想要的结果:)