473,473 Members | 4,189 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pivot table sql -- COUNT() not working as expected

hi.
I am using a pseudo "pivot table" SQL query to generate a report but I
am totally stumped on why the COUNT() function isn't getting me the
expected results and was wondering if anyone has some ideas...

it is for a statistical report showing which lead source a member
followed when creating a profile (i.e. CNN ad, Chicago Tribune ad,
etc), grouped by age range.

here is the SQL that should total up rows based each specific
condition, but doesn't:

SELECT ms.display_name AS lead_source, EXTRACT(YEAR FROM
AGE(pd.birth_date)) AS age_range,
COUNT(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-03-14' THEN
p.profile_id ELSE 0 END) AS "Total 03/07/04",
COUNT(CASE WHEN p.added_on BETWEEN '2004-03-14' AND '2004-03-21' THEN
p.profile_id ELSE 0 END) AS "Total 03/14/04",
COUNT(CASE WHEN p.added_on BETWEEN '2004-03-21' AND '2004-03-28' THEN
p.profile_id ELSE 0 END) AS "Total 03/21/04",
COUNT(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-06-27' THEN
p.profile_id ELSE 0 END) AS "Total 03/07/2004 - 06/27/2004",
COUNT(p.profile_id) as "Total "
FROM ss_profile p, ss_profile_detail pd, ss_profile_multi_select s,
ss_multi_select ms
WHERE p.profile_id = pd.profile_id AND s.profile_id = p.profile_id AND
s.multi_select_id = ms.multi_select_id AND ms.selection_type =
'how_did_you_hear'
GROUP BY ms.display_name, age_range
;

the output of the query is this:
http://farmdev.com/test-report-w-count.txt
(you will prob need to paste that into a fix-width font to see it
properly)

the numbers are all wrong... they are the same for each column for
some reason across the board but I don't know why.

As a workaround I created a column called "counter", which will always
have the value "1" and did a SUM of that to mimic COUNT .... and it
works fine! so what is the problem with COUNT? here is the workaround
SQL:

SELECT ms.display_name AS lead_source, EXTRACT(YEAR FROM
AGE(pd.birth_date)) AS age_range,
SUM(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-03-14' THEN
p.counter ELSE 0 END) AS "Total 03/07/04",
SUM(CASE WHEN p.added_on BETWEEN '2004-03-14' AND '2004-03-21' THEN
p.counter ELSE 0 END) AS "Total 03/14/04",
SUM(CASE WHEN p.added_on BETWEEN '2004-03-21' AND '2004-03-28' THEN
p.counter ELSE 0 END) AS "Total 03/21/04",
SUM(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-06-27' THEN
p.counter ELSE 0 END) AS "Total 03/07/2004 - 06/27/2004",
SUM(p.counter) as "Total "
FROM ss_profile p, ss_profile_detail pd, ss_profile_multi_select s,
ss_multi_select ms
WHERE p.profile_id = pd.profile_id AND s.profile_id = p.profile_id AND
s.multi_select_id = ms.multi_select_id AND ms.selection_type =
'how_did_you_hear'
GROUP BY ms.display_name, age_range
;

and here is how that report looks, which shows the correct numbers:
http://farmdev.com/test-report-w-sum.txt

... ok... the limited date range and limited lead sources isn't the
best for example purposes but this should give an idea of what I'm
trying to accomplish. also, to avoid confusion, "age range", actually
does get compressed in the PHP script so it looks more like 18-21 ...
number.
thanks in advance,
Kumar
Nov 23 '05 #1
2 6986
В Вск, 20.06.2004, в 17:44, kumar mcmillan пишет:
hi.
I am using a pseudo "pivot table" SQL query to generate a report butI
am totally stumped on why the COUNT() function isn't getting me
theexpected results and was wondering if anyone has some ideas...

it is for a statistical report showing which lead source a
memberfollowed when creating a profile (i.e. CNN ad, Chicago Tribune
ad,etc), grouped by age range.

