Solved

I want to know how many rows the result is

Posted on 2011-03-24
16
265 Views
Last Modified: 2012-05-11
select i.orderid,i.used,o.availability
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64
order by i.orderid desc


I want to know how many rows the result is
0
Comment
Question by:rgb192
  • 6
  • 5
  • 3
  • +1
16 Comments
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility

use select @@ROWCOUNT after you run the query
select i.orderid,i.used,o.availability
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64
order by i.orderid desc

select @@ROWCOUNT

Open in new window

0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
select @@ROWCOUNT
0
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility

or you just want the count only

select count(1) as 'Total Count'
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64
0
 

Author Comment

by:rgb192
Comment Utility

select i.orderid,i.used,o.availability,count(1) as 'Total Count'
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64


Msg 8120, Level 16, State 1, Line 1
Column 'orderitems.orderid' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


I need the query and the rowcount in one query
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
you have an aggregate (count) without a group by.

can you tell us exactly what you are trying to acheive?
0
 

Author Comment

by:rgb192
Comment Utility
i have a query of which i return results

i want to know how many rows the results are

the number of rows = the number of i.orderid
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
select i.orderid,i.used,o.availability,(select count(*) from orderitems where orderid = i.orderid) as 'Total Count'
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64

0
 

Author Comment

by:rgb192
Comment Utility
select i.orderid,i.used,o.availability,(select count(*) from orderitems where orderid = i.orderid) as 'Total Count'
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64



there are 6 rows = 6 results  but 'total count' is 1


I need this in one query because I am using this query in php not sql server
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
can you show a sample of your data and the expected result?
0
 
LVL 5

Expert Comment

by:bitref
Comment Utility
select i.orderid,i.used,o.availability,(select count(*) from orderitems) as 'Total Count'
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64 

Open in new window

0
 

Author Comment

by:rgb192
Comment Utility
select i.orderid,i.used,o.availability,(select count(*) from orderitems) as 'Total Count'
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64


now 'total count' is the number of rows in all of table orderitems

i want 'total count' to be the number of rows returned
0
 
LVL 5

Expert Comment

by:bitref
Comment Utility
Declare @Count int;

select @Count = Count(*)
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64 ;

select i.orderid,i.used,o.availability,@Count as 'Total Count'
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64 

Open in new window

0
 

Author Comment

by:rgb192
Comment Utility
Declare @Count int;

select @Count = Count(*)
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64 ;

select i.orderid,i.used,o.availability,@Count as 'Total Count'
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64





works... is there anyway to do it in one query
0
 
LVL 5

Expert Comment

by:bitref
Comment Utility
With(
select Count(*) as c
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64) as cc
select i.orderid,i.used,o.availability,cc.c as 'Total Count'
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64 

Open in new window

0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
or this:

select i.orderid,i.used,o.availability
,(select Count(*)
from orderitems i2
inner join orders o2 on o2.orderid = i2.orderid
right join payments y2 on o2.orderid = y2.orderid
inner join products p2 on i2.productid = p2.productid
where o2.statusid in (9,10,90,180,260) and y2.type is not null
and i2.sku=4
and i2.productid=64) as 'Total Count'
from orderitems i
inner join orders o on o.orderid = i.orderid
right join payments y on o.orderid = y.orderid
inner join products p on i.productid = p.productid
where o.statusid in (9,10,90,180,260) and y.type is not null
and i.sku=4
and i.productid=64



0
 

Author Closing Comment

by:rgb192
Comment Utility
bitref:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'as'.

emoreau:
works and displays the rowcount.  thank you
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now