使用max(列)和另一列> 0连接表(Join tables using max(column) with a with another column >0)
我有两张桌子,我正在尝试为我们的筹款部门建立一个查询,但我正在努力。 使用SQL Server 2008。
上诉表中包含有关我们将向哪些成员发送捐款申诉的数据。
Yearly_Gift表包含会员每年捐赠多少钱的数据。 有时,此表可以为特定年份提供零美元金额。
我正在尝试做的是使用这两个表来提出有人捐赠的最后一年以及他们捐赠了多少,只要它大于零,我们发出的呼吁。
以下是表格和一些数据的简化版本。 我也包括我想要的输出。 谁能帮我这个?
--Build Tables CREATE TABLE [dbo].[Appeals]( [Appeal_ID] [int] NOT NULL, [Member_ID] [int] NOT NULL, CONSTRAINT [PK_Appeals] PRIMARY KEY CLUSTERED ( [Appeal_ID] ASC, [Member_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[Yearly_Gift]( [Member_ID] [int] NOT NULL, [FiscalYear] [char](4) NOT NULL, [Amount] [money] NULL, CONSTRAINT [PK_Yearly_Gift] PRIMARY KEY CLUSTERED ( [Member_ID] ASC, [FiscalYear] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] --Fill tables INSERT INTO Appeals VALUES (1,101) INSERT INTO Appeals VALUES (1,102) INSERT INTO Appeals VALUES (2,101) INSERT INTO Appeals VALUES (2,102) INSERT INTO Appeals VALUES (2,103) INSERT INTO Appeals VALUES (2,104) INSERT INTO Appeals VALUES (2,105) INSERT INTO Yearly_Gift VALUES(101,'2015',100) INSERT INTO Yearly_Gift VALUES(102,'2014',0) INSERT INTO Yearly_Gift VALUES(102,'2012',150) INSERT INTO Yearly_Gift VALUES(102,'2011',200) INSERT INTO Yearly_Gift VALUES(103,'2013',500) INSERT INTO Yearly_Gift VALUES(103,'2014',500) INSERT INTO Yearly_Gift VALUES(104,'2012',200) INSERT INTO Yearly_Gift VALUES(104,'2015',100)
期望的输出
Appeal_ID Member_ID FiscalYear Amount 2 101 2015 100 2 102 2012 150 2 103 2014 500 2 104 2015 100 2 105 NULL NULL
感谢您提供的任何帮助。
I have two tables that I’m trying to build a query with for our fundraising department but that I’m struggling with. Using SQL Server 2008.
The Appeals table holds data about which members we are going to send an appeal for a donation to.
The Yearly_Gift table holds data about how much money a member donated on a yearly basis. Sometimes this table can have a zero dollar amount for a specific year.
What I’m trying to do is use these two tables to come up with the last year that someone donated and how much they donated, provided that it was greater than zero, for the appeal we are sending out.
Here are simplified versions of the tables and some data. I’m also including my desired output. Can anyone help me with this?
--Build Tables CREATE TABLE [dbo].[Appeals]( [Appeal_ID] [int] NOT NULL, [Member_ID] [int] NOT NULL, CONSTRAINT [PK_Appeals] PRIMARY KEY CLUSTERED ( [Appeal_ID] ASC, [Member_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[Yearly_Gift]( [Member_ID] [int] NOT NULL, [FiscalYear] [char](4) NOT NULL, [Amount] [money] NULL, CONSTRAINT [PK_Yearly_Gift] PRIMARY KEY CLUSTERED ( [Member_ID] ASC, [FiscalYear] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] --Fill tables INSERT INTO Appeals VALUES (1,101) INSERT INTO Appeals VALUES (1,102) INSERT INTO Appeals VALUES (2,101) INSERT INTO Appeals VALUES (2,102) INSERT INTO Appeals VALUES (2,103) INSERT INTO Appeals VALUES (2,104) INSERT INTO Appeals VALUES (2,105) INSERT INTO Yearly_Gift VALUES(101,'2015',100) INSERT INTO Yearly_Gift VALUES(102,'2014',0) INSERT INTO Yearly_Gift VALUES(102,'2012',150) INSERT INTO Yearly_Gift VALUES(102,'2011',200) INSERT INTO Yearly_Gift VALUES(103,'2013',500) INSERT INTO Yearly_Gift VALUES(103,'2014',500) INSERT INTO Yearly_Gift VALUES(104,'2012',200) INSERT INTO Yearly_Gift VALUES(104,'2015',100)
Desired Output
Appeal_ID Member_ID FiscalYear Amount 2 101 2015 100 2 102 2012 150 2 103 2014 500 2 104 2015 100 2 105 NULL NULL
Thank you for any help that you can provide.
原文:https://stackoverflow.com/questions/33830470
最满意答案
当你想要初始化这样的数组时,你必须使用const值(在编译时知道这些值)。 例如:
const int r = 1, c = 2; int m[r][c];
但是,在您的情况下,您不知道编译期间的大小。 所以你必须创建一个动态数组。 这是一个示例代码段。
#include <iostream> int main() { int n_rows, n_cols; int **first; std::cout << "Please enter no.of rows and columns of the 1st Matrix, respectively :"; std::cin >> n_rows >> n_cols; // allocate memory first = new int*[n_rows](); for (int i = 0; i < n_rows; ++i) first[i] = new int[n_cols](); // don't forget to free your memory! for (int i = 0; i < n_rows; ++i) delete[] first[i]; delete[] first; return 0; }
You have to use const values (the values are known during compilation time) when you want to initialize an array like this. For example:
const int r = 1, c = 2; int m[r][c];
However, in your case you don't know the size during compilation. So you have to create a dynamic array. Here is an example snippet.
#include <iostream> int main() { int n_rows, n_cols; int **first; std::cout << "Please enter no.of rows and columns of the 1st Matrix, respectively :"; std::cin >> n_rows >> n_cols; // allocate memory first = new int*[n_rows](); for (int i = 0; i < n_rows; ++i) first[i] = new int[n_cols](); // don't forget to free your memory! for (int i = 0; i < n_rows; ++i) delete[] first[i]; delete[] first; return 0; }
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
你没有2D数组。 你有一个指向1D数组的指针数组; 这些阵列中的每一个都可以(并且可能有99.9%)位于内存中的不同地址。 因此,您不能像memset那样将它们视为一个连续的块。 最好的(也是最容易缓存的)选项是动态分配整个数组: int *arr = malloc(2 * 5 * sizeof(int)); 这将需要你做手动建立索引: for (r = 0; r < 5; r++) { for (c = 0; c < 2; c++) { arr[r * 2 + c] = 1; ...
-
首先,你得到了变量错误。 这个论点是arr ,而不是a 。 a[i] = rand()毫无意义。 a[i]是一整行,你不能给它分配一个数字。 要访问二维数组的元素,请使用两个下标。 void init(int arr[10][100000]) { for (int i = 0; i <10; i++){ for(int k = 0; k < 100000; k++){ arr[i][k] = rand(); } } } Fi ...
-
这里: int *array = new int[g]; int s = 0; while(s < (g*g)) { cin >> array[s]; s++; } 你正在写过数组的末尾。 如果g是3,则array只有3个元素,索引从0到2,但是你一直写到array[8] 。 Here: int *array = new int[g]; int s = 0; while(s < (g*g)) { cin >> array[s]; s++; } You're writing ...
-
首先,赋值和初始化之间存在差异。 OP标题是关于初始化的。 其次,您没有告诉我们您的2D数组是类成员(静态/非静态)还是命名空间变量。 - 既然你提到在类构造函数中初始化它,我假设它是一个类非静态成员,因为: $ 12.6.2 / 2 - “除非mem-initializer-id命名构造函数的类,构造函数类的非静态数据成员,或者该类的直接或虚拟基础,否则mem-initializer是不正确的。” 此外,从C ++ 03开始,成员数组不能在OP的情况下在构造函数初始化列表中初始化(不确定C ++ 0x)。 ...
-
您的总和代码非常好。 但问题在于初始化数组的方式。 在这里查看您的代码: int k = array[0][j]; int l = array[1][j]; array[0][j] = (i+1)+(j); array[1][j] = k; array[2][j] = l; 看到当i=0且j = 0 ,您将array[0][0]值修改为1而对于i =1 , j =0 ,您再次将array[0][0]的值修改为2等等。 所以初始化你的数组如: for (int i = 0; i < array. ...
-
在C中初始化2D char数组(Initializing 2D char array in C)[2023-08-24]
我认为图片有帮助。 这里是char newarray[5][10] 。 它是一个由10个字符组成的数组和一个由5个数组组成的数组。 你可以用一个memset调用清除它。 这是char **array 。 它说array是一个指针。 它指向什么? 一个指向角色的指针。 请记住指针算术。 如果array是碰巧指向一个指针的指针,那么(*array)等于array[0] ,这就是array指向的指针。 什么是array[1] ? 它是数组指向的数组中的第二个指针 。 什么是array[0][0] ? 它是arra ... -
当你想要初始化这样的数组时,你必须使用const值(在编译时知道这些值)。 例如: const int r = 1, c = 2; int m[r][c]; 但是,在您的情况下,您不知道编译期间的大小。 所以你必须创建一个动态数组。 这是一个示例代码段。 #include
int main() { int n_rows, n_cols; int **first; std::cout << "Please enter no.of rows and column ... -
您可能希望使用锯齿状的阵列而不是2D阵列。 简而言之,2D数组始终是N x M矩阵,您无法调整大小,而锯齿状数组是数组数组,您可以使用不同的大小单独初始化每个内部元素(请参阅此处的详细信息) int maxBound = 100; Random rnd = new Random(); int numLoops = rnd.Next(1000, 1200); string[][] jagged = new string[numLoops][]; for (int i = 0; i < numLoops; ...