SQL - 在未定义的时间间隔内查询记录

时间:2016-01-22 13:01:38

标签: sql sql-server datediff northwind

我需要使用SQL(确切地说是SQL Server)从convert -size 100x100 xc:"srgb(145,114,94)" average.png 数据库中的<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- GCM --> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:name="com.dummy.mobile.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.dummy.mobile.permission.C2D_MESSAGE" /> <android:uses-permission android:name="android.permission.READ_PHONE_STATE" /> <android:uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 表中获取以下数据:

  • 查找至少下3个订单的所有客户(Orders) 间隔为6个月(Northwind)。

因此,对于以下数据,查询应返回CustomerID

OrderDate

2 个答案:

答案 0 :(得分:1)

SELECT N1.CustomerID, COUNT(N2.OrderDate)  FROM Northwind N1
INNER JOIN Northwind N2
ON N1.CustomerID=N2.CustomerID
AND N1.OrderDate<=Dateadd(MONTH, 6, N2.OrderDate)
GROUP BY N1.CustomerID
HAVING COUNT(N2.OrderDate)>=3

答案 1 :(得分:-1)

尝试以下方法:

SELECT OrderDate, CustomerID FROM orders GROUP BY OrderDate HAVING   COUNT(CustomerID) >= 3 ORDER BY COUNT(CustomerID) DESC