2017-12-11 1 views
1
select 
    l.LocationName, l.LocationPrefixCode, 
    sum(case lt.transid when 1 then lt.Points else 0 end) as TotalEarnpoints, 
    sum(case lt.transid when 2 then lt.points else 0 end) as TotalRedeempoints, 
    sum(case lt.transid when 1 then 1 else 0 end) as NoOfBillsEarned, 
    sum(case lt.transid when 2 then 1 else 0 end) as NoOfBillsRedeemed 
from 
    InvLoyaltyTransaction lt 
inner join 
    dbo.Location l ON lt.LocationID = l.LocationID 
where 
    cast(DocumentDate as date) between convert(date, '" + fromDate + "', 103) 
            and convert(date, '" + toDate + "', 103) 
    and CardType in (select CardMasterID 
        from CrmReportCondition) 
    and lt.LocationID != 1 
group by 
    l.LocationName, l.LocationPrefixCode 
order by 
    l.LocationName 
+0

ist das einfache SQL? Sie sollten keine doppelten Anführungszeichen in Ihrer Abfrage haben. – Badiparmagi

+0

Verwenden Sie '@' am Anfang Ihrer SQL-Abfragezeichenfolge. Dann können Sie Zeilen ohne Verwendung von '+' oder '' 'umbrechen. –

+0

Welche Bedingung prüfen Sie in der CASE-Klausel? Die Syntax lautet: CASE WHEN THEN ELSE END. Es gibt nichts zwischen" CASE "und" WHEN " –

Antwort

0
select 
l.LocationName, l.LocationPrefixCode, 
sum(case when lt.transid=1 then lt.Points else 0 end) as TotalEarnpoints, 
sum(case when lt.transid=2 then lt.points else 0 end) as TotalRedeempoints, 
sum(case when lt.transid=1 then 1 else 0 end) as NoOfBillsEarned, 
sum(case when lt.transid=2 then 1 else 0 end) as NoOfBillsRedeemed 
from 
    InvLoyaltyTransaction lt 
inner join 
    dbo.Location l ON lt.LocationID = l.LocationID 
where 
    cast(DocumentDate as date) between convert(date, '" + fromDate + "', 103) 
            and convert(date, '" + toDate + "', 103) 
    and CardType in (select CardMasterID 
        from CrmReportCondition) 
    and lt.LocationID != 1 
group by 
    l.LocationName, l.LocationPrefixCode 
order by 
    l.LocationName 
Verwandte Themen