Thursday, March 29, 2012

Count query problem :(

Hi all,

Hope someone can help me with this ...

This is my query. The problem with it is that it only returns Areas with >0
Topics. So if a new Area is created, it hasn't a topic until one is
created, and it doesn't list!

Is it possible to return even those rows where the count is 0, or what is
another way around this?

Thanks in advance, Jen

SELECT dbo.chat_board.boardID, dbo.chat_area.areaID,
dbo.chat_area.name, dbo.chat_area.datecreated,
COUNT(dbo.chat_topic.topicID) AS counttopics
FROM dbo.chat_board INNER JOIN
dbo.chat_area ON
dbo.chat_board.boardID = dbo.chat_area.boardID INNER JOIN
dbo.chat_topic ON
dbo.chat_area.areaID = dbo.chat_topic.areaID
GROUP BY dbo.chat_board.boardID, dbo.chat_area.areaID,
dbo.chat_area.name, dbo.chat_area.datecreated

--
Fast Track On Line -Web Design and Development
Portfolio http://www.fasttrackonline.co.uk

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.692 / Virus Database: 453 - Release Date: 28/05/2004On Wed, 2 Jun 2004 11:55:27 +0100, Jenny wrote:

>Hi all,
>Hope someone can help me with this ...
>This is my query. The problem with it is that it only returns Areas with >0
>Topics. So if a new Area is created, it hasn't a topic until one is
>created, and it doesn't list!
>Is it possible to return even those rows where the count is 0, or what is
>another way around this?
>Thanks in advance, Jen
>SELECT dbo.chat_board.boardID, dbo.chat_area.areaID,
> dbo.chat_area.name, dbo.chat_area.datecreated,
> COUNT(dbo.chat_topic.topicID) AS counttopics
>FROM dbo.chat_board INNER JOIN
> dbo.chat_area ON
> dbo.chat_board.boardID = dbo.chat_area.boardID INNER JOIN
> dbo.chat_topic ON
> dbo.chat_area.areaID = dbo.chat_topic.areaID
>GROUP BY dbo.chat_board.boardID, dbo.chat_area.areaID,
> dbo.chat_area.name, dbo.chat_area.datecreated

Hi Jenny,

Try replacing the second "INNER JOIN" with "LEFT OUTER JOIN". Check Books
Online for the details.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

count query problem

I have a table with a list of clients, some tasks associated with each and the status of each task (open, closed).

I want to return a SINGLE row for each client, along with a COUNT of the number of "open" and "closed" tasks.

The typical count statement I am using is this:

select SiteName, Count(IssueStatus) as cnt, IssueStatus,
from IssueMaster
group by SiteName, IssueStatus

It works fine, however, it returns a row for each combination of client and status. I would like it to return "Client, Count of Open, Count of Closed".

Any ideas?It depends on which SQL dialect you are using, but I'd use Sum() combined with CASE instead of Count().

-PatP|||I'm using MSSQL, but can I SUM a non-numeric column? And do you have an example I could use as a model?

Thanks!|||Like this:

SUM (CASE WHEN status='OPEN' THEN 1 ELSE 0 END) AS open_count|||Or still using COUNT:

COUNT (CASE WHEN status='OPEN' THEN 1 END) AS open_count

Count Query Performance

I have a query that seems simple but apparently is not.Any thoughts on performance optimization would be helpful.The following query works, but it takes 20 minutes to run.

WITH

MEMBER [Measures].[90-100] AS

'COUNT( FILTER( EXISTING [Student].[Student].[Student].Members, [Measures].[Avg Attendance] >.90)

)'

MEMBER [Measures].[80-90] AS

'COUNT(FILTER( EXISTING [Student].[Student].[Student].Members, [Measures].[Avg Attendance] >.80

AND [Measures].[Avg Attendance] <.90)

)'

SELECT

{[Measures].[90-100], [Measures].[80-90]} ON COLUMNS,

NON EMPTY EXISTING [School].[By District Area].[School].MEMBERS ON ROWS

FROM [Student Engagement]

There has got to be a better way. My guess is that somehow I need to limit the resulting set of schools * students that get counted to only the nonempties, but I’m not sure how to formulate the MDX.

