Home > Blockchain >  currentgroup() equivalent in sql?
currentgroup() equivalent in sql?

Time:07-07

My dataset: enter image description here

I want to write SQL statement to group by and 1) add Quantity; 2) calculate earliest date based on the group by. In Dax I wrote this:

Table 2 = GROUPBY(
    'Table',[Region],"Total Quantity",sumx(CURRENTGROUP(),[Quantity]),"First Available",MinX(CURRENTGROUP(),[Availability])
)

What is the SQL equivalent? Thank you

CodePudding user response:

If the RDBMS is MS-SQL then the equivalent is

select 
  Region, 
  SUM(Quantity) as [Total Quantity], 
  MIN(Availabilty) as [First Available] 
from 
  @t1 
group by 
  Region /*you need to add the grouping columns manually here*/

CodePudding user response:

Nevermind, after trial and error, I solved this myself:

SELECT product, region, min(availability) as 'First Available', sum (quantity) as N from [table2] group by product, region
  • Related