在pivot函数中使用sum(列)的SQL查询

时间:2015-04-23 03:06:48

标签: c# sql .net pivot

从此SQL query get count item for report per day of the month?

我可以使用代码获取报告:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

const int grid_rows=50;
const int grid_cols=2;

double slope(double thrust[grid_rows][grid_cols],double time);

// Constant Declarations
const double PI = 3.1415926535; // the radius/diameter of a circle

// Main Program
int main( )
{
 system("CLS");
 cout << "Take Home #12 by - "
    << "CETUA\n\n";

double thrust[grid_rows][grid_cols];
double time;
double newton;
char ans;
int i=0;
int j=0;

cout << "Enter thrust curve data (0 for thrust end list): "<<endl;
for(i=0;i < grid_rows; i++)
{
    for(j=0; j< grid_cols;j++)
    {
        cin >> thrust[i][j];
        if(thrust[i][j]==0)
        {
            break;
        }
    }
    if(thrust[i][j]==0)
    {
        break;
    }
  }


  do
  {
    cout << "Enter a time: "<<endl;
    cin >> time;

    newton=slope(thrust,time);

    cout << "The thrust at time "<<time << " is " << newton << " newtons."                 <<endl:
    cout << "Would you like another thrust value? (Y or N): " <<endl;
    cin >> ans;
  }while(ans=='Y'||ans=='y');
}

double slope(double thrust[50][2],double time)
{
  double newton;

  while(time > thrust[50][2]);
  {
    for(int i=0;i < grid_rows; i++)
    {
      for( int j=0; j< grid_cols;j++)
      {
        newton=((time - thrust[i][j])/(thrust[i][j]-thrust[i][j]))
            *(thrust[i][j]-thrust[i][j])+thrust[i][j];
        return newton;
      }
    }
   }
 }

但是现在,我的表格中有一个新列( SELECT name, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31], ([1] + [2] + [3] + [4] + [5] + [6] + [7] + [8] + [9] + [10] + [11] + [12] + [13] + [14] + [15] + [16] + [17] + [18] + [19] + [20] + [21] + [22] + [23] + [24] + [25] + [26] + [27] + [28] + [29] + [30] + [31]) as total FROM ( SELECT Name, id, Datepart(day, [date]) day FROM item WHERE MONTH([date]) = 2 AND YEAR([date]) = 2015 ) x PIVOT ( count(id) FOR day IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31]) ) p )(参见http://imagizer.imageshack.us/a/img911/110/hCx7ho.png

我需要amount来替换sum(amount)以获取总金额。

我尝试添加选择列count(id)amount来替换sum(amount),但它无效(我得到了它:http://imageshack.com/a/img911/6023/zhMe7I.png

请帮帮我或指导我。

1 个答案:

答案 0 :(得分:0)

你需要两个,count(id)和Sum(金额)?如果是,则需要两个透视查询。否则你应该可以用这样的数量替换id:

 .....
FROM   
(
    SELECT Name, 
        amount, 
        Datepart(day, [date]) day 
    FROM   item 
    WHERE  MONTH([date]) = 2 AND YEAR([date]) = 2015
) x 
PIVOT 
(
    sum(amount) 
    FOR day IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19],  [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31]) 
) p