创建子查询以从多个表中检索数据。

huangapple go评论67阅读模式
英文:

Creating a subquery to retrieve data from more than one table

问题

我有如下所示的代码; 我需要在我的存储过程中搜索我创建的新变量couryear

SELECT 
    ,s.kid, ep.exper_id, ep.exper_name, mp.md_name, 
    mo.md_option_id, kk.external_id,
    mv.completed_id,
    COUNT(ce.times_observed) AS countoftimes,
    CASE
        WHEN kid = '7180' THEN '044986'
        WHEN kid = '7800' THEN '044984' 
    END AS cour,
    CASE 
        WHEN CE.yid = '2024' THEN '2023-24'
        WHEN CE.yid = '2023' THEN '2022-23'  
    END AS [year]
FROM
    ks.reqe_md_pool mp, ks.reqe_md_options mo, 
    ks.reqe_md_values mv, ks.reqe_experiences_pool ep,
    ks.reqe_completed_experiences ce
LEFT JOIN
    ks.oasis_person kk ON kk.person_id = ce.student_id
LEFT JOIN  
    ks.oasis_schedule s ON ce.schedule_id = s.schedule_id, 
    ks.reqe_list_version lv 
WHERE 
    ep.exper_id = '1492'
    AND mv.md_id = mp.md_id 
    AND ce.exper_id = ep.exper_id 
    AND ce.completed_id = mv.completed_id 
    AND mv.md_option_id = mo.md_option_id 
    AND ce.list_version_id = lv.list_version_id 
GROUP BY 
    s.did, s.cid, s.lid, ep.exper_id, ep.exper_name,
    mp.md_id, mp.md_name, mo.md_option_id, mo.md_option_order, 
    mo.md_option_name, ce.student_id, mv.text_answer, mv.md_value_id,
    mv.completed_id, CE.YID, kk.external_id         

我如何在SQL中的存储过程中搜索yearcour

我尝试像这样做:

SELECT * 
FROM
    (*顶部的代码*) 
WHERE
    cour = @cour
    AND year = @year

然后我尝试使用CROSS APPLY,它可以用于year,但不能用于cour,因为cour位于左连接中:

SELECT 
    ,s.kid, ep.exper_id, ep.exper_name, mp.md_name, 
    mo.md_option_id, kk.external_id,
    mv.completed_id
 FROM
    ks.reqe_md_pool mp, ks.reqe_md_options mo, 
    ks.reqe_md_values mv, ks.reqe_experiences_pool ep,
    ks.reqe_completed_experiences ce
LEFT JOIN
    ks.oasis_person kk ON kk.person_id = ce.student_id
LEFT JOIN  
    ks.oasis_schedule s ON ce.schedule_id = s.schedule_id, 
    ks.reqe_list_version lv 
CROSS APPLY (
    SELECT 
        CASE 
            WHEN kid='7180' THEN '044986'
            WHEN kid='7800' THEN '044984'
        END AS UCFCOURSEID
) C(UCFCOURSEID)
WHERE 
    ep.exper_id = '1492'
    AND mv.md_id = mp.md_id 
    AND ce.exper_id = ep.exper_id 
    AND ce.completed_id = mv.completed_id 
    AND mv.md_option_id = mo.md_option_id 
    AND ce.list_version_id = lv.list_version_id 
GROUP BY 
    s.did, s.cid, s.lid, ep.exper_id, ep.exper_name,
    mp.md_id, mp.md_name, mo.md_option_id, mo.md_option_order, 
    mo.md_option_name, ce.student_id, mv.text_answer, mv.md_value_id,
    mv.completed_id, CE.YID, kk.external_id 

其余部分是值和分组的地方。

英文:

I have code as shown below; I need to search new variables I created as cour and year in my stored procedure

SELECT 
    ,s.kid, ep.exper_id, ep.exper_name, mp.md_name, 
    mo.md_option_id, kk.external_id,
    mv.completed_id,
    COUNT(ce.times_observed) AS countoftimes,
    CASE
        WHEN kid = '7180' THEN '044986'
        WHEN kid = '7800' THEN '044984' 
    END AS cour,
    CASE 
        WHEN CE.yid = '2024' THEN '2023-24'
        WHEN CE.yid = '2023' THEN '2022-23'  
    END AS [year]
FROM
    ks.reqe_md_pool mp, ks.reqe_md_options mo, 
    ks.reqe_md_values mv, ks.reqe_experiences_pool ep,
    ks.reqe_completed_experiences ce
LEFT JOIN
    ks.oasis_person kk ON kk.person_id = ce.student_id
LEFT JOIN  
    ks.oasis_schedule s ON ce.schedule_id = s.schedule_id, 
    ks.reqe_list_version lv 
WHERE 
    ep.exper_id = '1492'
    AND mv.md_id = mp.md_id 
    AND ce.exper_id = ep.exper_id 
    AND ce.completed_id = mv.completed_id 
    AND mv.md_option_id = mo.md_option_id 
    AND ce.list_version_id = lv.list_version_id 
GROUP BY 
    s.did, s.cid, s.lid, ep.exper_id, ep.exper_name,
    mp.md_id, mp.md_name, mo.md_option_id, mo.md_option_order, 
    mo.md_option_name, ce.student_id, mv.text_answer, mv.md_value_id,
    mv.completed_id, CE.YID, kk.external_id         

How can I search for year and cour in stored procedure created in SQL?

I tried to do something like this:

SELECT * 
FROM
    (*code at the top* ) 
WHERE
    cour = @cour
    AND year = @year

Then I tried cross apply, which worked with year, but didn't work with cour, because that able is in left join

SELECT 
    ,s.kid, ep.exper_id, ep.exper_name, mp.md_name, 
    mo.md_option_id, kk.external_id,
    mv.completed_id
 
FROM
    ks.reqe_md_pool mp, ks.reqe_md_options mo, 
    ks.reqe_md_values mv, ks.reqe_experiences_pool ep,
    ks.reqe_completed_experiences ce
LEFT JOIN
    ks.oasis_person kk ON kk.person_id = ce.student_id
LEFT JOIN  
    ks.oasis_schedule s ON ce.schedule_id = s.schedule_id, 
    ks.reqe_list_version lv 
   CROSS APPLY (SELECT 
       case WHEN kid='7180' then '044986'
            WHEN kid='7800' then '044984'
 
           END AS UCFCOURSEID)
               C(UCFCOURSEID)
WHERE 
    ep.exper_id = '1492'
    AND mv.md_id = mp.md_id 
    AND ce.exper_id = ep.exper_id 
    AND ce.completed_id = mv.completed_id 
    AND mv.md_option_id = mo.md_option_id 
    AND ce.list_version_id = lv.list_version_id 
GROUP BY 
    s.did, s.cid, s.lid, ep.exper_id, ep.exper_name,
    mp.md_id, mp.md_name, mo.md_option_id, mo.md_option_order, 
    mo.md_option_name, ce.student_id, mv.text_answer, mv.md_value_id,
    mv.completed_id, CE.YID, kk.external_id         

The rest is where values and group by

答案1

得分: 1

一种简化此操作的方法是利用 apply,它提供了可以包括在 select 列表中并在 where 子句中引用的派生列。

以下是一个示例:

select ..., cour, ...
from ...
cross apply (
  select case when kid='7180' then '044986' when kid='7800' then '044984' end
)c(cour)
where ...
  and cour = @cour
group by...

注意:代码部分未进行翻译。

英文:

One way you can make this easier is to utilise apply which provides the derived column you can then both include in your select list and reference in your where clause.

Here is an example:

select ..., cour, ...
from ...
cross apply (
  select case when kid='7180' then '044986' when kid='7800' then '044984' end
)c(cour)
where ...
  and cour = @cour
group by...

huangapple
  • 本文由 发表于 2023年6月30日 01:39:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定