内连接不按预期工作

时间:2012-11-15 10:34:43

标签: sql sql-server-2005

我正在尝试打开另一个表并在inner join

中获取一些值

问题是我有很多错误而无法运行。我想我的语法错误了。我的代码是:

INNER JOIN (SELECT (min(TA_Timestamp)as timestampmin,ta_wp_id as ta_wp_id) as test 
ON test.ta_wp_id = p17.PA_WP_ID
FROM ta_tasks WHERE test.ta_wp_id = p17.pa_wp_id)

我正在尝试获取结果的表名为ta_tasks,它包含一个名为ta_wp_id的字段。另一个表名为parameters,它包含一行名为pa_wp_id

的行

我需要的是在min(TA_Timestamp) = ta_tasks

时从ta_wp_id获取pa_wp_id

有解决方案吗?

1 个答案:

答案 0 :(得分:2)

看起来你错过了GROUP BY,你的语法有点偏差。试试这个:

SELECT
    p1.pa_value as SDay,
    p2.pa_value as SMonth,
    p3.pa_value as SYear,
    p4.pa_value as EDay,
    p5.pa_value as EMonth,
    p6.pa_value as EYear,
    SUBSTRING(p7.PA_NAME,11, len(p7.PA_NAME)) as _index_,
    p8.pa_value as _status_,
    p9.pa_value as approved_CHECK_status,
    p10.pa_value as expense_type_CHECK,
    p11.pa_value as congress_name,
    p12.pa_value as _Initiator_,
    p13.pa_value as planned_budget,
    p14.pa_value as actual_budget,
    p15.pa_value as congressNumHCP,
    p16.pa_value as congressNumMSD,
    p17.pa_value as RequestNumber,
    test.timestampmin    

FROM WP_WorkflowProcess p
LEFT JOIN PA_Parameter p1 on p1.PA_WP_ID=p.wp_id AND p1.PA_NAME = 'Event_Day_From'
LEFT JOIN PA_Parameter p2 on p2.PA_WP_ID=p.wp_id AND p2.PA_NAME = 'Event_Month_From'
LEFT JOIN PA_Parameter p3 on p3.PA_WP_ID=p.wp_id AND p3.PA_NAME = 'Event_Year_From'
LEFT JOIN PA_Parameter p4 on p4.PA_WP_ID=p.wp_id AND p4.PA_NAME = 'Event_Day_To'
LEFT JOIN PA_Parameter p5 on p5.PA_WP_ID=p.wp_id AND p5.PA_NAME = 'Event_Month_To'
LEFT JOIN PA_Parameter p6 on p6.PA_WP_ID=p.wp_id AND p6.PA_NAME = 'Event_Year_To'
LEFT JOIN PA_Parameter p7 on p7.PA_WP_ID=p.wp_ID
    AND ((p7.PA_NAME like 'reqExpense_' AND CAST( p7.PA_wp_id as varchar(15) ) like CAST( p.wp_id as varchar(5) ) COLLATE DATABASE_DEFAULT ) 
    OR (p7.PA_NAME like 'reqExpense__' AND CAST( p7.PA_wp_id as varchar(15) ) like CAST( p.wp_id as varchar(5) ) COLLATE DATABASE_DEFAULT) )
LEFT JOIN PA_Parameter p8 on p8.PA_WP_ID=p.wp_id AND p8.PA_NAME = 'EventStatus'
LEFT JOIN PA_Parameter p9 on p9.PA_WP_ID=p.wp_id AND p9.PA_NAME = 'MAapproval'
LEFT JOIN PA_Parameter p10 on p10.PA_WP_ID=p.wp_id AND p10.PA_NAME = 'reqExpense'+ SUBSTRING(p7.PA_NAME,11, len(p7.PA_NAME))
LEFT JOIN PA_Parameter p11 on p11.PA_WP_ID=p.wp_id AND p11.PA_NAME = 'EventNameReal'
LEFT JOIN PA_Parameter p12 on p12.PA_WP_ID=p.wp_id AND p12.PA_NAME = 'Requisitioner'
LEFT JOIN PA_Parameter p13 on p13.PA_WP_ID=p.wp_id AND p13.PA_NAME = 'Preliminary_BudgetText'
LEFT JOIN PA_Parameter p14 on p14.PA_WP_ID=p.wp_id AND p14.PA_NAME = 'Final_BudgetText'
LEFT JOIN PA_Parameter p15 on p15.PA_WP_ID=p.wp_id AND p15.PA_NAME = 'congressNumberHCP'+SUBSTRING(p7.PA_NAME,11, len(p7.PA_NAME))
LEFT JOIN PA_Parameter p16 on p16.PA_WP_ID=p.wp_id AND p16.PA_NAME = 'congressNumberMSD'+SUBSTRING(p7.PA_NAME,11, len(p7.PA_NAME))
LEFT JOIN PA_Parameter p17 on p17.PA_WP_ID=p.wp_id AND p17.PA_NAME = 'RequestNumber'
INNER JOIN
(
    SELECT min(TA_Timestamp) as timestampmin, ta_wp_id as ta_wp_id
    FROM ta_tasks
    GROUP BY ta_wp_id
) as test 
    ON test.ta_wp_id = p17.PA_WP_ID

WHERE p.wp_type='EventManagementNew'