SQL根据条件进行汇总

时间:2019-01-18 11:10:29

标签: sql oracle aggregate summarize

假设我有一个名为Table1的表,其中包含三列VALUECODETYPEVALUE列包含针对不同问题的1-4分。 CODE列包含与该问题有关的代码。 TYPE列指示该问题是纸质调查(PAP)还是在线调查(ONL)。

这是桌子的样子。

#Table1

VALUE    CODE    TYPE
-----   ------  ------ 
  2      Q2      ONL 
  1      Q2      PAP
  3      Q2      ONL
  4      Q2      ONL
  1      Q2      ONL
  1      Q2      ONL
  1      Q2      PAP
  2      Q2      ONL
  4      Q2      ONL
  2      Q2      ONL
  3      Q1      ONL
  1      Q1      ONL
  4      Q1      ONL
  3      Q1      ONL
      and so on...

我的目标

使用诸如此类的SQL查询(Oracle)总结该表-

1)我首先为CODE 计数所有值为 1的 2的 的值第二季度

2)并将该数字除以第二季度值的总数数量

3)按TYPE将结果分组,以便我将 ONL PAP 的结果分组。

预期结果

因此,在本例中,此查询的结果为-

TYPE      SCORE
PAP       0.625 (5/8)
ONL       1     (2/2)

我的尝试

如此简单,我似乎无法获得正确的价值。我觉得这需要嵌套查询来存储不同的值,但是我找不到正确的示例来指导我。这是我不正确的尝试-

SELECT TYPE, VALUE/COUNT(VALUE) as SCORE FROM Table1
where
CODE = 'Q2' AND
VALUE in (1,2)
GROUP BY TYPE

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:3)

您可以简单地做到:

select type, avg(case when value in (1, 2) then 1.0 else 0 end) 
from table1
where code = 'Q2'
group by type;

答案 1 :(得分:1)

这里是:

[TestClass]
public class FileWriteTests
{

    [TestMethod]
    public void TestMethodAfterClose()
    {
        var currentDir = Environment.CurrentDirectory;
        var fileToMonitor = "test.txt";
        List<string> output = new List<string>();
        var watcherTest = new FileWatcherTest(fileToMonitor, currentDir, output);

        File.Delete(Path.Combine(currentDir, fileToMonitor));
        using (var writer = new StreamWriter(Path.Combine(currentDir, fileToMonitor), true))
        {
            writer.WriteLine($"test");
            writer.Flush();
        }
        System.Threading.Thread.Sleep(10);
        Assert.AreEqual(1, output.Count);
        Assert.AreEqual("test", output[0]);

    }

    [TestMethod]
    public void TestMethodAfterFlush()
    {
        var currentDir = Environment.CurrentDirectory;
        var fileToMonitor = "test.txt";
        List<string> output = new List<string>();
        var watcherTest = new FileWatcherTest(fileToMonitor, currentDir, output);

        File.Delete(Path.Combine(currentDir, fileToMonitor));

        using (var writer = new StreamWriter(Path.Combine(currentDir, fileToMonitor), true))
        {
            try
            {
                writer.WriteLine($"test");
                writer.Flush();
                System.Threading.Thread.Sleep(1000);
                // add break point here for BareTail
                Assert.AreEqual(1, output.Count);
                Assert.AreEqual("test", output[0]);
            }
            catch
            {
                Assert.Fail("Test failed");
            }
        }
    }

    public class FileWatcherTest
    {
        public string FileName { get; set; }
        public string Directory { get; set; }
        private List<string> linesRead;
        private FileSystemWatcher watcher;
        public FileWatcherTest(string fileName, string directory, List<string> output)
        {
            FileName = fileName;
            Directory = directory;
            linesRead = output;
            watcher = new FileSystemWatcher();
            watcher.Path = directory;
            watcher.Filter = FileName;
            watcher.Changed += Watcher_Changed;
            watcher.EnableRaisingEvents = true;
            watcher.NotifyFilter =  NotifyFilters.Attributes |
                                    NotifyFilters.CreationTime |
                                    NotifyFilters.DirectoryName |
                                    NotifyFilters.FileName |
                                    NotifyFilters.LastAccess |
                                    NotifyFilters.LastWrite |
                                    NotifyFilters.Security |
                                    NotifyFilters.Size;
        }

        private void Watcher_Changed(object sender, FileSystemEventArgs e)
        {
            using (var fileStream = File.Open(Path.Combine(Directory, FileName), FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete | FileShare.Inheritable))
            {
                using (var reader = new StreamReader(fileStream))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        linesRead.Add(line);
                    }
                }
            }
        }
    }
}

答案 2 :(得分:1)

几种方法。
查询的问题在于您没有执行第二步-获取第二季度的响应总数。

这是我的SQLFiddle:http://www.sqlfiddle.com/#!9/898b3f/7

getInstructionType(applicationNumber, function(result) {
  getInstructionValidationResult(applicationNumber, function(type) {
    setValidationRowColor(result);
  });
});

function getInstructionValidationResult(applicationNumber) {
  var url = //-snip-;
  $.get(url, function(data) {
    return data;
  });
}

function getInstructionType(applicationNumber) {
  var url = //-snip-;
  $.get(url, function(data) {
    return data;
  });
}