首页 \ 问答 \ 使用max(列)和另一列> 0连接表(Join tables using max(column) with a with another column >0)

使用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
更新时间:2021-08-17 13:08

最满意答案

当你想要初始化这样的数组时,你必须使用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;
}

相关问答

更多
  • 你没有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. ...
  • 我认为图片有帮助。 这里是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; ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)