Hi,
I have a query like
select tbl1.f1, tbl2.f2, count(tbl1.f1) as YY from tbl1, tbl2 where
tbl1.f1=tbl2.f2 group by tbl1.f1, tbl2.f2
for example, I can get the results like
f1 f2 YY
--
x1 y1 2
x2 y2 5
x3 y3 1
As you can see, total rows for the query is 3 (rows). How can I get
the "3" in the one select query like:
f1 f2 YY Total_Rows
--
x1 y1 2 3
x2 y2 5 3
x3 y3 1 3
or write another query to count the total rows (to get the "3") only?
Thanks a lot.
Ouyanghi
just see this:
select tbl1.f1, tbl2.f2, count(tbl1.f1) as YY ( select count(*) from tbl1,
tbl2 where
tbl1.f1=tbl2.f2 group by tbl1.f1, tbl2.f2 ) as total_rows from tbl1, tbl2
where
tbl1.f1=tbl2.f2 group by tbl1.f1, tbl2.f2
probably this solves your problem
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---
"zxo102" wrote:
> Hi,
> I have a query like
> select tbl1.f1, tbl2.f2, count(tbl1.f1) as YY from tbl1, tbl2 where
> tbl1.f1=tbl2.f2 group by tbl1.f1, tbl2.f2
> for example, I can get the results like
>
> f1 f2 YY
> --
> x1 y1 2
> x2 y2 5
> x3 y3 1
>
> As you can see, total rows for the query is 3 (rows). How can I get
> the "3" in the one select query like:
> f1 f2 YY Total_Rows
> --
> x1 y1 2 3
> x2 y2 5 3
> x3 y3 1 3
> or write another query to count the total rows (to get the "3") only?
>
> Thanks a lot.
> Ouyang
>|||You may also want to summarise the data in a report style query using
COMPUTE, the link below will take you to an example on how to implement
this:
http://www.mssql.com.au/kb/html/gmg... />
&@.sa_id=58
"zxo102" <zxo102@.gmail.com> wrote in message
news:1122690598.655507.63480@.g47g2000cwa.googlegroups.com...
> Hi,
> I have a query like
> select tbl1.f1, tbl2.f2, count(tbl1.f1) as YY from tbl1, tbl2 where
> tbl1.f1=tbl2.f2 group by tbl1.f1, tbl2.f2
> for example, I can get the results like
>
> f1 f2 YY
> --
> x1 y1 2
> x2 y2 5
> x3 y3 1
>
> As you can see, total rows for the query is 3 (rows). How can I get
> the "3" in the one select query like:
> f1 f2 YY Total_Rows
> --
> x1 y1 2 3
> x2 y2 5 3
> x3 y3 1 3
> or write another query to count the total rows (to get the "3") only?
>
> Thanks a lot.
> Ouyang
>|||Hi Chandra,
this sql query:
"select count(*) from tbl1, tbl2 where tbl1.f1=tbl2.f2 group by
tbl1.f1, tbl2.f2"
gives group count instead of total count from the query with "group
by".
Any ideas?
Thanks.
Ouyang|||hi
can you please send the ddl so that i can help u out
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---
"zxo102" wrote:
> Hi Chandra,
> this sql query:
> "select count(*) from tbl1, tbl2 where tbl1.f1=tbl2.f2 group by
> tbl1.f1, tbl2.f2"
> gives group count instead of total count from the query with "group
> by".
> Any ideas?
> Thanks.
> Ouyang
>|||hi
i am sorry, you can try this:
select count(*) from tbl1, tbl2 where tbl1.f1=tbl2.f2
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---
"zxo102" wrote:
> Hi Chandra,
> this sql query:
> "select count(*) from tbl1, tbl2 where tbl1.f1=tbl2.f2 group by
> tbl1.f1, tbl2.f2"
> gives group count instead of total count from the query with "group
> by".
> Any ideas?
> Thanks.
> Ouyang
>|||Hi Guillaume,
Thanks for your information. I got the total row using "compute".
But the returned results from SQL server are in two parts. The value
of the total row is in the second part.
Since I call this query from python application which can not grab the
value from the second part, do you know how to get the total row in a
query which just return the total row only?
Thanks a lot.
Ouyang|||Hi Chandra,
My ddl is as follows:
select t_all_status.enter_diag AS r_ICDCODE,
count(t_all_status.enter_diag) AS r_SUM2,
t_icd.chinese_diag AS r_CHINESE,
t_dept.dept_name as r_DEPT_NAME
from t_all_status, t_icd,t_dept
where (t_all_status.enter_dept=t_dept.dept_code)
AND (t_all_status.enter_diag not like 'M%')
AND (t_all_status.enter_diag = t_icd.icdcode)
group by t_dept.dept_name,t_all_status.enter_diag, t_icd.chinese_diag
order by r_SUM2 DESC
Thanks
Ouyang|||Put the row count in an output variable. Maybe something like this:
select *
from ...
set @.finalRowCount = @.@.rowcount
@.finalRowCount must be declared as an in/out parameter of your procedure.
ML|||Hi ML,
I don't know very much about the stuff you mentioned, can you give
me an exmaple for that using my DDL as follows:
select t_all_status.enter_diag AS r_ICDCODE,
count(t_all_status.enter_diag) AS r_SUM2,
t_icd.chinese_diag AS r_CHINESE,
t_dept.dept_name as r_DEPT_NAME
from t_all_status, t_icd,t_dept
where (t_all_status.enter_dept=3Dt_dep=ADt.dept_code)
AND (t_all_status.enter_diag not like 'M%')
AND (t_all_status.enter_diag =3D t_icd.icdcode)
group by t_dept.dept_name,t_all_status.=ADenter_diag,
t_icd.chinese_diag
order by r_SUM2 DESC=20
I really appraciate your help.
Ouyang
Wednesday, March 7, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment