MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 10th, 2008, 01:17 PM
fletchsod's Avatar
fletchsod fletchsod is offline
I lov C in AIX/Linux, hate C++
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2003
Location: Jacksonville, Florida
Posts: 1,521 fletchsod User rank is Corporal (100 - 500 Reputation Level)fletchsod User rank is Corporal (100 - 500 Reputation Level)fletchsod User rank is Corporal (100 - 500 Reputation Level)fletchsod User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 2 Days 19 h 45 m 9 sec
Reputation Power: 8
Simply the SQL query script by putting 2 seperate sql query into 1 sql query somehow?

Anyone know how to combine these 2 sql queries into 1? Noticed it use #tmpResult which I'm trying to remove. By taking out the #tmpResult, I noticed it's kind of tought to do so by fitting the "NumDays" field into everything. So, I welcome some help.

Code:
SELECT TOP 5
	SoldID, 
	Year, 
	MakeID, 
	ModelID, 
	SalePrice, 
	DATEDIFF(Day, StockDate, SoldDate) AS NumDays
INTO 	#tmpResult 
FROM 		tblSold
WHERE SaleType = 'R'


Code:
SELECT	TOP 5 COUNT(SoldID) AS [Count], 
	[Year], 
	MakeID, 
	ModelID, 
	AVG(SalePrice),
	AVG(NumDays)
FROM  #tmpResult
GROUP BY   [Year], 
		MakeID, 
		ModelID
ORDER BY AVG(NumDays) ASC


Thanks...

Reply With Quote
  #2  
Old July 11th, 2008, 03:33 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,826 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 1 Day 10 h 29 m 39 sec
Reputation Power: 278
Apart from the usage of reserved words (count, year), what's wrong with this?
sql Code:
Original - sql Code
  1. SELECT 
  2.   TOP 5
  3.  COUNT(SoldID) AS [COUNT],
  4.  [Year],
  5.  MakeID,
  6.  ModelID,
  7.  AVG(ts.SalePrice),
  8.  AVG(DATEDIFF(Day, StockDate, SoldDate))
  9. FROM 
  10.  tblSold
  11. GROUP BY   
  12.  [Year],
  13.  MakeID,
  14.  ModelID
  15. ORDER BY
  16.  AVG(DATEDIFF(Day, StockDate, SoldDate)) ASC
Or rather, would you care to explain the top 5 in both queries?

Reply With Quote
  #3  
Old July 11th, 2008, 08:30 AM
fletchsod's Avatar
fletchsod fletchsod is offline
I lov C in AIX/Linux, hate C++
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2003
Location: Jacksonville, Florida
Posts: 1,521 fletchsod User rank is Corporal (100 - 500 Reputation Level)fletchsod User rank is Corporal (100 - 500 Reputation Level)fletchsod User rank is Corporal (100 - 500 Reputation Level)fletchsod User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 2 Days 19 h 45 m 9 sec
Reputation Power: 8
Yea, that would work... I'm surprised the DATEDIFF() works now in the "ORDER BY" clause. It didn't yesterday, so I must had made an error somewhere and MSSQL gave me the wrong error message.

The original programmer did this...

Code:
/*****************************************/
CREATE TABLE #tmpGroup
(
	[Count] INT,
	[Year] INT,
	MakeID VARCHAR(50),
	ModelID VARCHAR(50),
	[SalePrice] Decimal(19,2),
	[Avg Days in Inventory] INT
)
/*****************************************/

SELECT * INTO 	#tmpResult 
FROM 		vwSoldPriceNational

INSERT INTO #tmpGroup	
(
            [Count],
	[Year],
	MakeID,
	ModelID,
	[SalePrice],
	[Avg Days in Inventory])
SELECT	TOP 5 
             COUNT(SoldID) AS [Count], 
	[Year], 
	MakeID, 
	ModelID, 
	AVG(SalePrice),
	AVG(NumDays)
FROM	#tmpResult
GROUP BY  [Year], 
	MakeID, 
	ModelID
ORDER BY AVG(NumDays) DESC

SELECT [Year],
	MakeID AS 'Make',
	ModelID AS 'Model',
	SalePrice AS 'Sale Price',
	[Avg Days in Inventory]
FROM #tmpGroup


So, I was revising and revising to get down to one sql query. So the best one I got was in the original posting. I had to cuz it takes too long to get the result.

Thanks for the posting. Sometime having 2 heads are better than one if I had trouble pin-pointing or narrowing down the script. I got to cut the response time from 14 seconds to 3 seconds with the new sql query.

Thanks...

Reply With Quote
  #4  
Old July 11th, 2008, 09:02 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,826 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 1 Day 10 h 29 m 39 sec
Reputation Power: 278
The original programmer should be beaten, and hard ...
Comments on this post
fletchsod agrees!

Reply With Quote
  #5  
Old July 11th, 2008, 09:09 AM
fletchsod's Avatar
fletchsod fletchsod is offline
I lov C in AIX/Linux, hate C++
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2003
Location: Jacksonville, Florida
Posts: 1,521 fletchsod User rank is Corporal (100 - 500 Reputation Level)fletchsod User rank is Corporal (100 - 500 Reputation Level)fletchsod User rank is Corporal (100 - 500 Reputation Level)fletchsod User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 2 Days 19 h 45 m 9 sec
Reputation Power: 8
Yea! You should have seen how many views, stored-procedures and unnecessary physical tables the original programmer did. It's a nightmare and it's been giving me headache, especially with so many jumping around in the script. LOL! You should have see the way he talks and the ways he makes everyone feel small. I'm still working on cleaning up and removing waste stuffs for almost 3 years now. The problem with him is he doesn't think and he does things the easy way.

Last edited by fletchsod : July 11th, 2008 at 09:17 AM.

Reply With Quote
  #6  
Old July 11th, 2008, 09:20 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,826 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 1 Day 10 h 29 m 39 sec
Reputation Power: 278
I hope he is a thing of the past.

Reply With Quote
  #7  
Old July 11th, 2008, 11:21 AM
fletchsod's Avatar
fletchsod fletchsod is offline
I lov C in AIX/Linux, hate C++
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2003
Location: Jacksonville, Florida
Posts: 1,521 fletchsod User rank is Corporal (100 - 500 Reputation Level)fletchsod User rank is Corporal (100 - 500 Reputation Level)fletchsod User rank is Corporal (100 - 500 Reputation Level)fletchsod User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 2 Days 19 h 45 m 9 sec
Reputation Power: 8
Quote:
Originally Posted by pabloj
I hope he is a thing of the past.


Yep! He quit 2 years ago, although not sure why he quit though.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Simply the SQL query script by putting 2 seperate sql query into 1 sql query somehow?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway