473,748 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

one query question

HI,

I was ask to do a query to get the member active condition

I had table to show the member number and active status in three
month.

ACC A1 A2 A3
---- ----------- ----------- -----------
c001 1 1 1
c002 1 1 1
c003 1 1 0
c004 1 1 0
c005 1 0 0
c006 1 0 1

the analyser request following data

Sample like:

Jan 6 active memeber

Feb 4 active member 2 inactive member

Mar (2 active memeber + 2 inactive memeber under Feb 60 active
member ) (1 active memeber+1 inactive memeber under Feb 40 inactive
member)

Is there any way to do that?

I could use similar query "select a1,a2,a3 ,count(acc) group by
a1,a2,a3 to get the each level info" but how to conbine each level
info into tree info?

Thanks.
Nov 12 '05 #1
7 2054
es**********@ya hoo.com (jane) wrote in message news:<db******* *************** ****@posting.go ogle.com>...
HI,

I was ask to do a query to get the member active condition

I had table to show the member number and active status in three
month.

ACC A1 A2 A3
---- ----------- ----------- -----------
c001 1 1 1
c002 1 1 1
c003 1 1 0
c004 1 1 0
c005 1 0 0
c006 1 0 1

the analyser request following data

Sample like:

Jan 6 active memeber

Feb 4 active member 2 inactive member

Mar (2 active memeber + 2 inactive memeber under Feb 60 active
member ) (1 active memeber+1 inactive memeber under Feb 40 inactive
member)

Is there any way to do that?

I could use similar query "select a1,a2,a3 ,count(acc) group by
a1,a2,a3 to get the each level info" but how to conbine each level
info into tree info?

Thanks.


I think maybe I should explain my question more clearly.
example:
If there are 6 active at Jan. 2003 , 4 of 6-Jan-A are active at Feb, 2
of 6-Jan-A are not active at Feb. and 3 of 4-Feb-A are active at Mar,
1 of 4-Feb-A is not active at Mar. 2 of 2-Feb-NA is active in Mar.

So, the table is like

mem_no Jan Feb Mar
c001 1 1 1
c002 1 1 1
c003 1 1 1
c004 1 1 0
c005 1 0 1
c006 1 0 1
I hope get following three layer diagram like a b-tree. Active at
left, noactive at right.

--Jan 6

--Feb 4 2
--Mar 3 1 2 0

any suggestion is so appreciated.

Thanks.
Nov 12 '05 #2
> So, the table is like

mem_no Jan Feb Mar
c001 1 1 1
c002 1 1 1
c003 1 1 1
c004 1 1 0
c005 1 0 1
c006 1 0 1
I hope get following three layer diagram like a b-tree. Active at
left, noactive at right.

--Jan 6

--Feb 4 2
--Mar 3 1 2 0

------------------------------ Command Entered ------------------------------
SELECT Month_Name , Layer_Diagram
FROM (
SELECT 'Jan' , ' ' || CHAR(SMALLINT(S UM( Jan )))
|| ' ' || CHAR(SMALLINT(S UM( 1-Jan )))
FROM MemberActiveSta tus
UNION ALL
SELECT 'Feb' , ' ' || CHAR(SMALLINT(S UM( Jan * Feb )))
|| ' ' || CHAR(SMALLINT(S UM( Jan * (1-Feb) )))
|| ' ' || CHAR(SMALLINT(S UM( (1-Jan) * Feb )))
|| ' ' || CHAR(SMALLINT(S UM( (1-Jan) * (1-Feb) )))
FROM MemberActiveSta tus
UNION ALL
SELECT 'Mar' , CHAR(SMALLINT(S UM( Jan * Feb * Mar )))
|| ' ' || CHAR(SMALLINT(S UM( Jan * Feb * (1-Mar) )))
|| ' ' || CHAR(SMALLINT(S UM( Jan * (1-Feb) * Mar )))
|| ' ' || CHAR(SMALLINT(S UM( Jan * (1-Feb) * (1-Mar) )))
|| ' ' || CHAR(SMALLINT(S UM( (1-Jan) * Feb * Mar )))
|| ' ' || CHAR(SMALLINT(S UM( (1-Jan) * Feb * (1-Mar) )))
|| ' ' || CHAR(SMALLINT(S UM( (1-Jan) * (1-Feb) * Mar )))
|| ' ' || CHAR(SMALLINT(S UM( (1-Jan) * (1-Feb) * (1-Mar) )))
FROM MemberActiveSta tus
) X( Month_Name , Layer_Diagram)
ORDER BY LENGTH(Layer_Di agram)
;
-----------------------------------------------------------------------------

