如何在SQL中有效地计算某些列值的出现次数?

时间:2017-02-15 02:19:45

标签: mysql sql data-modeling

我正在使用MySQL。

我有service_status的表格:

   id | service_id | service_status
   ----------------------------------------------------------------------
   0  | 1001       | download_started
   1  | 1001       | download_started
   2  | 1002       | download_started
   3  | 1002       | download_started
   4  | 1002       | download_failed
   5  | 1003       | download_started
   6  | 1003       | download_failed
   7  | 1003       | something_else
   8  | 1003       | another_thing

我想查询所有service_id个,以及另外两列计算download_started的数量和download_failed的数量:

id | service id | download_started | download_failed
----------------------------------------------------------------------
0  | 1001       | 2                | 0
1  | 1002       | 2                | 1
2  | 1003       | 1                | 1

我只关心download_starteddownload_failed的状态。

非常感谢。

2 个答案:

答案 0 :(得分:1)

你可以用这个:

SELECT 
    service_id,
    SUM(CASE WHEN service_status = 'download_started' THEN 1 ELSE 0 END) download_started,
    SUM(CASE WHEN service_status = 'download_failed' THEN 1 ELSE 0 END) download_failed
FROM 
    table_name
GROUP BY 
    service_id
ORDER BY 
    service_id;

答案 1 :(得分:0)

在MySQL中,您可以将表达式简化为:

/* 
 * This is the offending computed variable.
 */

static var nextImagePath: URL {
  return nextFilePathForDirectoryAtURL(imageDirectory, withExtension: "jpg");
}

/*
 * The method called by above variable. It looks through all the
 * files in a directory, finds the one with the highest index,
 * and returns a new path by incrementing the highest index by 1.
 */

fileprivate static func nextFilePathForDirectoryAtURL(_ url: URL, withExtension ext: String) -> URL {
  guard let files = try? FileManager.default.contentsOfDirectory(
    at: url,
    includingPropertiesForKeys: nil,
    options: .skipsHiddenFiles) else {
      fatalError("Could not create next file path for directory at url: \(url)");
  }

  var maxFileNumber = 0;
  for file in files {
    let fileName = file.deletingPathExtension().lastPathComponent;

    guard
      let fileNumber = Int(fileName),
      file.pathExtension.lowercased() == ext.lowercased()
      else { continue }

    maxFileNumber = max(maxFileNumber, fileNumber);
  }

  return url.appendingPathComponent("\(maxFileNumber + 1).\(ext)");
}

/*
 * Some supporting computed variables for constructing directories.
 */

fileprivate static var libraryDirectory: URL {
  guard let url = try? FileManager.default.url(
    for: .libraryDirectory,
    in: .userDomainMask,
    appropriateFor: nil,
    create: true) else {
      fatalError("Could not create library directory url.");
  }

  return url;
}

fileprivate static var documentSetDirectory: URL {
  let directory = libraryDirectory.appendingPathComponent("MemberDocumentSets");

  try? FileManager.default.createDirectory(
    at: directory,
    withIntermediateDirectories: true,
    attributes: nil);

  return directory;
}

fileprivate static var imageDirectory: URL {
  let directory = documentSetDirectory.appendingPathComponent("Images");

  try? FileManager.default.createDirectory(
    at: directory,
    withIntermediateDirectories: true,
    attributes: nil);

  return directory;
}