总结SQL中的表

时间:2016-12-03 23:10:23

标签: sql sql-server

我有下表:

Plant   Type     A      B      D
P1      ABC      1      2      0
P1      DEF      0      0      0
P1      GHI      0      0      1
P1      THE      0      0      0
P1      FGE      0      1      0
P1      REF      0      0      0
P1      XYZ      0      0      0
P2      ABC      0      1      0
P2      DEF      0      1      0
P2      GHI      0      0      2
P2      THE      0      0      0
P2      FGE      0      0      0
P2      REF      0      0      0
P2      XYZ      0      0      0
P3      ABC      1      0      0
P3      DEF      2      0      0
P3      GHI      0      0      1
P3      THE      0      0      0
P3      FGE      0      0      0
P3      REF      0      0      0
P3      XYZ      0      0      0
P4      ABC      0      0      0
P4      DEF      0      0      0
P4      GHI      0      0      0
P4      THE      0      0      1
P4      FGE      0      0      0
P4      REF      1      0      0
P4      XYZ      0      0      0

我有7种不同类型的药物= [ABC DEF GHI THE FGE REF XYZ ]  每个可以分为4个不同的类别。这些药物是由4种不同的植物制成的。所有植物都有能力制造所有药物。我想知道看一下表中每种类型和每个类别中每种植物制造的药物有多少。我的结果表应该是这样的。

var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var app = express();

var api = new ParseServer({
  databaseURI: 'mongodb://localhost:27017/dev', // Connection string for your MongoDB database
  cloud: '/home/myApp/cloud/main.js', // Absolute path to your Cloud Code
  appId: 'myAppId',
  masterKey: 'myMasterKey', // Keep this key secret!
  fileKey: 'optionalFileKey',
  serverURL: 'http://localhost:1337/parse' // Don't forget to change to https if needed
});

// Serve the Parse API on the /parse URL prefix
app.use('/parse', api);

// Receive incoming Twilio SMS messages
app.post('/messages', function(req, res) {
  console.log(req.body.Body);
  // do something
  // send an empty response to Twilio
  res.send("<Response />");
});

app.listen(1337, function() {
  console.log('parse-server-example running on port 1337.');
});

我不确定哪些命令甚至可以用来开始。任何正确方向的帮助将不胜感激。

2 个答案:

答案 0 :(得分:5)

如果类别的数量是固定的,您可以简单地使用条件聚合。

select tplant.plantid,ttype.type,
count(case when t.category='A' then t.plant end) A,
count(case when t.category='B' then t.plant end) B,
count(case when t.category='D' then t.plant end) D
from (select distinct type from typetable) ttype --replace typetable with the table that has all the types
cross join (select distinct plantid from tablename) tplant
left join tablename t on t.plantid=tplant.plantid and t.type=ttype.type
group by tplant.plantid,ttype.type

答案 1 :(得分:2)

@ vkp的回答是正确的。以下可能会有更好的性能(假设tplant(type, plantid, category)上的索引:

select p.plantid, t.type, tp.A, tp.B, tp.D
from (select distinct type from typetable) t cross join
     (select distinct plantid from tablename) p outer apply
     (select sum(case when t.category = 'A' then 1 else 0 end) as A,
             sum(case when t.category = 'B' then 1 else 0 end) as B,
             sum(case when t.category = 'D' then 1 else 0 end) as D
      from tplant tp
      where tp.type = t.type and tp.plantid = p.plantid
     ) tp;