结合没有Aggregate?

时间:2012-10-15 17:00:34

标签: sql sql-server tsql select

长话短说我对T-SQL很陌生,我正在为我们的调度部门查询JD Edwards的一些信息。我们是一家制造工厂,包含我们的零件信息的表格以及制作每个零件的组件对于所有组件使用单个列,每个组件有多行,因此我们可以有一个包含20行的单个零件,我有两个这个查询的问题让我有点难过,首先是我希望每个工单看到一行,其中包含所有的零件信息,来自WO表我在下面的查询中使用了Finished部件号:

来自该WO& amp;的必要列的原始数据。成品:

WALITM                      IXLITM                      WADOCO
TF2-10426IA                 CF-S1094P-UL                800059
TF2-10426IA                 CF-S1094P-UL                800059
TF2-10426IA                 CF-S1094P-UL                800059
TF2-10426IA                 CF-S1094P-UL                800059
TF2-10426IA                 TE2-06718                   800059
TF2-10426IA                 TE2-06718                   800059
TF2-10426IA                 TE2-06718                   800059
TF2-10426IA                 TE2-06718                   800059
TF2-10426IA                 RP-TR23                     800059
TF2-10426IA                 RP-TR23                     800059
TF2-10426IA                 RP-TR23                     800059
TF2-10426IA                 RP-TR23                     800059
TF2-10426IA                 RP-BX35                     800059
TF2-10426IA                 RP-BX35                     800059
TF2-10426IA                 RP-BX35                     800059
TF2-10426IA                 RP-BX35                     800059

(很抱歉,今天显然是完整的n00b,除了创建HTML表格外,还无法弄清楚如何将其格式化为表格)

    SELECT WO, TF, TE, CAP, LABEL
    FROM (SELECT DISTINCT Sched.dbo.TBS.WADOCO AS WO,
    Sched.dbo.TBS.WALITM AS TF,
    CASE WHEN Part.IXLITM LIKE 'TE%' THEN Part.IXLITM END AS TE,
    CASE WHEN Part.IXLITM LIKE 'CF%' THEN Part.IXLITM END AS CAP,
    CASE WHEN Part.IXLITM LIKE 'LBL%' THEN Part.IXLITM END AS LABEL
    FROM WB.dbo.Part
    INNER JOIN Sched.dbo.TBS ON TBS.WAAITM=Part.IXKITA) AS T2
    WHERE WO=800059

哪个收益率:

WO          TF            TE          CAP            LABEL
800059      TF2-10426IA   NULL        NULL           NULL
800059      TF2-10426IA   NULL        CF-S1094P-UL   NULL
800059      TF2-10426IA   TE2-06718   NULL           NULL

我做了一些其他的尝试,将所有内容组合在一条线上,但没有任何运气,但我认为类似于'Group By'的东西是理想的......

这个查询的第二部分也让我感到有点难过,虽然我已经开始研究上述问题,但我需要从IXLITM列Where IXKITA=[The TE2***] for the TF2*** Number中提取更多数据。

我认为在这两个问题之间我可能会寻找T-SQL以外的解决方案,但我想首先要求所有专家提供一些见解。

提前致谢,非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

试试这个:

SELECT WO, 
       TF, 
       TE, 
       CAP, 
       LABEL 
FROM   (SELECT DISTINCT WADOCO   AS WO, 
                        WALITM   AS TF, 
                        Max(CASE 
                              WHEN IXLITM LIKE 'TE%' THEN PART.IXLITM 
                              ELSE '' 
                            END) AS TE, 
                        Max(CASE 
                              WHEN IXLITM LIKE 'CF%' THEN PART.IXLITM 
                              ELSE '' 
                            END) AS CAP, 
                        Max(CASE 
                              WHEN IXLITM LIKE 'LBL%' THEN PART.IXLITM 
                              ELSE '' 
                            END) AS LABEL 
        FROM   PART 
        GROUP  BY WALITM, 
                  WADOCO)T2 
WHERE  WO = 800059 

SQL Fiddle上有一个可行的解决方案。

答案 1 :(得分:1)

SELECT DISTINCT Sched.dbo.TBS.WADOCO AS WO,
Sched.dbo.TBS.WALITM AS TF,
TE.IXLITM AS TE,
CF.IXLITM AS CAP,
LBL.IXLITM AS LABEL
FROM Sched.dbo.TBS
LEFT JOIN WB.dbo.Part TE ON TBS.WAAITM = Part.IXKITA AND LEFT(Part.IXLITM,2) = 'TE'
LEFT JOIN WB.dbo.Part CF ON TBS.WAAITM = Part.IXKITA AND LEFT(Part.IXLITM,2) = 'CF'
LEFT JOIN WB.dbo.Part LBL ON TBS.WAAITM = Part.IXKITA AND LEFT(Part.IXLITM,3) = 'LBL'
WHERE WO=800059