Any ideas?

Chris

Instead of EXISTING in the calculated measures, which isn't needed unless other attributes of the [Student] dimension are placed on query axes, you could try NonEmpty(), like:

WITH

MEMBER [Measures].[90-100] AS

COUNT( FILTER( NonEmpty([Student].[Student].[Student].Members,

{[Measures].[AttendFactCount]}),

[Measures].[Avg Attendance] >.90)

)

MEMBER [Measures].[80-90] AS

COUNT(FILTER( NonEmpty([Student].[Student].[Student].Members,

{[Measures].[AttendFactCount]}),

[Measures].[Avg Attendance] >.80

AND [Measures].[Avg Attendance] <.90)

)

where [AttendFactCount] is some cube measure from the relevant measure group (assuming that [Avg Attendance] is a calculated measure like: [PresentSum] / [AttendFactCount]).|||

Deepak-

Thank you! Inserting the “NonEmpty” worked. The query returned in 5 seconds.

The question is why.Given, that I had “Non Empty” in the select statement and the NonEmptyBehavior(NEB) set to [Measures].[Absence] for the calculated member [Measures].[Avg Attendance] (Avg Attendance = Absence/ Enrollment) in the initial query,I would have assumed that the query engine would suppress the non empties automatically based on the NEB.Can you help me understand?

Chris

|||

Chris,

The “Non Empty” in the select statement wouldn't help because, if the 2 calculated measures are simply defined as counts, they could be 0, but never empty. Regarding the NEB, I'm not sure; but it could be that the query optimizer doesn't infer from the condition: [Avg Attendance] > 0.9 that [Avg Attendance] isn't empty. You could check whether changing the condition to: Not IsEmpty([Avg Attendance]) And [Avg Attendance] > 0.9 speeds up the query.

Count Query Help

I am not too good at programming but I need to get the
names and the count of the selected tables (Tables that
starts with aa). I should have the following fromat.
Tablename Count
table1 256
table2 347
..... ...
I wrote a while statement but my select at the end don't
work.
Can anyone show me how my query should look like '
Thanks for any help..There isn't any queries that I know of that will return the table names.
This would have to be done in program code there is probable an API for it,
however the query for the count is simple if you can get the table name (in
code) beginning with aa:
SELECT count(*) FROM TableName
Hope it helps
Gav
"George" <anonymous@.discussions.microsoft.com> wrote in message
news:8e6801c40841$78eb9ce0$a601280a@.phx.gbl...
> I am not too good at programming but I need to get the
> names and the count of the selected tables (Tables that
> starts with aa). I should have the following fromat.
> Tablename Count
> table1 256
> table2 347
> ..... ...
> I wrote a while statement but my select at the end don't
> work.
> Can anyone show me how my query should look like '
> Thanks for any help..|||Here is a basic rowcount routine:
use pubs
go
SELECT A.name, B.rows=20
FROM sysobjects A
JOIN sysindexes B ON A.ID =3D B.ID
WHERE A.type =3D 'U'
AND B.INDID < 2=20
ORDER BY A.Name
--=20
Keith
"George" <anonymous@.discussions.microsoft.com> wrote in message =
news:8e6801c40841$78eb9ce0$a601280a@.phx.gbl...
> I am not too good at programming but I need to get the=20
> names and the count of the selected tables (Tables that=20
> starts with aa). I should have the following fromat.
>=20
> Tablename Count
> table1 256
> table2 347
> ..... ...
>=20
> I wrote a while statement but my select at the end don't=20
> work.
> Can anyone show me how my query should look like '
>=20
> Thanks for any help..|||That did it.
Thanks a lot.

>--Original Message--
>Here is a basic rowcount routine:
>use pubs
>go
>SELECT A.name, B.rows
>FROM sysobjects A
>JOIN sysindexes B ON A.ID = B.ID
>WHERE A.type = 'U'
> AND B.INDID < 2
>ORDER BY A.Name
>--
>Keith
>
>"George" <anonymous@.discussions.microsoft.com> wrote in
message news:8e6801c40841$78eb9ce0$a601280a@.phx.gbl...
don't
>.
>sql

Count query

Hi

I had posted this question earlier but could not get the solution, may be i was not clear with the doubts i had

