• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

how can I create a rowcount in my query?

Hello everybody

I want to make a query like this:

SELECT TOP 15 * FROM TABLE

15 rows will return, but I would like a field that could by a countrow, is there any way to do this?

So I wouldl have a result like this:

1 house
2 paper
3 phone
4 chair

and so on

thanks
0
hidrau
Asked:
hidrau
  • 4
  • 3
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
what version of sql server?
can you put the query into a stored procedure?
0
 
hidrauAuthor Commented:
2005
0
 
hidrauAuthor Commented:
no, I can't use a store proc
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
RamanhpCommented:
SELECT Count(field1) AS Count, Field1
FROM (select  SELECT TOP 15 * FROM TABLE)
GROUP BY field1
0
 
RamanhpCommented:
oops,

here's the soln.


SELECT Count(field1) AS Count, Field1
FROM (SELECT TOP 15 * FROM TABLE)
GROUP BY field1
0
 
hidrauAuthor Commented:
I am having this error
Incorrect syntax near the keyword 'GROUP'.
0
 
RamanhpCommented:
its working fine at my end



 select count(field_name), field_name from (SELECT TOP 15 * FROM tablename) group by field_name


in the above query, just replace the
field_name
that i suppose it
ItemType
in your table

and update the tableaname to the name of your table

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
with sql 2005:
SELECT TOP 15 t.*, ROW_NUMBER() OVER ( ORDER BY 1 ) r
  FROM TABLE t

Open in new window

0
 
hidrauAuthor Commented:
Perfect this solution :))

Thanks very much
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now