根据日期区分

时间:2018-07-05 10:49:27

标签: sql-server

我在这里的某个人的帮助下编写了这种表结构和查询,它可以完美地工作,但是我想根据日期对行进行排序。 这是结构和示例数据:

DROP TABLE [TransactionMaster];
DROP TABLE [VoucherType];

CREATE TABLE [VoucherType](
    [VoucherTypeCode] [tinyint] NOT NULL PRIMARY KEY,
    [FullName] [nvarchar](255) NOT NULL
);


INSERT INTO [VoucherType] VALUES (1, 'Cash Payment Voucher');
INSERT INTO [VoucherType] VALUES (2, 'Cash Receipt Voucher');
INSERT INTO [VoucherType] VALUES (3, 'Bank Payment Voucher');
INSERT INTO [VoucherType] VALUES (4, 'Bank Receipt Voucher');


CREATE TABLE [TransactionMaster](
    [ID] [bigint] NOT NULL PRIMARY KEY,
    [VoucherTypeCode] [tinyint] NOT NULL,
    [PayeeName] [varchar](255) NOT NULL,
    [TransactionDate] datetime,
    [RefNo] [nvarchar](50) NULL
    CONSTRAINT [FK_tbl_TransactionMaster_tbl_VoucherType] FOREIGN KEY([VoucherTypeCode])
    REFERENCES [VoucherType] ([VoucherTypeCode])
)



INSERT INTO [TransactionMaster] VALUES (1, 2, 'Asim', '2018-03-21', 'CRV-0001-LHR');
INSERT INTO [TransactionMaster] VALUES (2, 4, 'Ali', '2018-03-21', 'BRV-2421-KHI');
INSERT INTO [TransactionMaster] VALUES (3, 1, 'Erick', '2018-03-23', 'CPV-5435-ISL');
INSERT INTO [TransactionMaster] VALUES (4, 3, 'Asim', '2018-03-24', 'BPV-2345-CAN');
INSERT INTO [TransactionMaster] VALUES (5, 2, 'Mehboob', '2018-03-25', 'CRV-2976-PSH');
INSERT INTO [TransactionMaster] VALUES (6, 1, 'Erick', '2018-03-25', 'CPV-2323-KOH');

这是查询

SELECT tb1.Refno
  ,tb1.[FullName] AS VType
  ,tb1.PayeeName
  ,tb2.Refno
  ,tb2.[FullName] AS VType
  ,tb2.PayeeName
FROM 
  (
    (
      SELECT 
         ROW_NUMBER() OVER ( ORDER BY id ) AS rowid
        ,TransactionMaster.RefNo
        ,TransactionMaster.PayeeName
        ,[VoucherType].FullName
      FROM 
        TransactionMaster
      INNER JOIN 
        [VoucherType] 
          ON TransactionMaster.VoucherTypeCode = [VoucherType].VoucherTypeCode
      WHERE TransactionMaster.[VoucherTypeCode] IN ( 1,3 )
    ) tb1 
    FULL OUTER JOIN 
      (
        SELECT 
           ROW_NUMBER() OVER ( ORDER BY id ) AS rowid
          ,TransactionMaster.RefNo
          ,TransactionMaster.PayeeName
          ,[VoucherType].FullName
        FROM 
          TransactionMaster
        INNER JOIN 
          [VoucherType] 
            ON TransactionMaster.VoucherTypeCode = [VoucherType].VoucherTypeCode
        WHERE TransactionMaster.[VoucherTypeCode] IN ( 2,4 )
      ) tb2 ON tb1.rowid = tb2.rowid
  )

现在的输出是这样的:

Refno           VType                   PayeeName   TransactionDate         Refno           VType                   PayeeName   TransactionDate

CPV-5435-ISL    Cash Payment Voucher    Erick       2018-03-23 00:00:00.000 CRV-0001-LHR    Cash Receipt Voucher    Asim        2018-03-21 00:00:00.000
BPV-2345-CAN    Bank Payment Voucher    Asim        2018-03-24 00:00:00.000 BRV-2421-KHI    Bank Receipt Voucher    Ali         2018-03-21 00:00:00.000
CPV-2323-KOH    Cash Payment Voucher    Erick       2018-03-25 00:00:00.000 CRV-2976-PSH    Cash Receipt Voucher    Mehboob     2018-03-25 00:00:00.000

我想按升序显示日期,如果该日期不存在优惠券,则应为null。 我将预期的输出放在下面

Refno           VType                   PayeeName   TransactionDate                 Refno           VType                   PayeeName   TransactionDate

                                                                                    CRV-0001-LHR    Cash Receipt Voucher    Asim        2018-03-21 00:00:00.000
                                                                                    BRV-2421-KHI    Bank Receipt Voucher    Ali         2018-03-21 00:00:00.000
CPV-5435-ISL    Cash Payment Voucher    Erick       2018-03-23 00:00:00.000
BPV-2345-CAN    Bank Payment Voucher    Asim        2018-03-24 00:00:00.000 
CPV-2323-KOH    Cash Payment Voucher    Erick       2018-03-25 00:00:00.000         CRV-2976-PSH    Cash Receipt Voucher    Mehboob     2018-03-25 00:00:00.000

2 个答案:

答案 0 :(得分:1)

您可以使用COALESCE来确定日期,然后按顺序进行排序。希望这能解决您的问题

with data1 as (
select a.id,a.VoucherTypeCode,PayeeName,TransactionDate,RefNo,FullName from TransactionMaster a inner join  [VoucherType] b on a.VoucherTypeCode = b.VoucherTypeCode
where a.VoucherTypeCode in (1,3)
),

data2 as (
select a.id,a.VoucherTypeCode,PayeeName,TransactionDate,RefNo,FullName from TransactionMaster a inner join  [VoucherType] b on a.VoucherTypeCode = b.VoucherTypeCode
where a.VoucherTypeCode in (2,4)
)
select *,COALESCE(a.TransactionDate,b.TransactionDate) as FullDate from data1 a full join data2 b on a.TransactionDate = b.TransactionDate
order by FullDate

结果

enter image description here

答案 1 :(得分:1)

尝试一下

select tb1.Refno, 
        tb1.[FullName] as VType, 
        tb1.PayeeName, 
        tb2.Refno, 
        tb2.[FullName] as VType, 
        tb2.PayeeName 
from(
        (
            select ROW_NUMBER()over (partition by TransactionDate order by id) as rowid, 
                    TransactionMaster.RefNo, 
                    TransactionMaster.PayeeName, 
                    [VoucherType].FullName,
                  TransactionMaster.TransactionDate
            from TransactionMaster 
                    inner join [VoucherType] on  TransactionMaster.VoucherTypeCode = [VoucherType].VoucherTypeCode
            where TransactionMaster.[VoucherTypeCode] in (1,3)
        )tb1
            full outer join (
                                select ROW_NUMBER()over (partition by TransactionDate order by id) as rowid, 
                                        TransactionMaster.RefNo, 
                                        TransactionMaster.PayeeName, 
                                        [VoucherType].FullName,
                                    TransactionMaster.TransactionDate
                                from TransactionMaster 
                                        inner join [VoucherType] on  TransactionMaster.VoucherTypeCode = [VoucherType].VoucherTypeCode
                                where TransactionMaster.[VoucherTypeCode] in (2,4)
                            ) tb2 on tb1.rowid = tb2.rowid and tb1.TransactionDate = tb2.TransactionDate
)
相关问题