Showing posts with label along. Show all posts
Showing posts with label along. Show all posts

Monday, March 12, 2012

Group by With count

Hi,
Iam using a count along with a group by condition. (Eg., Select count(col1),
col1 from table1 where col1 = <value> group by col1)
If I run the query and if no matching records if found the result doesnt sho
w anything. Why is this so?. But if I give
count(column) without the group by condition then a single record is fetched
based on no of records..
I not able to really see why the first one is not returning any records if t
he condition does not match.
Thanx in advance
regards
MaheshBasically, when the condition does not match, there is nothing is count (it
is a empty result set), which causes nothing to be displayed.
--
HTH,
SriSamp
Please reply to the whole group only!
http://www32.brinkster.com/srisamp
"Mahesh" <anonymous@.discussions.microsoft.com> wrote in message
news:0E67C7CE-7744-41D3-B733-17E6BF42E9E4@.microsoft.com...
quote:

> Hi,
> Iam using a count along with a group by condition. (Eg., Select

count(col1),col1 from table1 where col1 = <value> group by col1)
quote:

> If I run the query and if no matching records if found the result doesnt

show anything. Why is this so?. But if I give
quote:

> count(column) without the group by condition then a single record is

fetched based on no of records..
quote:

> I not able to really see why the first one is not returning any records if

the condition does not match.
quote:

>
> Thanx in advance
> regards
> Mahesh
>
|||If you want to see the count and the value of Col1 then you can do so like
this:
SELECT COUNT(*), <value>
FROM Table1
WHERE col1 = <value>
David Portas
--
Please reply only to the newsgroup
--

Group by With count

Hi,
Iam using a count along with a group by condition. (Eg., Select count(col1),col1 from table1 where col1 = <value> group by col1)
If I run the query and if no matching records if found the result doesnt show anything. Why is this so?. But if I give
count(column) without the group by condition then a single record is fetched based on no of records..
I not able to really see why the first one is not returning any records if the condition does not match.
Thanx in advance
regards
MaheshBasically, when the condition does not match, there is nothing is count (it
is a empty result set), which causes nothing to be displayed.
--
HTH,
SriSamp
Please reply to the whole group only!
http://www32.brinkster.com/srisamp
"Mahesh" <anonymous@.discussions.microsoft.com> wrote in message
news:0E67C7CE-7744-41D3-B733-17E6BF42E9E4@.microsoft.com...
> Hi,
> Iam using a count along with a group by condition. (Eg., Select
count(col1),col1 from table1 where col1 = <value> group by col1)
> If I run the query and if no matching records if found the result doesnt
show anything. Why is this so?. But if I give
> count(column) without the group by condition then a single record is
fetched based on no of records..
> I not able to really see why the first one is not returning any records if
the condition does not match.
>
> Thanx in advance
> regards
> Mahesh
>|||If you want to see the count and the value of Col1 then you can do so like
this:
SELECT COUNT(*), <value>
FROM Table1
WHERE col1 = <value>
--
David Portas
--
Please reply only to the newsgroup
--

Sunday, February 26, 2012

group by clause

Hi all,
i have 2 tables in pubs database, first one is titles and second
one is sales,
i want to get sum of quantity which is in sales table along with title_id
and title.
i'm executing this query
select titles.title_id,title,sum(qty) as quantity from titles join sales on
sales.title_id=titles.title_id group by titles.title_id
This is giving some error,I'm not able to find, where this error comingWhat error?
Anyway, look up GROUP BY in Books Online. You're selecting two columns (not
counting the aggregate), but only grouping by one.
ML
http://milambda.blogspot.com/|||Manish ML is right...
SELECT
titles.title_id
,title
,SUM(qty) as quantity
FROM
titles join sales on
sales.title_id=titles.title_id
GROUP BY titles.title_id
is what you had!
you just need to add the title column to the GROUP BY like so
SELECT
titles.title_id
,title
,SUM(qty) as quantity
FROM
titles join sales on
sales.title_id=titles.title_id
GROUP BY titles.title_id, titles.title
The reason: if a column is not in an aggregate function in the SELECT list
of a GROUP BY query, it must be included in the GROUP BY clause, therefore
you don't have to put SUM(qty) (since it's in an aggregate function) in the
GROUP BY, and in fact can't.
Regards
CharlesA