按日期在SQL中分组

时间:2017-08-21 12:37:56

标签: sql sql-server sql-server-2008

                SelectPhoneContact selectContact = new SelectPhoneContact();

                ArrayList<String> MatchingContacts = new ArrayList<String>();
                try {
                    JSONArray Object = new JSONArray(JsonArrayMatchingContacts);
                    for (int x = 0; x < Object.length(); x++) {
                        final JSONObject obj = Object.getJSONObject(x);
                        MatchingContacts.add(obj.getString("contact_phonenumber"));

                    }

                } catch(Exception e) {
                    e.printStackTrace();
                }

                if (phoneNumberofContact.equals(MatchingContacts))
                {

                    phoneNumberofContact= "Already a contact";
                    selectPhoneContacts.add(selectContact);
                } else {
                    selectPhoneContacts.add(selectContact);
                }

                selectContact.setName(name);
                selectContact.setPhone(phoneNumberofContact);

数据:

SELECT  DISTINCT AccountID,  
        FolderID,   
        EffectiveDate, 
        MAX(SUBSTRING(VersionNumber, CHARINDEX('_',VersionNumber )+1, LEN(VersionNumber))) as VersionNumber
FROM [Temp].[DocumentList] 
WHERE IsMajor = 1
AND AccountID=331  
GROUP BY EffectiveDate, VersionNumber, FolderID, AccountID

我想要一个日期的最大版本号

5 个答案:

答案 0 :(得分:2)

您可以通过选择已选择的所有内容并使用MAX()功能GROUP BY来实现此目的:

SELECT AccountID, FolderID, EffectiveDate, MAX(VersionNumber) VersionNumber
FROM
(
    SELECT AccountID, FolderID, EffectiveDate,Substring(VersionNumber,Charindex('_',VersionNumber )+1, 
    LEN(VersionNumber))) as VersionNumber
    FROM        [Temp].[DocumentList] 
    WHERE      IsMajor =   1
               AND AccountID=331 
) t  
GROUP BY AccountID, FolderID, EffectiveDate  

答案 1 :(得分:0)

这样的事情会起作用吗?

SELECT MAX(VersionNumber) FROM TableName WHERE EffectiveDate=@parameter

在EffectiveDate出现的任何地方,您都会选择VersionNumber的最高值。它可能需要一点点推动和修补,但这应该是它的机制!

Here's a link to the MIN and MAX function!

答案 2 :(得分:0)

SELECT AccountID, FolderID, EffectiveDate, VersionNumber
FROM
(
    SELECT AccountID, FolderID, EffectiveDate, VersionNumber, ROW_NUMBER() OVER(PARTITION BY EffectiveDate ORDER BY VersionNumber DESC) AS row_num
    FROM
    Temp.DocumentList
)A
WHERE row_num = 1

答案 3 :(得分:0)

希望这是你想要的。

select EffectiveDate,max(VersionNumber) as VersionNumber
from
(
        SELECT  DISTINCT AccountID,  
        FolderID,   
        EffectiveDate, 
        MAX(SUBSTRING(VersionNumber, CHARINDEX('_',VersionNumber )+1,      LEN(VersionNumber))) as VersionNumber
        FROM [Temp].[DocumentList] 
        WHERE IsMajor = 1
        AND AccountID=331  
        GROUP BY EffectiveDate, VersionNumber, FolderID, AccountID
) q
  group by EffectiveDate     

答案 4 :(得分:0)

试试这个

    SELECT  DISTINCT AccountID,  
        FolderID,   
        EffectiveDate, 
        MAX(SUBSTRING(VersionNumber, CHARINDEX('_',VersionNumber )+1, LEN(VersionNumber))) as VersionNumber
FROM [Temp].[DocumentList] 
WHERE IsMajor = 1
AND AccountID=331  
GROUP BY EffectiveDate, FolderID, AccountID

您想要找到最大版本号,因此请不要按

分组