2017-04-02 1 views
0

ich eine Tabelle wie unten Struktur haben:, wie eine Abfrage zu tun, indem sie einige ausgewählte Daten auf Gruppe auszuwählen

ID|PrcessorID|BatchNO|NoOfTransaction|BatchCreatedDate 
1 |20  |2  |3    |2017-03-28 
2 |21  |3  |3    |2017-04-01 
3 |21  |4  |7    |2017-04-01 
4 |20  |5  |3    |2017-04-01 
5 |21  |6  |2    |2017-04-02 
6 |20  |7  |4    |2017-04-02 

und eine andere Tabelle ist wie

ProcessorID|ProcessorName 
20   |Payme 
21   |Payany 

ich die Daten insgesamt bekommen haben Anzahl der Transaktionen und Anzahl der Charge an jedem bestimmten Datum von jedem spezifischen Prozessor von vor 3 Tagen Gruppe durch den Prozessor, so dass ich Daten wie erhalten kann:

, was ich jetzt bin doin ist:

Select a.ProcessorId As ProcessorID,b.ProcessorName As ProcessorName,BatchCreatedDate,Sum(NoofTRansaction) As TotNoofTransaction,Count(BatchNo) As BatchCountfrom TableA a innerjoin TableB b on a.ProcessorID=b.ProcessorID where BatchcreatedDate<GetDate() and BatchCreatedDate>GetDate()-4 groupby ProcessorName,BatchCreatedDate 

aber diese Abfrage gibt mir führen wie

PrcessorName|Batchcount|TotNoOfTransaction|BatchCreatedDate 

    Payany |1   |3     |2017-04-01 
    Payany |1   |7     |2017-04-01 
    Payme |1   |3     |2017-04-01 
    Payany |1   |2     |2017-04-02 
     Payme |1   |4     |2017-04-02 

Antwort

1

Meine beste Vermutung ist, dass BatchCreatedDate eine Zeitkomponente hat. Macht das was du willst?

Select a.ProcessorId As ProcessorID, b.ProcessorName As ProcessorName, 
     cast(BatchCreatedDate as date) as bcd, 
     Sum(NoofTRansaction) As TotNoofTransaction, 
     Count(BatchNo) As BatchCount 
from TableA a inner join 
    TableB b 
    on a.ProcessorID = b.ProcessorID 
where BatchcreatedDate < GetDate() and BatchCreatedDate > GetDate()-4 
group by a.ProcessorId, b.ProcessorName, cast(BatchCreatedDate as date); 
+0

ja sein ein Datetime-Feld – Tanmay

1
;WITH Cte1 (ID,ProcessorID,BatchNO,NoOfTransaction,BatchCreatedDate) 
As 
(
SELECT 1 ,20 ,2 ,3 ,'2017-03-28' UNION ALL 
SELECT 2 ,21 ,3 ,3 ,'2017-04-01' UNION ALL 
SELECT 3 ,21 ,4 ,7 ,'2017-04-01' UNION ALL 
SELECT 4 ,20 ,5 ,3 ,'2017-04-01' UNION ALL 
SELECT 5 ,21 ,6 ,2 ,'2017-04-02' UNION ALL 
SELECT 6 ,20 ,7 ,4 ,'2017-04-02' 
) 
,cte2(ProcessorID,ProcessorName) AS (
     SELECT 20,'Payme'UNION ALL 
     SELECT 21,'Payany' 
     ) 

SELECT DISTINCT cte2.ProcessorName 
    ,COUNT(BatchNO) OVER (
     PARTITION BY cte1.ProcessorID 
     ,Cte1.BatchCreatedDate ORDER BY Cte1.BatchCreatedDate 
     )As Batchcount 
    ,SUM(NoOfTransaction) OVER (
     PARTITION BY Cte1.ProcessorID 
     ,Cte1.BatchCreatedDate ORDER BY Cte1.BatchCreatedDate 
     ) As TotNoOfTransaction 
    ,cte1.BatchCreatedDate 
FROM Cte1 
INNER JOIN cte2 ON cte2.ProcessorID = Cte1.ProcessorID 
ORDER BY cte1.BatchCreatedDate 
Verwandte Themen