如何使用' Group By'加入两张桌子时

时间:2014-09-08 16:21:44

标签: sql sql-server

我有两个表StudentMaster和StudentQualificationDetalis,并有一些如下所示的数据。 enter image description here

MarksPercentage列是varchar

I need result like-
details of students who have AdmissionBase = 10+2 and
Get the number of students in AdmissionBase having percentage 50% - 60% and
61% - 70% and
71% - 80% and
81% - 90% and
91% - 100%

我想要的结果示例是 -

**AdmissionBase          Percentage          NumberOfStudents**
10+2                     50% - 60%           3
10+2                     61% - 70%           2

以上只是一个例子。表中的记录可能会有所不同。

这是我试过的

SELECT          sm.AdmissionBase,
                (
                CASE
                WHEN sq.MarksPerecent IS NULL OR sq.MarksPerecent = '' THEN 'Not Known'
                WHEN CONVERT(DECIMAL, sq.MarksPerecent) >= 50 AND CONVERT(DECIMAL, sq.MarksPerecent) <= 60 THEN '50% - 60%'
                WHEN CONVERT(DECIMAL, sq.MarksPerecent) >= 61 AND CONVERT(DECIMAL, sq.MarksPerecent) <= 70 THEN '61% - 70%'
                WHEN CONVERT(DECIMAL, sq.MarksPerecent) >= 71 AND CONVERT(DECIMAL, sq.MarksPerecent) <= 80 THEN '71% - 80%'
                WHEN CONVERT(DECIMAL, sq.MarksPerecent) >= 81 AND CONVERT(DECIMAL, sq.MarksPerecent) <= 90 THEN '81% - 90%'
                WHEN CONVERT(DECIMAL, sq.MarksPerecent) >= 91 AND CONVERT(DECIMAL, sq.MarksPerecent) <= 100 THEN '91% - 100%'
                ELSE 'Below 50%'
                END
                )Percentage,
                COUNT(*)
FROM            StudentMaster sm, StudentQualificationDetails sq
where           sm.registerationnumber = sq.registerationnumber
and             sm.AdmissionBase = sq.ExamDescription
and             sm.admissionbase = '10+2'
group by percentage

但它给我错误 - 列名称'百分比'无效。

由于

3 个答案:

答案 0 :(得分:1)

你需要一张桌子来容纳你的乐队。像这样的东西

DECLARE @PercentageBand TABLE
(
    MinPercent    float,
    MaxPercent    float,
    BandName      varchar(10)
)

INSERT INTO @PercentageBand
    VALUES  (0, 10,'0 - 9.9%'),
            (10, 20, '10% - 19.9%')
            (etc.)

然后与另外两个人一起加入这张桌子:

SELECT      M.AdmissionBase,
            B.BandName,
            COUNT(m.RegistrationNumber) AS NumberOfStudents
FROM        StudentMaster               M
INNER JOIN  StudentQualificationDetails D ON M.RegistrationNumber = D.RegistrationNumber
INNER JOIN  @PercentageBand             B ON D.MarksPercent >= B.MinPercent
                                         AND D.MarksPercent <  B.MaxPercent
GROUP BY    M.AdmissionBase,
            B.BandName

答案 1 :(得分:1)

这是Zoff提供的更简化版本。它使用一个按表自行创建的组来处理连接。我有最后一个条目为101%,因为连接基于&gt; =和&lt;上下文。

SELECT
      sm.AdmissionBase,
      PctRanks.Grp,
      COUNT(*) as TotalPerGroup
   from 
       StudentMaster sm
          JOIN StudentQualificationDetails sqd
             ON sm.RegistrationNumber = sqd.RegistrationNumber
             JOIN ( select 'Less than 50%' as Grp, 0 as AtLeast, 50 as LessThan
                    UNION select '50% - 60%   ', 50, 61
                    UNION select '61% - 70%   ', 61, 70
                    UNION select '71% - 80%   ', 71, 80
                    UNION select '81% - 90%   ', 81, 90
                    UNION select '91% - 100%   ', 91, 101 ) PctRanks
                ON sqd.MarksPercent >= PctRanks.AtLeast
                AND sqd.MarksPercent < PctRanks.LessThan
   where 
      sm.AdmissionBase = '10+2'
   group by
      sm.AdmissionBase,
      PctRanks.Grp

按要求说明。 首先,您可以创建一个Zoff提供的表,但我更喜欢在线使用较小的目的表。因此,包装为(查询)PctRanks的查询会内联。通过&#34; AS&#34;简单地选择值。列名称将表示1行。通过执行彼此的并集,可以为同一结果集创建其他行。这会产生P&R的结果集,包括#34; Grp&#34;,Atleast和LessThan列以及6行。

接下来,从您的硕士生名单开始。您只关心那些具有特定许可基础的人,WHERE子句本身应该很简单。

现在是混合......学生大师别名&#34; sm&#34; (只有AdmissionBase =&#39; 10 + 2&#39;)加入StudentQualificationDetails表别名&#34; sqd&#34;简化可读性。加入是根据学生的注册号码。最后,从sqd条目中,将其加入临时结果集&#34; PctRanks&#34; MarksPercent在您收集的两个数字范围内。

最后一栏是获得录取基数,%范围的文字描述和通用COUNT(*)。由于执行聚合函数(例如count(),min(),max(),avg(),sum()),引擎需要知道如何应用公共元素(因此你的%组标签)。

答案 2 :(得分:0)

您是否考虑过动态计算范围(除以10并减少分数)? 您也可以在GROUP-BY-Statement中使用它:

SELECT 
  sm.AdmissionBase,
  Floor(sqd.MarksPercent/10) AS Percentage,
  COUNT(sm.RegistrationNumber) AS NumberOfStudents
FROM StudentMaster sm
INNER JOIN StudentQualificationDetails sqm ON (sm.RegistrationNumber = sqm.RegistrationNumber)
WHERE
  sm.AdmissionBase = "10+2"
GROUP BY sm.AdmissionBase, FLOOR(sqd.MarksPercent/10)