Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL Server中计算特定场景的模式,如下所示_Sql_Sql Server_Stored Procedures_Mode - Fatal编程技术网

在SQL Server中计算特定场景的模式,如下所示

在SQL Server中计算特定场景的模式,如下所示,sql,sql-server,stored-procedures,mode,Sql,Sql Server,Stored Procedures,Mode,我有两张桌子 Table 1 ColumnA ColumnB Account1 DeptA Account2 DeptA Account3 DeptA Account4 DeptB Account5 DeptB Table 2 ColumnC ColumnA ColumnD ColumnE(Date) Deposit1 Account1 10 Deposit1 Account2 20 Deposit1 Account3 10 Deposit2 Account1 10 D

我有两张桌子

Table 1

ColumnA ColumnB 
Account1  DeptA
Account2  DeptA
Account3  DeptA
Account4  DeptB
Account5  DeptB


Table 2

ColumnC  ColumnA  ColumnD  ColumnE(Date)
Deposit1 Account1 10
Deposit1 Account2 20
Deposit1 Account3 10
Deposit2 Account1 10
Deposit2 Account2 30
Deposit2 Account3  30
Deposit3 Account2 20
Deposit3 Account3 10

Deposit1 Account4 20
Deposit1 Account5 20
Deposit2 Account4 10
Deposit2 Account5 20



Expected Output
Count Mode(ColumnD) ColumnC   ColumnB  
2       10            Deposit1  Dept1    
2       30            Deposit2   Dept1
1       10            Deposit3   Dept1
2       20            Deposit1   Dept2
1       10            Deposit2   Dept2
我应该能够在SQL中计算列D的模式,如下所示 根据表1, 对于给定的ColumnB,对该组中的所有ColumnA逐个ColumnB,计算具有ColumnD值的同一ColumnC的模式

如果我们观察预期的输出,Dept1有Account1、Account2和Account3。如果我们在Dept1和Dept2中都有,但是我们需要根据给定的Dept1和Dept2计算模式


存储过程或SQL都很有帮助

在SQL中计算模式非常简单-只需使用group by进行行号操作。我认为以下是您正在尝试做的:

select tt.*
from (select t1.columnb, t2.columnc, t2.columnd, count(*) as cnt,
             row_number() over (partition by t1.columnb, t2.columnc order by count(*) desc) as seqnum
      from table2 t2 join
           table1 t1
           on t2.columna = t1.columna
      group by t1.columnb, t2.columnc, t2.columnd
     ) tt
where seqnum = 1;

在SQL中计算模式非常简单-只需使用group by进行行号操作。我认为以下是您正在尝试做的:

select tt.*
from (select t1.columnb, t2.columnc, t2.columnd, count(*) as cnt,
             row_number() over (partition by t1.columnb, t2.columnc order by count(*) desc) as seqnum
      from table2 t2 join
           table1 t1
           on t2.columna = t1.columna
      group by t1.columnb, t2.columnc, t2.columnd
     ) tt
where seqnum = 1;

B栏的结果来自哪里?只能看到DeptA和B列B的可能重复项来自表1,外键列A在表2中关联columnB结果来自何处?只能看到DeptA和B列B的可能重复项来自表1,外键列A在表2中关联,而不是子查询中缺少的group by,您可能还需要在行\号分区中按t2.columnc分区。@zlk。谢谢。当每列C的D列有计数1时,如何使用日期?在这种情况下,我应该能够为Mode@TechJump当没有模式时,惯例是模式应表示为填充的最小/最大范围选择tt.*从选择t1.columnb、t2.columnc、t2.columnd,计数为cnt,按t1.columnb分区的行数,t2.columnc按计数排序,t2.columne desc作为表2中的seqnum t2在t2上连接表1 t1。columna=t1。columna按t1.columnb、t2.columnc、t2.columnd、t2.columne tt分组,其中seqnum=1;这似乎正确吗?除了子查询中缺少的group by之外,您可能还需要在row_number分区中按t2.columnc分区。@zlk。谢谢。当每列C的D列有计数1时,如何使用日期?在这种情况下,我应该能够为Mode@TechJump当没有模式时,惯例是模式应表示为填充的最小/最大范围选择tt.*从选择t1.columnb、t2.columnc、t2.columnd,计数为cnt,按t1.columnb分区的行数,t2.columnc按计数排序,t2.columne desc作为表2中的seqnum t2在t2上连接表1 t1。columna=t1。columna按t1.columnb、t2.columnc、t2.columnd、t2.columne tt分组,其中seqnum=1;这似乎正确吗?