SQL显示每个客户只有同一个表中的最后一个发票号

时间:2015-09-02 20:58:56

标签: sql-server tsql max

我有一个表格,其中包含客户名称和另一列中的发票的顺序编号,我希望每位客户只选择发票凭证编号最多的行。

例如我有这种情况:

Customer name | DocNum
----------------------    
cust1           1 
cust1           2
cust1           3
cust2           5
cust3           7
cust3           9
cust4           11
cust4           12

我会得到:

Customer name | DocNum
-----------------------
cust1                3
cust2                5
cust3                9
cust4               12

我尝试了一些查询但没有正常工作。

SELECT 
    CustName, DocNum, MAX(DocNum) AS "Last invoice number"
FROM   
    TDocHeader
GROUP BY 
    CustName
HAVING 
    MAX(DocNum) > 1;

SELECT 
    CustName, DocNum AS 'Last invoice number'
FROM   
    TDocHeader
WHERE  
    DocNum IN (SELECT Max(DocNum)
               FROM TDocHeader
               GROUP BY CustName) 

谢谢

2 个答案:

答案 0 :(得分:4)

你几乎就在那里:)

删除DocNum条款,因为有些客户只能拥有1个文档/发票号。例如:上面示例中的GROUP BY。同时删除SELECT CustName as 'CustomerName' , MAX(DocNum) AS 'Last invoice number' FROM TDocHeader GROUP BY CustName ,因为它不属于#include <stdio.h> #include <stdlib.h> #include <mpi.h> #define N 9 // array size int A[N] = {0,2,1,5,4,3,7,6,8}; // this is a dummy array that should only be initialized on rank == ROOT int main(int argc, char *argv[]) { int size; int rank; const int VERY_LARGE_INT = 999999; const int ROOT = 0; // the master rank that holds A to begin with int tag = 1234; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); // think size = 4 for this example MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* How many numbers you send from ROOT to each other rank. Note that for this implementation to work, (size-1) must divide N. */ int count = N/(size-1); int *localArray = (int *)malloc(count * sizeof(int)); int localMin; // minimum computed on rank i int globalMin; // will only be valid on rank == ROOT /* rank == ROOT sends portion of A to every other rank */ if (rank == ROOT) { for(int dest = 1; dest < size; ++dest) { // If you are sending information from one rank to another, you use MPI_Send or MPI_Isend. // If you are sending information from one rank to ALL others, then every rank must call MPI_Bcast (similar to MPI_Reduce below) MPI_Send(&A[(dest-1)*count], count, MPI_INT, dest, tag, MPI_COMM_WORLD); printf("P0 sent a %d elements to P%d.\n", count, dest); } localMin = VERY_LARGE_INT; // needed for MPI_Reduce below } /* Every other rank is receiving one message: from ROOT into local array */ else { MPI_Recv(localArray, count, MPI_INT, ROOT, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE); localMin = findMin(localArray, count); printf("Min for P%d is %d.\n", rank, localMin); } /* At this point, every rank in communicator has valid information stored in localMin. Use MPI_Reduce in order to find the global min among all ranks. Store this single globalMin on rank == ROOT. */ MPI_Reduce(&localMin, &globalMin, 1, MPI_INT, MPI_MIN, ROOT, MPI_COMM_WORLD); if (rank == ROOT) printf("Global min: %d\n", globalMin); /* The last thing you do is Finalize MPI. Nothing should come after. */ MPI_Finalize(); return 0; } 子句。

这是一个有效的SQLFiddle:http://sqlfiddle.com/#!3/117ca7/3

单击SQL Fiddle页面上的蓝色 Run SQL 按钮后,查看底部的输出。

enter image description here

这是SQL

MPI_Send

答案 1 :(得分:1)

包含数据样本的临时表

DECLARE @TDocHeader AS TABLE
    (
      CustName VARCHAR(10) ,
      DocNum INT
    )
INSERT  INTO @TDocHeader
VALUES  ( 'cust1', 1 ),
        ( 'cust1', 2 ),
        ( 'cust1', 3 ),
        ( 'cust2', 5 ),
        ( 'cust3', 7 ),
        ( 'cust3', 9 ),
        ( 'cust4', 11 ),
        ( 'cust4', 12 );

由@Shiva发布的变体(最佳)

SELECT  CustName AS CustomerName ,
        MAX(DocNum) AS [Last invoice number]
FROM    @TDocHeader
GROUP BY CustName

其他变体,仅供参考

SELECT  TOP 1 WITH TIES
        CustName AS CustomerName ,
        DocNum AS [Last invoice number]
FROM    @TDocHeader
ORDER BY ROW_NUMBER() OVER (PARTITION BY CustName ORDER BY CustName, DocNum DESC)

另一个附加变体,也仅适用于信息

SELECT  DISTINCT CustName AS CustomerName ,
        MAX(DocNum) OVER (PARTITION BY CustName) AS [Last invoice number]
FROM    @TDocHeader