|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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... |
|
#2
|
||||
|
||||
|
Apart from the usage of reserved words (count, year), what's wrong with this?
Or rather, would you care to explain the top 5 in both queries?
__________________
My blog about OpenSource Databases PDF tutorials about OSS databases, DBMonster ... Please contribute to Open Source Development, fill bug reports!!! Developer Shed eSupport Commented my.ini/my.cnf (PLEASE ADD YOUR OWN CONFIG TRICK) An introduction to database normalization Natural or Surrogate key Custom ordering for your results Correlated and uncorrelated subqueries Don't turn your outer joins into inner joins |
|
#3
|
||||
|
||||
|
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... |
|
#4
|
||||
|
||||
|
The original programmer should be beaten, and hard ...
|
|
#5
|
||||
|
||||
|
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. |
|
#6
|
||||
|
||||
|
I hope he is a thing of the past.
|
|
#7
|
||||
|
||||
|
Quote:
Yep! He quit 2 years ago, although not sure why he quit though. |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Simply the SQL query script by putting 2 seperate sql query into 1 sql query somehow? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|