Sql 如何通过查询选择group by上的某些选定数据

Sql 如何通过查询选择group by上的某些选定数据,sql,sql-server,Sql,Sql Server,我有一个如下结构的表格: 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

我有一个如下结构的表格:

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
另一张桌子是这样的

ProcessorID|ProcessorName
20         |Payme
21         |Payany
我必须获得每个特定处理者在每个特定日期的交易总数和批次计数数据(由处理者在3天前分组),以便我可以获得如下数据:

PrcessorName|Batchcount|TotNoOfTransaction|BatchCreatedDate

     Payany |2         |10                |2017-04-01
     Payme  |1         |3                 |2017-04-01
     Payany |1         |2                 |2017-04-02
      Payme |1         |4                 |2017-04-02 
我现在正在做的是:

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

我最好的猜测是,
BatchCreatedDate
有一个时间组件。这是你想要的吗

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);
选择a.ProcessorId作为ProcessorId,b.ProcessorName作为ProcessorName,
将(BatchCreatedDate作为日期)转换为bcd,
总计(无交易)为无交易,
计数(BatchNo)为BatchCount
从表a到内部连接
表b
在a.ProcessorID=b.ProcessorID上
其中BatchcreatedDateGetDate()-4
按a.ProcessorId、b.ProcessorName、cast(BatchCreatedDate作为日期)分组;

我最好的猜测是
BatchCreatedDate
有一个时间组件。这是你想要的吗

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);
选择a.ProcessorId作为ProcessorId,b.ProcessorName作为ProcessorName,
将(BatchCreatedDate作为日期)转换为bcd,
总计(无交易)为无交易,
计数(BatchNo)为BatchCount
从表a到内部连接
表b
在a.ProcessorID=b.ProcessorID上
其中BatchcreatedDateGetDate()-4
按a.ProcessorId、b.ProcessorName、cast(BatchCreatedDate作为日期)分组;
;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