here is the SQL that should total up rows based each
specificcondition, but doesn't:

SELECT ms.display_name AS lead_source, EXTRACT(YEAR
FROMAGE(pd.birth_date)) AS age_range,
COUNT(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-03-14'
THENp.profile_id ELSE 0 END) AS "Total 03/07/04",


You want

COUNT(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-03-14' THEN p.profile_id ELSE NULL END) AS "Total 03/07/04"

or

COUNT(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-03-14' THEN 1 ELSE NULL END) AS "Total 03/07/04"

or

COUNT(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-03-14' THEN 42 ELSE NULL END) AS "Total 03/07/04"

or

COUNT(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-03-14' THEN true ELSE NULL END) AS "Total 03/07/04"

which is all the same.

--
Markus Bertheau <tw*****@bluetwanger.de>
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #2
В Вск, 20.06.2004, в 17:44, kumar mcmillan пишет:
hi.
I am using a pseudo "pivot table" SQL query to generate a report butI
am totally stumped on why the COUNT() function isn't getting me
theexpected results and was wondering if anyone has some ideas...

it is for a statistical report showing which lead source a
memberfollowed when creating a profile (i.e. CNN ad, Chicago Tribune
ad,etc), grouped by age range.

here is the SQL that should total up rows based each
specificcondition, but doesn't:

SELECT ms.display_name AS lead_source, EXTRACT(YEAR
FROMAGE(pd.birth_date)) AS age_range,
COUNT(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-03-14'
THENp.profile_id ELSE 0 END) AS "Total 03/07/04",


You want

COUNT(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-03-14' THEN p.profile_id ELSE NULL END) AS "Total 03/07/04"

or

COUNT(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-03-14' THEN 1 ELSE NULL END) AS "Total 03/07/04"

or

COUNT(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-03-14' THEN 42 ELSE NULL END) AS "Total 03/07/04"

or

COUNT(CASE WHEN p.added_on BETWEEN '2004-03-07' AND '2004-03-14' THEN true ELSE NULL END) AS "Total 03/07/04"

which is all the same.

--
Markus Bertheau <tw*****@bluetwanger.de>
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim_k | last post by:
Does anyone have an example of how to export a datagrid to an Excel pivot table? The code below exports the grid contents to Excel using the Response object. I'd like to expand it to show the...
0
by: kumar mcmillan | last post by:
hi. I am using a pseudo "pivot table" SQL query to generate a report but I am totally stumped on why the COUNT() function isn't getting me the expected results and was wondering if anyone has some...
1
by: kingster | last post by:
Hi, I have a regular dataset and all i want to do is make a pivot table display in a browser with the datasource of the pivot table to be this dataset and then the end-user will be able to do...
3
by: Gert v O | last post by:
Can someone help me parsing this ms-access PIVOT sql-statement to a ms-sql-server sql-statement? Many thanks in advance TRANSFORM Count(KlantenStops.id) AS AantalVanid SELECT...
5
by: JayDawg | last post by:
Is there any way to create a pivot table in a report? I have a query with the fields Date, Name, and Data, and I am trying to create a report that has the dates running across the top (like...
1
by: clickon | last post by:
Does anyone know of a site outlining a good method of implementing a Pivot table style grid using aGridView control or similar?
9
by: PeteCresswell | last post by:
I've got something called "Reference Rates". The idea is that on a given day, we have various rates of return for various entities. e.g. Libor 3-month return, Libor 6-month return, US Treasury...
1
benchpolo
by: benchpolo | last post by:
I have data extracted from Access db to Excel with a pivot table. Somehow, I am having issues with the pivot table were it doesnt update the totals. For example, the first extract i did in Access...
0
by: Clare CAVS | last post by:
I have a table with a lookup column referring to another table . tblRooms has two fields, (Autonumber), and . The column I want to display is the RoomName column. If I have Bound Column = 1,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.