SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how to write the most efficient query ?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

anantharengan
Starting Member

India
2 Posts

Posted - 04/27/2012 :  15:53:59  Show Profile  Reply with Quote
comments (sno is an identity column)
--------

sno statusid areaid year comments date entityId
1 1 1 2012 abc 12/12/2010 1
2 2 1 2012 def 11/12/2010 1
3 3 1 2012 def 10/12/2010 1
4 1 2 2013 abc 09/12/2010 1
5 2 2 2013 def 08/12/2010 1
6 3 2 2013 def 07/12/2010 1
7 2 1 2013 def 06/12/2010 1
8 3 1 2013 def 05/12/2010 1
9 1 2 2012 abc 04/12/2010 2
10 2 2 2012 def 03/12/2010 2
11 3 2 2012 def 02/12/2010 2
12 1 2 2013 abc 01/12/2010 2
13 2 2 2013 def 30/11/2010 2
14 3 2 2013 def 29/11/2010 2



entity
------
entityId name
1 entity1
2 entity2


status
-------
statusid status-description
1 approved
2 disapproved
3 submitted


My i/p --> areaid (for example areadid=1)

my o/p should be

a largest year from comments table which contains all the entities(2 in this case)
and their last dated statusid should be 1(approved),if it satisfies this condition, then

that year should be the o/p


example 1
--------------
i/p : area id=1
o/p : should be empty


example 2
--------------
i/p : area id=2
o/p : 2013


I am able to get the o/p through my query but I had used table variables and while loops
how to achieve my goal in a most efficient way?

visakh16
Very Important crosS Applying yaK Herder

India
47040 Posts

Posted - 04/27/2012 :  16:06:42  Show Profile  Reply with Quote
quote:
Originally posted by anantharengan

comments (sno is an identity column)
--------

sno statusid areaid year comments date entityId
1 1 1 2012 abc 12/12/2010 1
2 2 1 2012 def 11/12/2010 1
3 3 1 2012 def 10/12/2010 1
4 1 2 2013 abc 09/12/2010 1
5 2 2 2013 def 08/12/2010 1
6 3 2 2013 def 07/12/2010 1
7 2 1 2013 def 06/12/2010 1
8 3 1 2013 def 05/12/2010 1
9 1 2 2012 abc 04/12/2010 2
10 2 2 2012 def 03/12/2010 2
11 3 2 2012 def 02/12/2010 2
12 1 2 2013 abc 01/12/2010 2
13 2 2 2013 def 30/11/2010 2
14 3 2 2013 def 29/11/2010 2



entity
------
entityId name
1 entity1
2 entity2


status
-------
statusid status-description
1 approved
2 disapproved
3 submitted


My i/p --> areaid (for example areadid=1)

my o/p should be

a largest year from comments table which contains all the entities(2 in this case)
and their last dated statusid should be 1(approved),if it satisfies this condition, then

that year should be the o/p


example 1
--------------
i/p : area id=1
o/p : should be empty


example 2
--------------
i/p : area id=2
o/p : 2013


I am able to get the o/p through my query but I had used table variables and while loops
how to achieve my goal in a most efficient way?




SELECT TOP 1 year
FROM table
where areaid = @yourareaidvalue
group by year 
having count(distinct entityid) = (select count(entityid) from entity)
ORDER BY year desc


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 04/27/2012 :  17:27:35  Show Profile  Visit SwePeso's Homepage  Reply with Quote
You can actually create a view and have the logic encapsulated there
DECLARE	@Comments TABLE
	(
		sno INT,
		StatusID INT,
		AreaID INT,
		Yr INT,
		Comments VARCHAR(100),
		Dte DATE,
		EntityID INT
	)

SET DATEFORMAT DMY

INSERT	@Comments
VALUES	( 1, 1, 1, 2012, 'abc', '12/12/2010', 1),
	( 2, 2, 1, 2012, 'def', '11/12/2010', 1),
	( 3, 3, 1, 2012, 'def', '10/12/2010', 1),
	( 4, 1, 2, 2013, 'abc', '09/12/2010', 1),
	( 5, 2, 2, 2013, 'def', '08/12/2010', 1),
	( 6, 3, 2, 2013, 'def', '07/12/2010', 1),
	( 7, 2, 1, 2013, 'def', '06/12/2010', 1),
	( 8, 3, 1, 2013, 'def', '05/12/2010', 1),
	( 9, 1, 2, 2012, 'abc', '04/12/2010', 2),
	(10, 2, 2, 2012, 'def', '03/12/2010', 2),
	(11, 3, 2, 2012, 'def', '02/12/2010', 2),
	(12, 1, 2, 2013, 'abc', '01/12/2010', 2),
	(13, 2, 2, 2013, 'def', '30/11/2010', 2),
	(14, 3, 2, 2013, 'def', '29/11/2010', 2)

DECLARE	@Entity TABLE
	(
		EntityID INT,
		Name VARCHAR(100)
	)

INSERT	@Entity
VALUES	(1, 'Entity1'),
	(2, 'Entity2')

DECLARE	@Status TABLE
	(
		StatusID INT,
		[Description] VARCHAR(100)
	)

INSERT	@Status
VALUES	(1, 'approved'),
	(2, 'disapproved'),
	(3, 'submitted')

-- SwePeso
SELECT		AreaID,
		CASE (SELECT COUNT(*) FROM @Entity)
			WHEN COUNT(*) THEN MAX(Yr)
			ELSE NULL
		END AS [Year]
FROM		(
			SELECT	AreaID,
				Dte,
				EntityID,
				StatusID,
				Yr,
				ROW_NUMBER() OVER (PARTITION BY AreaID, EntityID, DATEPART(YEAR, Dte) ORDER BY Dte DESC) AS rn
			FROM	@Comments
		) AS d
WHERE		rn = 1
		AND StatusID = (SELECT StatusID FROM @Status WHERE [Description] = 'Approved')
GROUP BY	AreaID



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

anantharengan
Starting Member

India
2 Posts

Posted - 04/27/2012 :  23:51:59  Show Profile  Reply with Quote
SwePeso,visakh16 thanks a lot for the reply. I tinkered a little bit in SwePeso's reply and got the answer. Thanks a lot.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000