【问题标题】:SQL, setting an indicator for duplicate recordSQL,设置重复记录的指标
【发布时间】:2016-05-28 23:26:32
【问题描述】:

我有一个类似下面结构的表

+----------+----------------+---------+
| Session  |  StudentNumber |    Marks|
+-------------------------------------+
| 1        |  1234          |  90     |
+-------------------------------------+
| 1        |  1345          |  90     |
+-------------------------------------+
| 1        |  1456          |   90    |
+-------------------------------------+

如果 Marks 列的所有值都相同(如上),我想将名为“PrintMarksInd”的指标设置为 N。

假设表格如下,则PrintMarksInd设置为Y

+--------+----------------+-----------+    
| Session|  StudentNumber |  Marks    |
+--------+----------------+-----------+
|   2    |    999         |  90       |
+--------+----------------+-----------+
|   2    |    980         |   90      |
+--------+----------------+-----------+
|   2    |    970         |  98       |
+--------+----------------+-----------+
|   2    |    960         |   98      |
+--------+----------------+-----------+    
|   2    |    950         |  60       |
+--------+----------------+-----------+

请帮我设置 PrintMarksIndicator

提前致谢

【问题讨论】:

  • 2个学生的分数不能完全一样是什么意思?
  • 没错,你为什么认为它们是重复的?
  • 我不确定您所说的“指标”是什么意思 - 您预期的结果是什么样的?
  • 您应该寻找的是给定会话中学生编号的重复 - 即在会话 1 中,您不能两次获得学生 10。

标签: sql duplicates indicator


【解决方案1】:

你可以尝试这样做:

DECLARE @indicator int
DECLARE @PrintMarksInd varchar(1)
select @indicator = count( distinct marks) from yourtable
if(@indicator = 1)
set @PrintMarksInd = 'Y'
else
set @PrintMarksInd = 'N'

即,如果计数为 1,则所有记录都具有相同的值,否则它也具有不同的值。

【讨论】:

  • 非常感谢拉胡尔。我使用了 count 并将该 count 包装在 decode 语句中。现在,如果 count 为 1,它将设置为 N,否则为 Y。
【解决方案2】:
SELECT 
    Session,
    student_number,
    marks,
    IF((SELECT 
        COUNT(marks)
        FROM
            Grades
        WHERE
            marks = table_one.marks) > 1,
    'Y',
    'N') AS PrintMarksInd
FROM
    Grades AS table_one

【讨论】:

    【解决方案3】:
    SELECT 
        Session,
        student_number,
        marks,
        IF((SELECT count(distinct marks)
            FROM
        Grades) = 1,
        'N',
        'Y') AS PrintMarksInd
    FROM
        Grades
    

    【讨论】:

    • 如果我理解正确,这就是你想要的!
    【解决方案4】:

    创建这个函数:

    create FUNCTION message_box ()
    RETURNS VARCHAR(50)
    AS
    BEGIN
    DECLARE @message_text  VARCHAR(50);
    If (select count(distinct marks)  from students_table) > 1
    set @message_text = 'not all students have the same mark'
    else 
    set @message_text='all students have the same mark'
    return @message_text    
    END;   
    

    然后在查询中使用 message_box() 函数:

    select top 1 dbo.message_box() as PrintMarksIndicator
    from students_table
    

    您只会收到“所有学生的分数相同”或“不是……”的消息
    如果这就是你要找的:)

    +----------+----------------+---------+
    | Session  |  StudentNumber |    Marks|
    +-------------------------------------+
    | 1        |  1234          |  90     |
    +-------------------------------------+
    | 1        |  1345          |  90     |
    +-------------------------------------+
    | 1        |  1456          |   90    |
    +-------------------------------------+  
    

    你会得到

    +----------+----------------------------+
    | PrintMarksIndicator                   |  
    +---------------------------------------+
    |wtf? all students have the same mark?       |  
    +---------------------------------------+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 2016-01-07
      • 2013-06-29
      • 2013-08-03
      • 1970-01-01
      • 2010-10-24
      相关资源
      最近更新 更多