MONTH_NAME LAYER_DIAGRAM
---------- -------------------------------------------------------
Jan 6 0
Feb 4 2 0 0
Mar 3 1 2 0 0 0 0 0

3 record(s) selected.
Nov 12 '05 #3
to*****@jp.ibm. com (Tokunaga T.) wrote in message news:<81******* *************** ****@posting.go ogle.com>...
So, the table is like

mem_no Jan Feb Mar
c001 1 1 1
c002 1 1 1
c003 1 1 1
c004 1 1 0
c005 1 0 1
c006 1 0 1
I hope get following three layer diagram like a b-tree. Active at
left, noactive at right.

--Jan 6

--Feb 4 2
--Mar 3 1 2 0

------------------------------ Command Entered ------------------------------
SELECT Month_Name , Layer_Diagram
FROM (
SELECT 'Jan' , ' ' || CHAR(SMALLINT(S UM( Jan )))
|| ' ' || CHAR(SMALLINT(S UM( 1-Jan )))
FROM MemberActiveSta tus
UNION ALL
SELECT 'Feb' , ' ' || CHAR(SMALLINT(S UM( Jan * Feb )))
|| ' ' || CHAR(SMALLINT(S UM( Jan * (1-Feb) )))
|| ' ' || CHAR(SMALLINT(S UM( (1-Jan) * Feb )))
|| ' ' || CHAR(SMALLINT(S UM( (1-Jan) * (1-Feb) )))
FROM MemberActiveSta tus
UNION ALL
SELECT 'Mar' , CHAR(SMALLINT(S UM( Jan * Feb * Mar )))
|| ' ' || CHAR(SMALLINT(S UM( Jan * Feb * (1-Mar) )))
|| ' ' || CHAR(SMALLINT(S UM( Jan * (1-Feb) * Mar )))
|| ' ' || CHAR(SMALLINT(S UM( Jan * (1-Feb) * (1-Mar) )))
|| ' ' || CHAR(SMALLINT(S UM( (1-Jan) * Feb * Mar )))
|| ' ' || CHAR(SMALLINT(S UM( (1-Jan) * Feb * (1-Mar) )))
|| ' ' || CHAR(SMALLINT(S UM( (1-Jan) * (1-Feb) * Mar )))
|| ' ' || CHAR(SMALLINT(S UM( (1-Jan) * (1-Feb) * (1-Mar) )))
FROM MemberActiveSta tus
) X( Month_Name , Layer_Diagram)
ORDER BY LENGTH(Layer_Di agram)
;
-----------------------------------------------------------------------------

MONTH_NAME LAYER_DIAGRAM
---------- -------------------------------------------------------
Jan 6 0
Feb 4 2 0 0
Mar 3 1 2 0 0 0 0 0

3 record(s) selected.


HI,

Thanks so much for your input.

I made a little change on your query

