SQL Server使用row_number和分区

时间:2013-08-29 10:23:10

标签: sql sql-server

以下我们通过这些条件获取列表。

我们最新付款日期的客户是输入日期,只有选定的付款方式(现金,cheq,信用卡等)

亲爱的Gordon Linof之前帮助我使用Row_number部分和查询工作。

但今天我们有一个奇怪的情况。当我们查看该客户未向我们发送付款时,查询并未获得列表棒中的客户。所以查询没有找到最后一个付款记录,因为这没有在列表中显示。

现在问题是......如果客户之前没有付款,我该如何更改此查询。

SELECT t.cari_kod 
       AS Cari_Kod, 
       t.cari_unvan1 
       AS Unvan, 
mikrodb_v14_dekor2011.dbo.Fn_carihesapanadovizbakiye('', 0, t.cari_kod, '', '', NULL, NULL, NULL, 0) AS BakiyeTutar,
t.cari_temsilci_kodu 
       AS TmsKodu, 
cpt.cari_per_adi 
       AS Adi, 
cpt.cari_per_soyadi 
       AS Soyadi, 
t.cha_tarihi 
       AS Tarih, 
t.cha_meblag 
       AS Tutar, 
Datediff(day, t.cha_tarihi, Getdate()) 
       AS GunFarki, 
t.cari_bolge_kodu 
       AS Bolge 
FROM   (SELECT cr.*, 
               chh.*, 
               Row_number() 
                 OVER ( 
                   partition BY chh.cha_kod 
                   ORDER BY chh.cha_recno DESC) AS seqnum 
        FROM   mikrodb_v14_dekor2011.dbo.cari_hesaplar cr 
               LEFT JOIN mikrodb_v14_dekor2011.dbo.cari_hesap_hareketleri chh 
                      ON chh.cha_kod = cr.cari_kod 
        WHERE  1 = 1 
               AND chh.cha_tip = 1 
               --and chh.cha_cinsi in (0,1,2,3,4,5,19,20)   
               AND chh.cha_evrak_tip IN ( 1, 2, 3, 4, 
                                          34, 65, 67, 98 ) 
               AND chh.cha_tarihi > '20130101') t 
       LEFT OUTER JOIN mikrodb_v14_dekor2011.dbo.cari_personel_tanimlari cpt 
                    ON ( t.cari_temsilci_kodu = cpt.cari_per_kod ) 
WHERE  t.seqnum = 1 
       AND t.cari_temsilci_kodu IN ( 'PKAD01', 'PKMS04' ) 
ORDER  BY 
mikrodb_v14_dekor2011.dbo.Fn_carihesapanadovizbakiye('', 0, cari_kod, '', '', NULL, NULL, NULL, 0) DESC 

0 个答案:

没有答案