GROUP BY多个派生的Max()和Min()列

时间:2014-10-06 14:52:59

标签: sql-server tsql group-by max

我正在尝试创建一个脚本,该脚本将按照我MAX()的一系列列对表内容进行分组。

没有方案很难描述:

我有一个预订表,我必须从单个客户创建一个表,客户由res系统拆分,每个res系统需要使用不同列的不同分组。

I.E:

277000 bookings for Ipcos
289300 bookings for Daph
300000 bookings for Tard

它们都存储在同一个表中,我想取所有列的最大值,除了一对我必须转换成整数和sum()其他列。

当我必须按照我创建的已铸造值和我创建的Min()值进行分组时,我的问题就来了。

我尝试将桌子加到彼此身上但是没有用,有人可以指出我正确的方向,因为非常沮丧。

选择Ipcos的代码

SELECT
    SUM(CAST(TotalCost AS MONEY)) AS TotalCost, 
    NettCost = Null,    
    Paid = Null,    
    Balance = Null, 
    Discount = Null,    
    Commission = Null,  
    Max(Adults) AS Adults,  
    Max(Children)AS Children,
    Max(Infants) AS Infants,
    Max(PAX) AS PAX,
    Sum(CAST(Duration AS int)) AS Duration,
    MAX([IdentityValue]) AS IdentityValue,
    MAX([FileName]) AS FileName,
    MAX([SheetName]) AS SheetName,
    MAX([LineNum]) AS LineNum,
    MAX([BookingDate]) AS BookingDate,
    CAST([DepartureDate] AS Int) AS IntDepartureDate,
    CAST([BookingDate] AS int) AS IntBookingDate,
    MIN(LineNum) AS MinLineNum
FROM 
    Booking
WHERE ResID = 3
GROUP BY 
    MinLineNum, IntBookingDate, IntDepartureDate

其他系统与此非常相似

任何帮助都会在adavnce中获得梦幻般的欢呼

1 个答案:

答案 0 :(得分:0)

您可以在不同的列中按顺序使用over(partition by)语句。看这里Over clause