I need to count the no of students for the different Intervention field which are like 14 different types

against the field gender (male and Female )and field Ethnicity (horizontal field headers) 5 different types below

A student can be Male and Hispanic Type 1

also

Male Hispanic Type 2

Male Hispanic Type 3

So his count is made in three places

I need to do this

for the whole District level

then for each Center under District level

then for each school under Center level

DISTRICT SUMMARY

Intervention

Total

Male

Female

Asian

Black

Hispanic

Indian

Multiracial

White

TYPE : 1

7

3

4

4

2

0

0

0

1

TYPE :2

6

3

3

1

0

4

0

0

1

TYPE : 3

3

1

2

1

2

0

0

0

0

TYPE : 4

5

2

3

2

3

0

0

0

0

TYPE : 5

1

0

1

1

0

0

0

0

0

TYPE : 7

2

1

1

0

2

0

0

0

0

TYPE : 8

1

1

0

0

0

1

0

0

0

TYPE : 9

2

1

1

0

1

0

0

0

1

TYPE : 14

1

1

0

0

1

0

0

0

0

Please help

Thanks

What does your table(s) structure look like?

|||

Hi

I am joining these TWO tables based on student ID

The fields Ethnicity and field Gender both come from one table Table A

The field Intervention Types comes from Table B

Table B is

a group of three tables because the table that has intervention types does not have student_id

so I need to link it via a common field with another table

This below is how i am working on the code

select

SUBQUERY.student_id,
SUBQUERY.Intervention_ID,
STDM.student_ethnicity,
STDM.student_gender,


count( case when STDM.STUDENT_ETHNICITY in('Asian') then STDM.student_id else NULL end) as [Asian],

count( case when STDM.STUDENT_ETHNICITY in ('Black') then STDM.student_id else NULL end) as [Black],

count( case when STDM.STUDENT_ETHNICITY in('Hispanic') then STDM.student_id else NULL end) as [Hispanic],

count( case when STDM.STUDENT_ETHNICITY in('American Indian') then STDM.student_id else NULL end) as [American Indian],

count( case when STDM.STUDENT_ETHNICITY in('Multiracial') then STDM.student_id else NULL end) as [Multiracial],

count( case when STDM.STUDENT_ETHNICITY in('White') then STDM.student_id else NULL end) as [White],

count( case when STDM.STUDENT_GENDER in('M') then STDM.student_id else NULL end) as [Male],

count( case when STDM.STUDENT_GENDER in('F') then STDM.student_id else NULL end) as [Female]

from DW_student.dbo.Student STDM
right join
(select distinct
student_id,INTV.intervention_id from
WAIN.DBO.Meeting MTNG
inner join WAIN.DBO.Meeting_Intervention MTGI on MTNG.Meeting_ID = MTGI.Meeting_ID
inner join WAIN.DBO.Intervention INTV on MTGI.Intervention_ID = INTV.Intervention_ID
) SUBQUERY
on SUBQUERY.student_id = STDM.student_id

group by
STDM.SCHOOL_REGION,
STDM.SCHOOL_NUMBER,
STDM.SCHOOL_NAME,
SUBQUERY.Intervention_ID,
STDM.STUDENT_ETHNICITY,
SUBQUERY.student_id,
STDM.STUDENT_GENDER
order by
STDM.SCHOOL_REGION,
STDM.SCHOOL_NUMBER,
STDM.SCHOOL_NAME,
STDM.STUDENT_ETHNICITY,
SUBQUERY.Intervention_ID asc,
SUBQUERY.student_id,
STDM.STUDENT_GENDER


|||This looks like it would be a lot easier to do in a cube or pivot table. Where type is your dimension on rows and ethnicity is your dimension on columns, and count would be the measure.|||

Can you please explain it relating it to the present example

Thanks

Sowmya

|||

Your logic is motly used when PIVOTing the Columns. I didn't find any table design changes here. Your data is perfect.

You are almost on correct track – Your logic & intention of the expression almost perfect. But you need to change the expression slightly to achive your result,

- Use 0 as ELSE value on the Case When

- Use Sum instead of Count

Here the updated query,

select

subquery.student_id,