SELECT Month_Name , Layer_Diagram
FROM (
SELECT 'Jan' , ' ' || CHAR(SMALLINT(S UM( Jan
)))
FROM test_e
UNION ALL
SELECT 'Feb' , ' ' || CHAR(SMALLINT(S UM( Jan * Feb
)))
|| ' ' || CHAR(SMALLINT(S UM( Jan * (1-Feb)
)))
FROM test_e
UNION ALL
SELECT 'Mar' , CHAR(SMALLINT(S UM( Jan * Feb * Mar
)))
|| ' ' || CHAR(SMALLINT(S UM( Jan * Feb *
(1-Mar) )))
|| ' ' || CHAR(SMALLINT(S UM( Jan * (1-Feb) * Mar
)))
|| ' ' || CHAR(SMALLINT(S UM( Jan * (1-Feb) *
(1-Mar) )))
FROM test_e
) X( Month_Name , Layer_Diagram)
ORDER BY LENGTH(Layer_Di agram)
;

I got the exactly result I want.

Another questions is it is too much edit work if I want to have 6 or
more layer. It is terrible to do the edit work and format the result.
Nov 12 '05 #4
> Another questions is it is too much edit work if I want to have 6 or
more layer. It is terrible to do the edit work and format the result.

How about this?
------------------------------ Command Entered ------------------------------
SELECT C.Jan , C.Feb , C.Mar
, COUNT(mem_no) Counts
FROM (VALUES (1,1,1) , (1,1,0) , (1,0,1) , (1,0,0)
) AS C (Jan, Feb, Mar)
LEFT OUTER JOIN
MemberActiveSta tus AS M
ON C.Jan = M.Jan
AND C.Feb = M.Feb
AND C.Mar = M.Mar
GROUP BY GROUPING SETS ( (C.Jan)
, (C.Jan, C.Feb)
, (C.Jan, C.Feb, C.Mar) )
ORDER BY C.Jan desc , C.Feb desc , C.Mar desc
;
-----------------------------------------------------------------------------

JAN FEB MAR COUNTS
----------- ----------- ----------- -----------
1 - - 6
1 1 - 4
1 1 1 3
1 1 0 1
1 0 - 2
1 0 1 2
1 0 0 0

7 record(s) selected.
Nov 12 '05 #5
For less than 6 layers:
(The sample is for 3 layers. Modify ON conditions and add GROUP BY
columns and ORDER BY columns, for 4, 5 and 6 layers.)
------------------------------ Command Entered
------------------------------
SELECT C.Jan , C.Feb , C.Mar
, COUNT(mem_no) Counts
FROM (VALUES
(1,1,1,1,1,1), (1,1,1,1,1,0), (1,1,1,1,0,1),
(1,1,1,1,0,0)
, (1,1,1,0,1,1), (1,1,1,0,1,0), (1,1,1,0,0,1),
(1,1,1,0,0,0)
, (1,1,0,1,1,1), (1,1,0,1,1,0), (1,1,0,1,0,1),
(1,1,0,1,0,0)
, (1,1,0,0,1,1), (1,1,0,0,1,0), (1,1,0,0,0,1),
(1,1,0,0,0,0)
, (1,0,1,1,1,1), (1,0,1,1,1,0), (1,0,1,1,0,1),
(1,0,1,1,0,0)
, (1,0,1,0,1,1), (1,0,1,0,1,0), (1,0,1,0,0,1),
(1,0,1,0,0,0)
, (1,0,0,1,1,1), (1,0,0,1,1,0), (1,0,0,1,0,1),
(1,0,0,1,0,0)
, (1,0,0,0,1,1), (1,0,0,0,1,0), (1,0,0,0,0,1),
(1,0,0,0,0,0)
) AS C (Jan, Feb, Mar, Apr, May, Jun)
LEFT OUTER JOIN
MemberActiveSta tus AS M
ON C.Jan = M.Jan
AND C.Feb = M.Feb
AND C.Mar = M.Mar
AND C.Apr = 0
AND C.May = 0
AND C.Jun = 0
GROUP BY GROUPING SETS ( (C.Jan)
, (C.Jan, C.Feb)
, (C.Jan, C.Feb, C.Mar) )
ORDER BY C.Jan desc , C.Feb desc , C.Mar desc
;
-----------------------------------------------------------------------------

