0

我正在尝试打开另一个表并在其中获取一些值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

有解决办法吗?

4

1 回答 1

2

看起来您缺少 aGROUP 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'
于 2012-11-15T10:38:57.677 回答