Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to query from virtual table?

Author  Topic 

Vaishu
Posting Yak Master

178 Posts

Posted - 2008-10-28 : 13:49:38
HI

I have table as below

URL---------------------ProductID------------------SearchTerms
http://www.some.com--------234----------------------Null
http://www.2.com-----------234----------------------Fuji
http://www.3.com-----------476---------------------- LI
http://www.some.com--------539-----------------------GH
http://www.2.com-----------234-----------------------rt

Expected result is

URL-------------------SearchTerms----ProductID------NunOfHits
http://www.some.com------Null-----------234----------- 3
http://www.3.com----------LI------------476----------- 1
http://www.some.com-------GH------------539----------- 1
http://www.2.com----------Fuji-------------------------
http://www.2.com-----------rt--------------------------

Could someone please Help??

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-28 : 14:07:58
Define NunofHits. where is it stored at.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-29 : 00:09:54
make be this

SELECT URL,SearchTerms,
CASE WHEN Seq=1 THEN ProductID ELSE NULL END AS ProductID,
CASE WHEN Seq=1 THEN NumOFHits ELSE NULL END AS NumOFHits
FROM
(
SELECT URL,SearchTerms,ProductID,COUNT(*) OVER(PARTITION BY ProductID) AS NumOfHits,
ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY SearchTerms) AS Seq
FROM Table)t
ORDER BY NumOFHits DESC,ProductID ASC
Go to Top of Page

Vaishu
Posting Yak Master

178 Posts

Posted - 2008-10-29 : 05:54:24
HI

I am getting Incorrect syntax near second select command. Also NumOfHits is the counts of ProdutId.

quote:
Originally posted by visakh16

make be this

SELECT URL,SearchTerms,
CASE WHEN Seq=1 THEN ProductID ELSE NULL END AS ProductID,
CASE WHEN Seq=1 THEN NumOFHits ELSE NULL END AS NumOFHits
FROM vWTable
(
SELECT URL,SearchTerms,ProductID,COUNT(*) OVER(PARTITION BY ProductID) AS NumOfHits,
ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY SearchTerms) AS Seq
FROM vWTable)t
ORDER BY NumOFHits DESC,ProductID ASC


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-29 : 06:02:09
quote:
Originally posted by Vaishu

HI

I am getting Incorrect syntax near second select command. Also NumOfHits is the counts of ProdutId.

quote:
Originally posted by visakh16

make be this

SELECT URL,SearchTerms,
CASE WHEN Seq=1 THEN ProductID ELSE NULL END AS ProductID,
CASE WHEN Seq=1 THEN NumOFHits ELSE NULL END AS NumOFHits
FROM vWTable
(
SELECT URL,SearchTerms,ProductID,COUNT(*) OVER(PARTITION BY ProductID) AS NumOfHits,
ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY SearchTerms) AS Seq
FROM vWTable)t
ORDER BY NumOFHits DESC,ProductID ASC





this is not what i posted. remove the view name near FROM
Go to Top of Page

Vaishu
Posting Yak Master

178 Posts

Posted - 2008-10-29 : 06:04:46
Hi

Fantastic. Thanks a lot SUPERMAN.

quote:
Originally posted by visakh16

quote:
Originally posted by Vaishu

HI

I am getting Incorrect syntax near second select command. Also NumOfHits is the counts of ProdutId.

quote:
Originally posted by visakh16

make be this

SELECT URL,SearchTerms,
CASE WHEN Seq=1 THEN ProductID ELSE NULL END AS ProductID,
CASE WHEN Seq=1 THEN NumOFHits ELSE NULL END AS NumOFHits
FROM vWTable
(
SELECT URL,SearchTerms,ProductID,COUNT(*) OVER(PARTITION BY ProductID) AS NumOfHits,
ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY SearchTerms) AS Seq
FROM vWTable)t
ORDER BY NumOFHits DESC,ProductID ASC





this is not what i posted. remove the view name near FROM

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-29 : 06:05:44
Cheers
Go to Top of Page
   

- Advertisement -