JAN FEB MAR COUNTS
----------- ----------- ----------- -----------
1 - - 6
1 1 - 4
1 1 1 3
1 1 0 1
1 0 - 2
1 0 1 2
1 0 0 0

7 record(s) selected.

More general sample(for less than 12 layers):
------------------------------ Command Entered
------------------------------
WITH C (seq, m1 , m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12) AS (
VALUES ( 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
UNION ALL
SELECT seq + 1
, CASE WHEN MOD(seq , 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 2, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 4, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 8, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 16, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 32, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 64, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 128, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 256, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 512, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/1024, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/2048, 2) = 1 THEN 1 ELSE 0 END
FROM C
WHERE seq < 8
-- Adjust to 2 to the power of layer.
)
SELECT m1 , m2 , m3
-- , m4 , m5 , m6
-- , m7 , m8 , m9
-- , m10 , m11 , m12
, COUNT(mem_no) Counts
FROM C
LEFT OUTER JOIN
MemberActiveSta tus AS M
ON C.m1 = M.Jan
AND C.m2 = M.Feb
AND C.m3 = M.Mar
-- AND C.m4 = M.Apr
-- AND C.m5 = M.May
-- AND C.m6 = M.Jun
-- AND C.m7 = M.Jul
-- AND C.m8 = M.Aug
-- AND C.m9 = M.Sep
-- AND C.m10 = M.Oct
-- AND C.m11 = M.Nov
-- AND C.m12 = M.Dec
WHERE C.m1 = 1
GROUP BY GROUPING SETS
(
(m1)
, (m1, m2)
, (m1, m2, m3)
-- , (m1, m2, m3, m4)
-- , (m1, m2, m3, m4, m5)
-- , (m1, m2, m3, m4, m5, m6)
-- , (m1, m2, m3, m4, m5, m6, m7)
-- , (m1, m2, m3, m4, m5, m6, m7, m8)
-- , (m1, m2, m3, m4, m5, m6, m7, m8, m9)
-- , (m1, m2, m3, m4, m5, m6, m7, m8, m9, m10)
-- , (m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11)
-- , (m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12)
)
ORDER BY
m1 desc , m2 desc , m3 desc
-- , m4 desc , m5 desc , m6 desc
-- , m7 desc , m8 desc , m9 desc
-- , m10 desc , m11 desc , m12 desc
;
-----------------------------------------------------------------------------

M1 M2 M3 COUNTS
----------- ----------- ----------- -----------
1 - - 6
1 1 - 4
1 1 1 3
1 1 0 1
1 0 - 2
1 0 1 2
1 0 0 0

7 record(s) selected.
Nov 12 '05 #6
to*****@jp.ibm. com (Tokunaga T.) wrote in message news:<81******* *************** ****@posting.go ogle.com>...
For less than 6 layers:
(The sample is for 3 layers. Modify ON conditions and add GROUP BY
columns and ORDER BY columns, for 4, 5 and 6 layers.)
------------------------------ Command Entered
------------------------------
SELECT C.Jan , C.Feb , C.Mar
, COUNT(mem_no) Counts
FROM (VALUES
(1,1,1,1,1,1), (1,1,1,1,1,0), (1,1,1,1,0,1),
(1,1,1,1,0,0)
, (1,1,1,0,1,1), (1,1,1,0,1,0), (1,1,1,0,0,1),
(1,1,1,0,0,0)
, (1,1,0,1,1,1), (1,1,0,1,1,0), (1,1,0,1,0,1),
(1,1,0,1,0,0)
, (1,1,0,0,1,1), (1,1,0,0,1,0), (1,1,0,0,0,1),
(1,1,0,0,0,0)
, (1,0,1,1,1,1), (1,0,1,1,1,0), (1,0,1,1,0,1),
(1,0,1,1,0,0)
, (1,0,1,0,1,1), (1,0,1,0,1,0), (1,0,1,0,0,1),
(1,0,1,0,0,0)
, (1,0,0,1,1,1), (1,0,0,1,1,0), (1,0,0,1,0,1),
(1,0,0,1,0,0)
, (1,0,0,0,1,1), (1,0,0,0,1,0), (1,0,0,0,0,1),
(1,0,0,0,0,0)
) AS C (Jan, Feb, Mar, Apr, May, Jun)
LEFT OUTER JOIN
MemberActiveSta tus AS M
ON C.Jan = M.Jan
AND C.Feb = M.Feb
AND C.Mar = M.Mar
AND C.Apr = 0
AND C.May = 0
AND C.Jun = 0
GROUP BY GROUPING SETS ( (C.Jan)
, (C.Jan, C.Feb)
, (C.Jan, C.Feb, C.Mar) )
ORDER BY C.Jan desc , C.Feb desc , C.Mar desc
;
-----------------------------------------------------------------------------

JAN FEB MAR COUNTS
----------- ----------- ----------- -----------
1 - - 6
1 1 - 4
1 1 1 3
1 1 0 1
1 0 - 2
1 0 1 2
1 0 0 0

7 record(s) selected.

More general sample(for less than 12 layers):
------------------------------ Command Entered
------------------------------
WITH C (seq, m1 , m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12) AS (
VALUES ( 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
UNION ALL
SELECT seq + 1
, CASE WHEN MOD(seq , 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 2, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 4, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 8, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 16, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 32, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 64, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 128, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 256, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 512, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/1024, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/2048, 2) = 1 THEN 1 ELSE 0 END
FROM C
WHERE seq < 8
-- Adjust to 2 to the power of layer.
)
SELECT m1 , m2 , m3
-- , m4 , m5 , m6
-- , m7 , m8 , m9
-- , m10 , m11 , m12
, COUNT(mem_no) Counts
FROM C
LEFT OUTER JOIN
MemberActiveSta tus AS M
ON C.m1 = M.Jan
AND C.m2 = M.Feb
AND C.m3 = M.Mar
-- AND C.m4 = M.Apr
-- AND C.m5 = M.May
-- AND C.m6 = M.Jun
-- AND C.m7 = M.Jul
-- AND C.m8 = M.Aug
-- AND C.m9 = M.Sep
-- AND C.m10 = M.Oct
-- AND C.m11 = M.Nov
-- AND C.m12 = M.Dec
WHERE C.m1 = 1
GROUP BY GROUPING SETS
(
(m1)
, (m1, m2)
, (m1, m2, m3)
-- , (m1, m2, m3, m4)
-- , (m1, m2, m3, m4, m5)
-- , (m1, m2, m3, m4, m5, m6)
-- , (m1, m2, m3, m4, m5, m6, m7)
-- , (m1, m2, m3, m4, m5, m6, m7, m8)
-- , (m1, m2, m3, m4, m5, m6, m7, m8, m9)
-- , (m1, m2, m3, m4, m5, m6, m7, m8, m9, m10)
-- , (m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11)
-- , (m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12)
)
ORDER BY
m1 desc , m2 desc , m3 desc
-- , m4 desc , m5 desc , m6 desc
-- , m7 desc , m8 desc , m9 desc
-- , m10 desc , m11 desc , m12 desc
;
-----------------------------------------------------------------------------

M1 M2 M3 COUNTS
----------- ----------- ----------- -----------
1 - - 6
1 1 - 4
1 1 1 3
1 1 0 1
1 0 - 2
1 0 1 2
1 0 0 0

7 record(s) selected.


Thanks for your very good suggestion.

But I still prefer the your first post query output(it's exactly
format I want). It is rather clear for our business guys to fill in
the data into their diagram.

a little suggestion, can we use the rollup instead of grouping set?

Also I used following query to get each layer data like following

select Jan,count(mem_n o)
from MemberActiveSta tus
group by Jan desc
JAN 2
----------- -----------
1 6
select Jan, Feb,count(mem_n o)
from MemberActiveSta tus
group by Jan desc, Feb desc.

JAN FEB 3
----------- ----------- -----------
1 1 4
1 0 2
select Jan, Feb, Mar,count(mem_n o)
from MemberActiveSta tus
group by Jan desc, Feb desc, Mar desc.
JAN FEB MAR 4
----------- ----------- ----------- -----------
1 1 1 3
1 1 0 1
1 0 1 2
But I just think how I could put the last output column of each query
together
like following output format:

6
4 2
3 1 2 0

I could put the last output column of each query into one table , then
query from this table. But is there any simple way?
Nov 12 '05 #7
> But I still prefer the your first post query output(it's exactly
format I want). It is rather clear for our business guys to fill in
the data into their diagram.

a little suggestion, can we use the rollup instead of grouping set? The reason I used GROUPING SETS in previous example is if I used
ROLLUP, the query result will include additional row corresponding to
GROUPING SETS ().
Also I used following query to get each layer data like following The reason I JOINed with table expression C is to include rows
corresponding to all combination of values of Jan, Feb and Mar, even
if counts of mem_no is 0.
select Jan,count(mem_n o)
from MemberActiveSta tus
group by Jan desc
JAN 2
----------- -----------
1 6
select Jan, Feb,count(mem_n o)
from MemberActiveSta tus
group by Jan desc, Feb desc.

JAN FEB 3
----------- ----------- -----------
1 1 4
1 0 2
select Jan, Feb, Mar,count(mem_n o)
from MemberActiveSta tus
group by Jan desc, Feb desc, Mar desc.
JAN FEB MAR 4
----------- ----------- ----------- -----------
1 1 1 3
1 1 0 1
1 0 1 2
By JOINning with table expression C, following additional row will be
included.
1 0 0 0
But I just think how I could put the last output column of each query
together
like following output format:

6
4 2
3 1 2 0

I could put the last output column of each query into one table , then
query from this table. But is there any simple way?

I afraid you think following example is too complex. But at least, it
will work.
Note:
You need only rows Jan = 1. So I removed m1 from table expression C.
Then I could use ROLLUP.
------------------------------ Command Entered
------------------------------
WITH
C (seq, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12) AS (
VALUES ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
UNION ALL
SELECT seq + 1
, CASE WHEN MOD(seq , 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 2, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 4, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 8, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 16, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 32, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 64, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 128, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 256, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/ 512, 2) = 1 THEN 1 ELSE 0 END
, CASE WHEN MOD(seq/1024, 2) = 1 THEN 1 ELSE 0 END
FROM C
WHERE seq < 4
-- Adjust to 2 to the power of (layer-1).
)
,
M AS (
SELECT m2 , m3
-- , m4 , m5
-- , m6 , m7 , m8 , m9 , m10 , m11 , m12
, COUNT(mem_no) Counts
, GROUPING(m2)
+GROUPING(m3)
-- +GROUPING(m4)
-- +GROUPING(m5)
-- +GROUPING(m6)
-- +GROUPING(m7)
-- +GROUPING(m8)
-- +GROUPING(m9)
-- +GROUPING(m10)
-- +GROUPING(m11)
-- +GROUPING(m12)
AS layer
FROM C
LEFT OUTER JOIN
MemberActiveSta tus AS M
ON M.Jan = 1
AND C.m2 = M.Feb
AND C.m3 = M.Mar
-- AND C.m4 = M.Apr
-- AND C.m5 = M.May
-- AND C.m6 = M.Jun
-- AND C.m7 = M.Jul
-- AND C.m8 = M.Aug
-- AND C.m9 = M.Sep
-- AND C.m10 = M.Oct
-- AND C.m11 = M.Nov
-- AND C.m12 = M.Dec
GROUP BY ROLLUP
(m2 , m3
-- , m4 , m5
-- , m6 , m7 , m8 , m9 , m10 , m11 , m12
)
)
,
Ord AS (
SELECT M.*
, ROWNUMBER()
OVER(PARTITION BY layer
ORDER BY m2 desc , m3 desc
-- , m4 desc , m5 desc
-- , m6 desc , m7 desc , m8 desc , m9 desc , m10
desc , m11 desc , m12 desc
) AS rnum
FROM M
)
,
Cat (rnum , layer , cat_counts) AS (
SELECT 1 , layer
, CAST( REPEAT(' ', 6*POWER(2,layer ) ) || CHAR(SMALLINT(c ounts))
AS VARCHAR(48) )
FROM Ord
WHERE rnum = 1
UNION ALL
SELECT Pre.rnum + 1 , Pre.layer
, cat_counts || REPEAT(' ', 6*(POWER(2,Pre. layer+1)-1) ) ||
CHAR(SMALLINT(c ounts))
FROM Cat Pre
, Ord New
WHERE Pre.rnum < 2048
AND Pre.layer = New.layer
AND New.rnum = Pre.rnum + 1
)

SELECT (SELECT MAX(layer) + 1 FROM Cat) - layer AS month
, cat_counts
FROM Cat R
WHERE rnum = (SELECT MAX(rnum)
FROM Cat S
WHERE R.layer = S.layer
)
;
-----------------------------------------------------------------------------

MONTH CAT_COUNTS
----------- ------------------------------------------------
1 6
2 4 2
3 3 1 2 0

3 record(s) selected.
Nov 12 '05 #8

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

Similar topics

9
3405
by: majsen | last post by:
Hi, I have problem running this query. It will time out for me... My database are small just about 200 members. I have a site for swaping appartments (rental). my query should look for match in a triangle. Like this member A -> B->C A give his appartment to B. B gives his appartment to C and finally C gives his appartment to A Soo my query looks for matching parameters like rooms, location, size
8
3270
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run simultaneously 4 similar queries it takes nearly 5 minutes instead of 4 times 9 seconds or something near of that. here is a sample query: select mertido, fomeazon, ertektipus, mertertek from t_me30 where fomeazon in (select distinct fomeazon...
3
14127
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record (and store it in field 2). > > I would like to run a query that returns all the data in the table plus the > record number, in a similar sense to the getuser() command to get the User's > Identity, I would like a GetRecord() command.
3
3089
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table before rowID answID qryrow questionID datafield 1591 12 06e 06e 06e question 1593 12 06f 06f 06f question 1594 12 answer to the question 06f
7
3391
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always either AND or OR but never mixed together. We can use Northwind database for my question, it is very similar to the structure of the problem on the database I am working on. IF(SELECT OBJECT_ID('REPORT')) IS NOT NULL DROP TABLE REPORT_SELECTION
6
4846
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID SalesManName AT Alan Time
2
2040
by: mmitchell_houston | last post by:
I'm working on a .NET project and I need a single query to return a result set from three related tables in Access 2003, and I'm having trouble getting the results I want. The details: Question ------------ QuestionID QuestionText Question_MediaTypeID
22
31208
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for June, and will return all records in that month.
3
2063
by: Richard Hollenbeck | last post by:
I am very sorry about the (almost) re-post, but you will see that my first question wasn't very clear; I have another question I posted this morning called, "in DAO: Run time error 3061 Too few parameters...." I have read many articles on the web about how to make a dynamic report based on a cross-tab query. But for some reason mine never works right. First, my saved query's criteria is the data in an open form's combo box. So the...
16
3512
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for renaming the duplicate records? My thinking was to take the results of the duplicate query, and somehow have it number each line where there is a duplicate (tried a groups query, but "count" won't work), then do an update query to change the duplicate to...
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
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 we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.