计算不同类型

时间:2018-05-01 16:57:10

标签: sql sql-server tsql sql-server-2008-r2

我有一个名为Food的表,其中包含BoxNumber,Analyte和AnalyteVal列。

我需要这些盒子中特定分析物(ana1,ana3,ana4)的总和。 我怎么能这样做?

原始数据:

(Integer)  (Varchar)  (Varchar)
BoxNumber | Analyte | AnalyteVal
1           ana1      5
1           ana2      6
1           ana3      2f
1           ana4      6
2           ana3      1
2           ana4      4
3           ana1      2
3           ana2      4f
3           ana3      NULL

结果应该是:

BoxNumber | AnalyteVal
1           11
2           5
3           2

我的尝试:

SELECT BoxNumer, 
       SUM(CAST(ISNUMERIC(AnalyteVal) AS DECIMAL(10,2))) AS [DesiredSum] 
  FROM Food 
 WHERE analyte IN ('ana1','ana3','ana4') 
 GROUP BY BoxNumber

4 个答案:

答案 0 :(得分:0)

试试这个:

describe("Update room list based on changing context values", () => {
  let page: HelperClass;
  let highSchoolRoomNameList: string[] = [];
  let middleSchoolRoomNameList: string[] = [];

  beforeAll(async () => {
    page = new HelperClass();
    await page.setBrowserSize();
    await page.commonContextTestSteps();
  });

  it("Changing school dropdown value", async () => {
    await page.waitForElement(page.schoolDropDown, 5000);
    await page.schoolDropDown.click();

    await page.waitForElement(page.dropDownList, 5000);
    await page.dropDownList.get(0).click();

    await browser
      .switchTo()
      .frame(element(by.tagName("iframe")).getWebElement());

    await page.roomList.each( item => {
      item.getAttribute("innerText").then(text => {
        highSchoolRoomNameList.push(text);
      });
    });

    await page.waitForElement(page.schoolDropDown, 5000);
    await page.schoolDropDown.click();

    await page.waitForElement(page.dropDownList, 5000);
    await page.dropDownList.get(1).click();

    await browser.switchTo().defaultContent();

    await browser
      .switchTo()
      .frame(element(by.tagName("iframe")).getWebElement());

    await page.roomList.each(item => {
      item.getAttribute("innerText").then(text => {
        middleSchoolRoomNameList.push(text);
      });
    });

    await protractor.promise.controlFlow().execute(() => {
      expect(highSchoolRoomNameList).not.toEqual(middleSchoolRoomNameList);
    });
  });
});

使用SQLFiddle示例here

答案 1 :(得分:0)

无需使用distinct

select BoxNumber, sum(cast(AnalyteVal AS DECIMAL(10, 2))) as [DesiredSum] 
from Food 
where analyte in ('ana1','ana3','ana4') and
      isnumeric(AnalyteVal) = 1 
group by BoxNumber;

如果您有更高版本的SQL Server,则可以使用try_convert()代替

select BoxNumer, sum(cast(AnalyteVal as DECIMAL(10, 2))) as [DesiredSum] 
from Food 
where analyte in ('ana1','ana3','ana4') and
      try_convert(int, AnalyteVal) is not null
group by BoxNumber;

答案 2 :(得分:0)

尝试此查询。

SELECT boxnumber, 
       Sum(Cast(analyteval AS DECIMAL(10, 2))) AS [DesiredSum] 
FROM   food 
WHERE  analyte IN ( 'ana1', 'ana3', 'ana4' ) 
       AND analyteval NOT LIKE '%[^0-9]%' 
GROUP  BY boxnumber; 

或者这......

SELECT boxnumber, 
       Sum(Cast(analyteval AS DECIMAL(10, 2))) AS [DesiredSum] 
FROM   food 
WHERE  analyte IN (SELECT DISTINCT analyte 
                   FROM   food
                   WHERE  analyte <> 'ana2') 
       AND analyteval NOT LIKE '%[^0-9]%' 
GROUP  BY boxnumber; 

结果

+-----------+------------+
| boxnumber | DesiredSum |
+-----------+------------+
|         1 |         11 |
|         2 |          5 |
|         3 |          2 |
+-----------+------------+

答案 3 :(得分:0)

我相信这应该有效:

SELECT BoxNumber, 
SUM(cast(AnalyteVal as int)) AS DesiredSum
FROM Food 
WHERE analyte in ('ana1', 'ana3','ana4')
AND IsNumeric(AnalyteVal) = 1
GROUP BY BoxNumber