subquery.intervention_id,

stdm.student_ethnicity,

stdm.student_gender,

sum(case when stdm.student_ethnicity in ('asian') then 1 else 0 end) as [Asian],

sum(case when stdm.student_ethnicity in ('black') then 1 else 0 end) as [Black],

sum(case when stdm.student_ethnicity in ('hispanic') then 1 else 0 end) as [Hispanic],

sum(case when stdm.student_ethnicity in ('american indian') then 1 else 0 end) as [American indian],

sum(case when stdm.student_ethnicity in ('multiracial') then 1 else 0 end) as [Multiracial],

sum(case when stdm.student_ethnicity in ('white') then 1 else 0 end) as [White],

sum(case when stdm.student_gender in ('m') then 1 else 0 end) as [Male],

sum(case when stdm.student_gender in ('f') then 1 else 0 end) as [Female]

from

dw_student.dbo.student stdm

right join

(

select distinct

student_id,intv.intervention_id

from

wain.dbo.meeting mtng

inner join wain.dbo.meeting_intervention mtgi on mtng.meeting_id = mtgi.meeting_id

inner join wain.dbo.intervention intv on mtgi.intervention_id = intv.intervention_id

) subquery

on subquery.student_id = stdm.student_id

group by

stdm.school_region,

stdm.school_number,

stdm.school_name,

subquery.intervention_id,

stdm.student_ethnicity,

subquery.student_id,

stdm.student_gender

order by

stdm.school_region,

stdm.school_number,

stdm.school_name,

stdm.student_ethnicity,

subquery.intervention_id asc,

subquery.student_id,

stdm.student_gender

Count query

Hi

I had posted this question earlier but could not get the solution, may be i was not clear with the doubts i had

I need to count the no of students for the different Intervention field which are like 14 different types

against the field gender (male and Female )and field Ethnicity (horizontal field headers) 5 different types below

A student can be Male and Hispanic Type 1

also

Male Hispanic Type 2

Male Hispanic Type 3

So his count is made in three places

I need to do this

for the whole District level

then for each Center under District level

then for each school under Center level

DISTRICT SUMMARY

Intervention

Total

Male

Female

Asian

Black

Hispanic

Indian

Multiracial

White

TYPE : 1

7

3

4

4

2

0

0

0

1

TYPE :2

6

3

3

1

0

4

0

0

1

TYPE : 3

3

1

2

1

2

0

0

0

0

TYPE : 4

5

2

3

2

3

0

0

0

0

TYPE : 5

1

0

1

1

0

0

0

0

0

TYPE : 7

2

1

1

0

2

0

0

0

0

TYPE : 8

1

1

0

0

0

1

0

0

0

TYPE : 9

2

1

1

0

1

0

0

0

1

TYPE : 14

1

1

0

0

1

0

0

0

0

Please help

Thanks

What does your table(s) structure look like?

|||

Hi

I am joining these TWO tables based on student ID

The fields Ethnicity and field Gender both come from one table Table A

The field Intervention Types comes from Table B

Table B is

a group of three tables because the table that has intervention types does not have student_id

so I need to link it via a common field with another table

This below is how i am working on the code

select

SUBQUERY.student_id,
SUBQUERY.Intervention_ID,
STDM.student_ethnicity,
STDM.student_gender,


count( case when STDM.STUDENT_ETHNICITY in('Asian') then STDM.student_id else NULL end) as [Asian],

count( case when STDM.STUDENT_ETHNICITY in ('Black') then STDM.student_id else NULL end) as [Black],

count( case when STDM.STUDENT_ETHNICITY in('Hispanic') then STDM.student_id else NULL end) as [Hispanic],

count( case when STDM.STUDENT_ETHNICITY in('American Indian') then STDM.student_id else NULL end) as [American Indian],

count( case when STDM.STUDENT_ETHNICITY in('Multiracial') then STDM.student_id else NULL end) as [Multiracial],

count( case when STDM.STUDENT_ETHNICITY in('White') then STDM.student_id else NULL end) as [White],

count( case when STDM.STUDENT_GENDER in('M') then STDM.student_id else NULL end) as [Male],

count( case when STDM.STUDENT_GENDER in('F') then STDM.student_id else NULL end) as [Female]

