1

这是我的表 [属性]:

Loanno  Balance amount  PropertyType
1001045 308731.770000     1
1001045 2007700.740000    2
1001045 3087318905.770    3
1001045 308731.770000     4
1001046 306589.67         1
1001046 456321.23         1
1001046 6932542.89        1
1001047 582563.56         1
1001047 965421.34         2
1001048 567894.34         1
1001048 567894.34         2

我必须获取余额最高的 [Loanno] 的属性类型。如果最高余额金额相同,并且如果贷款编号的属性类型不同,那么对于该贷款编号,我必须将属性类型填充为“8”。所以我的最终输出应该是这样的:

Loanno  PropertyType
1001045     3
1001046     1
1001047     2
1001048     8

编辑

这是我尝试过的,但我得到了重复的记录。

SELECT DISTINCT 
    LT.LOANNO, 
    IIF(COUNTS.MAX_BALANCE > 1, 8,LT.PROPERTY_TYPE) AS PROPERTY_TYPE1 
FROM 
    PROPERTY LT 
    INNER JOIN
    ( 
        SELECT 
            DISTINCT_ROWS.LOANNO, 
            COUNT(DISTINCT_ROWS.MaxBalance) AS MAX_BALANCE 
        FROM
            (
                SELECT DISTINCT 
                    L.LOANNO, 
                    MaxBalance 
                FROM 
                    PROPERTY AS L 
                    INNER JOIN 
                    (
                        SELECT 
                            LOANNO, 
                            MAX(BALANCE_AMOUNT) AS MaxBalance 
                        FROM PROPERTY 
                        GROUP BY LOANNO
                    ) AS SUB 
                        ON (L.LOANNO=SUB.LOANNO) 
                            AND (L.BALANCE_AMOUNT=SUB.MaxBalance)
            ) AS DISTINCT_ROWS
        GROUP BY DISTINCT_ROWS.LOANNO 
    ) AS COUNTS
        ON LT.LOANNO=COUNTS.LOANNO 
GROUP BY LT.LOANNO, IIF(COUNTS.MAX_BALANCE > 1, 8, LT.PROPERTY_TYPE)
4

2 回答 2

2

对于 Access SQL,您的查询要求具有挑战性。自定义 VBA 函数将允许您使用更简单的SELECT语句,但会带来您可能无法接受的问题:

  1. UDF(用户定义函数)只能在 Access 会话中运行的查询中使用......当您使用其他代码(.Net、Java、PHP、VBScript 等)连接到数据库并运行查询时不能使用.
  2. UDF 可能很慢。

如果您可以使用 UDF,则使用该GetPropertyType函数(见下文)的此查询将返回您所要求的内容。注意我用作tblProperties表名是因为Property是保留字。我还假设 Long 的数据类型为Loanno, Currency 为Balance_amount, Long 为Property_Type

SELECT
    sub.Loanno,
    GetPropertyType(sub.Loanno,sub.MaxBalance) AS PropertyType
FROM
    (
        SELECT
            Loanno,
            Max(Balance_amount) AS MaxBalance
        FROM tblProperties
        GROUP BY Loanno
    ) AS sub
ORDER BY sub.Loanno;

这是我用 Access 2007 测试的功能。

Public Function GetPropertyType(ByVal pLoanno As Long, _
        ByVal pBalance_amount As Currency) As Long

    Const cstrQdf As String = "qryLoanPropertyTypesCount"
    Dim db As DAO.database
    Dim qdf As DAO.QueryDef
    Dim lngReturn As Long

    Set db = CurrentDb
    Set qdf = db.QueryDefs(cstrQdf)
    qdf.Parameters("which_Loanno") = pLoanno
    qdf.Parameters("which_Balance_amount") = pBalance_amount

    If qdf.OpenRecordset()(0) > 1 Then
        lngReturn = 8
    Else
        lngReturn = DLookup("Property_Type", "tblProperties", _
            "Loanno=" & pLoanno & " AND Balance_amount=" & _
            pBalance_amount)
    End If

    Set qdf = Nothing
    Set db = Nothing
    GetPropertyType = lngReturn
End Function

该函数使用此保存的参数查询qryLoanPropertyTypesCount

