计算'X'然后将值排列为SQL中的行

时间:2016-11-28 02:29:58

标签: sql-server

我在SQL中有一个表,其中有许多列,每行中每列的值都是''或'X'。我需要为每个列计算这个'X',这可以通过以下代码完成;

links = response.xpath('//div[@class="title-and-desc"]/a')
for l in links:
    # url:
    print(l.xpath('@href').extract_first())
    # text with explicit xpath:
    print(l.xpath('div/text()').extract_first())
    # or with all text elements with relative //text:
    print(''.join(l.xpath('.//text()').extract()).strip())

查询的结果是;

SELECT COUNT(GVI0) AS GVI0, COUNT(GVI1) AS GVI1, COUNT(GVI2) AS GVI2
FROM dbo.HullInspectionProgram
WHERE (StructureEntry='1' AND Year='2016')

然而,(在我的理解中)为了将此计数值绑定到具有多个系列名称“GVI”和“NDT”的ASP.net图表控件中,我需要将该列设置为DataTable的行。 / p>

我尝试在SQL中使用UNPIVOT;

GVI0 NDT0 GVI1 NDT1 GVI2 NDT2
11   11   2    4    11   11

但似乎代码错了。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我认为以下内容对您有用。至少,作为一个开始。

SELECT *
FROM (
      SELECT COUNT(GVI0) AS GVI0, COUNT(GVI1) AS GVI1, COUNT(GVI2) AS GVI2
      FROM dbo.HullInspectionProgram
      WHERE (StructureEntry='1' AND Year='2016')
     ) P
UNPIVOT (GVI0Count FOR ListOfColumns IN (GVI0, GVI1, GVI2)) AS unpivott