from DW_student.dbo.Student STDM
right join
(select distinct
student_id,INTV.intervention_id from
WAIN.DBO.Meeting MTNG
inner join WAIN.DBO.Meeting_Intervention MTGI on MTNG.Meeting_ID = MTGI.Meeting_ID
inner join WAIN.DBO.Intervention INTV on MTGI.Intervention_ID = INTV.Intervention_ID
) SUBQUERY
on SUBQUERY.student_id = STDM.student_id

group by
STDM.SCHOOL_REGION,
STDM.SCHOOL_NUMBER,
STDM.SCHOOL_NAME,
SUBQUERY.Intervention_ID,
STDM.STUDENT_ETHNICITY,
SUBQUERY.student_id,
STDM.STUDENT_GENDER
order by
STDM.SCHOOL_REGION,
STDM.SCHOOL_NUMBER,
STDM.SCHOOL_NAME,
STDM.STUDENT_ETHNICITY,
SUBQUERY.Intervention_ID asc,
SUBQUERY.student_id,
STDM.STUDENT_GENDER


|||This looks like it would be a lot easier to do in a cube or pivot table. Where type is your dimension on rows and ethnicity is your dimension on columns, and count would be the measure.|||

Can you please explain it relating it to the present example

Thanks

Sowmya

|||

Your logic is motly used when PIVOTing the Columns. I didn't find any table design changes here. Your data is perfect.

You are almost on correct track – Your logic & intention of the expression almost perfect. But you need to change the expression slightly to achive your result,

- Use 0 as ELSE value on the Case When

- Use Sum instead of Count

Here the updated query,

select

subquery.student_id,

subquery.intervention_id,

stdm.student_ethnicity,

stdm.student_gender,

sum(case when stdm.student_ethnicity in ('asian') then 1 else 0 end) as [Asian],

sum(case when stdm.student_ethnicity in ('black') then 1 else 0 end) as [Black],

sum(case when stdm.student_ethnicity in ('hispanic') then 1 else 0 end) as [Hispanic],

sum(case when stdm.student_ethnicity in ('american indian') then 1 else 0 end) as [American indian],

sum(case when stdm.student_ethnicity in ('multiracial') then 1 else 0 end) as [Multiracial],

sum(case when stdm.student_ethnicity in ('white') then 1 else 0 end) as [White],

sum(case when stdm.student_gender in ('m') then 1 else 0 end) as [Male],

sum(case when stdm.student_gender in ('f') then 1 else 0 end) as [Female]

from

dw_student.dbo.student stdm

right join

(

select distinct

student_id,intv.intervention_id

from

wain.dbo.meeting mtng

inner join wain.dbo.meeting_intervention mtgi on mtng.meeting_id = mtgi.meeting_id

inner join wain.dbo.intervention intv on mtgi.intervention_id = intv.intervention_id

) subquery

on subquery.student_id = stdm.student_id

group by

stdm.school_region,

stdm.school_number,

stdm.school_name,

subquery.intervention_id,

stdm.student_ethnicity,

subquery.student_id,

stdm.student_gender

order by

stdm.school_region,

stdm.school_number,

stdm.school_name,

stdm.student_ethnicity,

subquery.intervention_id asc,

subquery.student_id,

stdm.student_gender

Count Query

hi! I am new to this whole SQL language...
had a question about using a count function or if i shuld even be using a count function for this...
Data I have to work with I would like my result set to look like this:
ID ID2 ID ID1Count ID2Count ID3Count ID4Count
A 1 A 2 2 2 3
A 1 B 1 0 3 1
A 2
A 3
A 2
A 4
A 4
A 4
A 3
B 3
B 4
B 1
B 3
B 3
I am stumped.... ANy help would be great... thanks!
nick

select ID,
sum(case when ID2 = 1 then 1 else 0 end) as ID1Count,
sum(case when ID2 = 2 then 1 else 0 end) as ID2Count,
sum(case when ID2 = 3 then 1 else 0 end) as ID3Count,
sum(case when ID2 = 4 then 1 else 0 end) as ID4Count,
from table1
group by ID|||Thanks for the direction! got it to work like a charm!