?
Solved

I want to know how many rows the result is

Posted on 2011-03-24
16
Medium Priority
?
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +1
16 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35209433

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 70

Expert Comment

by:Éric Moreau
ID: 35209437
select @@ROWCOUNT
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35209445

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:rgb192
ID: 35209475

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 70

Expert Comment

by:Éric Moreau
ID: 35209498
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
ID: 35209528
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 70

Expert Comment

by:Éric Moreau
ID: 35209556
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
ID: 35209593
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
 
LVL 70

Expert Comment

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

Expert Comment

by:bitref
ID: 35209764
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
ID: 35210262
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
ID: 35210439
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
ID: 35210644
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
ID: 35210699
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 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 35211710
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
ID: 35298767
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 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