» TSQL Finding Order that occurred in 3 consecutive months
TSQL Finding Order that occurred in 3 consecutive months
|September 18, 2010
Posted by forumadmin
Please help me to generate the following query. Say I have customer table and order table.
OrderID OrderDate CustID
100 01-JAN-2000 1
101 05-FEB-2000 1
102 10-MAR-2000 1
103 01-NOV-2000 2
104 05-APR-2001 2
105 07-MAR-2002 2
106 01-JUL-2003 1
107 01-SEP-2004 4
108 01-APR-2005 4
109 01-MAY-2006 3
110 05-MAY-2007 1
111 07-JUN-2007 1
112 06-JUL-2007 1
I want to find out the customers who have made orders on three successive months. (Query using SQL server 2005 and 2008 is allowed).
The desired output is:
CustName Year OrderDate
AA 2000 01-JAN-2000
AA 2000 05-FEB-2000
AA 2000 10-MAR-2000
AA 2007 05-MAY-2007
AA 2007 07-JUN-2007
AA 2007 06-JUL-2007
More Related Questions
- SQL update query using joins I have to update a field with a value which is returned by a join of 3 tables.
,im.sku as iSku
,gm.SKU as GSKU
,mm.ManufacturerId as ManuId
- How important is the order of columns in indexes? I've heard that you should put columns that will be the most selective at the beginning of the index declaration. Example:
CREATE NONCLUSTERED INDEX MyINDX on Table1
- How can I do an UPDATE statement with JOIN in SQL? I need to update this table in MS SQL Server 2005 with data from its 'parent' table, see below:
- How to SELECT * INTO [temp table] FROM [stored procedure] How do I do a SELECT * INTO [temp table] FROM [stored procedure]? Not FROM [Table] and without defining [temp table]?
Select all data from BusinessLine into tmpBusLine works fine.
- SQL Server – pull X random records per state I have a table with records for each zip code in the united states. For the purposes of displaying on a map, I need to select X random records per state. How would I go about doing […]
- How do I create unique constraint that also allows nulls in sql server Adding a column to table, and I want a unique constraint, so the column will insert GUID's, but to allow for existing data before column was added, I need to allow for NULL's in the […]
- How to append to a text field in t-sql SQL Server 2005 What is the best way to append to a text field using t-sql in Sql Server 2005?
With a varchar I would do this.
update tablename set fieldname = fieldname + 'appended string'
But this […]
- Get top 1 row of each group I have a table which I want to get the latest entry for each group. Here's the table:
|ID| DocumentID | Status | DateCreated |
| 2| 1 | S1 | […]
- sql query for displaying start/end points with time interval I have a table called vehicle_Summary and the data loks like:
A_Date vehicle_Name Location_Name
2012-02-08 09:36:20.000 AA000AA […]
- sp_replmonitorsubscriptionpendingcmds into a table [How2] Im looking to making a job that will tell me the number of pending cmd's on the replication,
Im using sp_replmonitorsubscriptionpendingcmds and im trying my best to put it into a […]