PARAMETERS which_Loanno Long, which_Balance_amount Currency;
SELECT Count(*) AS num_PropertyTypes
FROM
    (
        SELECT DISTINCT
            p.Loanno,
            p.Balance_amount,
            p.Property_Type
        FROM tblProperties AS p
        WHERE
                p.Loanno = [which_Loanno]
            AND p.Balance_amount = [which_Balance_amount]
    ) AS sub;
于 2013-05-27T15:56:13.363 回答
1

这是我构建查询的方式:

第 1 步:创建一个查询以查找每个 [Loanno] 的最大余额并将该查询保存为 [Loanno_MaxBalance]:

SELECT 
    Loanno, 
    MAX([Balance amount]) AS MaxBalance 
FROM [Property] 
GROUP BY Loanno

步骤 2a:创建一个查询来计算具有最大余额的行数,使用我们上面保存的查询来保持简单:

SELECT 
    [Property].Loanno, 
    [Property].[Balance amount], 
    COUNT(*) AS RowCount 
FROM 
    [Property]
    INNER JOIN
    [Loanno_MaxBalance]
        ON Loanno_MaxBalance.Loanno=[Property].Loanno 
            AND Loanno_MaxBalance.MaxBalance=[Property].[Balance amount]
GROUP BY [Property].Loanno, [Property].[Balance amount]

步骤 2b:看起来并不太可怕,所以让我们将步骤 1 中的 SQL 合并为子查询:

SELECT 
    [Property].Loanno, 
    [Property].[Balance amount], 
    COUNT(*) AS RowCount 
FROM 
    [Property]
    INNER JOIN
    (
        SELECT 
            Loanno, 
            MAX([Balance amount]) AS MaxBalance 
        FROM [Property] 
        GROUP BY Loanno
    ) AS Loanno_MaxBalance
        ON Loanno_MaxBalance.Loanno=[Property].Loanno 
            AND Loanno_MaxBalance.MaxBalance=[Property].[Balance amount]
GROUP BY [Property].Loanno, [Property].[Balance amount]

所以现在这个查询独立存在,我们不需要将 [Loanno_MaxBalance] 作为单独保存的查询保存在 Access 中。

我们将上述查询保存为 [Loanno_MaxBalance_Count]。

步骤 3a:现在使用 [Property] 表和 [Loanno_MaxBalance_Count] 查询导出 [PropertyType] 值:

SELECT DISTINCT 
    [Property].Loanno, 
    IIf(Loanno_MaxBalance_Count.RowCount>1, 8, [Property].PropertyType) AS PropertyType
FROM 
    [Property]
    INNER JOIN 
    [Loanno_MaxBalance_Count] 
        ON [Property].Loanno=Loanno_MaxBalance_Count.Loanno 
            AND [Property].[Balance amount]=Loanno_MaxBalance_Count.[Balance amount]

步骤 3b:哎呀,这还不算太糟糕。让我们“去做”并将 [Loanno_MaxBalance_Count] 查询引用替换为其 SQL 代码(来自步骤 2b)作为子查询:

SELECT DISTINCT 
    [Property].Loanno, 
    IIf(Loanno_MaxBalance_Count.RowCount>1, 8, [Property].PropertyType) AS PropertyType
FROM 
    [Property]
    INNER JOIN 
    (
        SELECT 
            [Property].Loanno, 
            [Property].[Balance amount], 
            COUNT(*) AS RowCount 
        FROM 
            [Property]
            INNER JOIN
            (
                SELECT 
                    Loanno, 
                    MAX([Balance amount]) AS MaxBalance 
                FROM [Property] 
                GROUP BY Loanno
            ) AS Loanno_MaxBalance
                ON Loanno_MaxBalance.Loanno=[Property].Loanno 
                    AND Loanno_MaxBalance.MaxBalance=[Property].[Balance amount]
        GROUP BY [Property].Loanno, [Property].[Balance amount]
    ) AS Loanno_MaxBalance_Count
        ON [Property].Loanno=Loanno_MaxBalance_Count.Loanno 
            AND [Property].[Balance amount]=Loanno_MaxBalance_Count.[Balance amount]

而已!一个独立的查询,无需保存 Access 查询依赖项。

于 2013-05-27T20:58:21.150 回答