查询中的结果重复

col17t5w  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(174)

我遇到了一个特定的sql server 2016查询报告重复行的问题,我不确定如何最好地消除它而不弄乱数据。请原谅我的业余写作技巧?下面是查询。我遇到问题的字段是[tender type]字段。交易可以有多个投标。例如:如果你在商店买东西,一半用现金,一半用信用卡支付,它会在dtu支付表中写两行。理想情况下,如果出现这种情况,[tender type]将显示类似“multiple”的内容,但即使它只显示第一个[tender type],我也会很高兴。如果您能提供任何建议,我将不胜感激。

--Return Detail
declare @StoreName varchar(50);
declare @StartDate varchar(50);
declare @EndDate varchar(50);

set @StoreName = '8582 Brevard'
set @StartDate = '03/13/2020'
set @EndDate = '03/13/2020'

select 
       s.name as [Store],
       concat(sf.staff_code, ' - ',sf.Firstname,' ',sf.lastname) as [Associate],
       dtp.cais as [Terminal],
       dtp.transnum as [Trans #],
       P.Product_code as [SKU],
       PN.Short_name as [Description],
       dtp.qty as [Qty],
       dtp.price_sold as [Amt (Ea.)],
       concat(v.VIPCode, ' - ',v.VIPGName,' ',v.VIPName) as [Customer],
       ISNULL(DTPy.tender_code, 'Exchange') as [Tender Type],
       cast(dtp.TS_ID as date) as [Date]
from 
       DT_product DTP 
       LEFT JOIN store s on s.store_code_id = DTP.STORE_CODE_ID
       LEFT JOIN RETAIL_TRANSACTION rt on 
             rt.cais = dtp.Cais 
             and rt.STORE_CODE_ID = dtp.STORE_CODE_ID 
             and rt.TRANSNUM = dtp.Transnum
             and rt.TRANSTYPE = dtp.TRANSTYPE
       LEFT JOIN Staff sf on sf.staff_id = rt.OPERATOR_ID
       LEFT JOIN PRODUCT_NAME PN on PN.Product_ID = DTP.PRODUCT_ID
       LEFT JOIN Product P on P.Product_ID = DTP.Product_ID
       LEFT JOIN VIP v on v.VIPCode_id = rt.VIPCODE_ID
       LEFT JOIN DT_Payment DTPy on 
             DTPy.cais = dtp.Cais 
             and DTPy.STORE_CODE_ID = dtp.RETURN_STORE_ID 
             and DTPy.TRANSNUM = dtp.Transnum
             and DTPy.TRANSTYPE = dtp.TRANSTYPE
where 
       DTP.qty < 0
       and DTP.TRANSTYPE = 'SALE'
       and s.name = @StoreName
       AND format(cast(rt.transdate as date), 'MM/dd/yyyy') >= @StartDate
       and format(cast(rt.transdate as date), 'MM/dd/yyyy') <= @EndDate
Order by
       cast(dtp.ts_id as date),
       [Trans #]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题