Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Possible with 1 query to get no. of appearance?

Author  Topic 

850t
Starting Member

1 Post

Posted - 2012-07-23 : 03:00:13
hi all,

I have a question related to a table that I'm trying to query.

Table A
---------------------
| Col A | Col B |
---------------------
| Y | 1 |
| Y | 1 |
| Y | 2 |
| Y | 3 |
| Y | 4 |
| Y | 4 |
| Y | 4 |
| N | 1 |
| N | 1 |
| N | 1 |
| N | 2 |
| N | 2 |
---------------------

I would like the output to be

--------------------------------------------------
| Col A | Num of Col A | Col B | Num of Col B |
--------------------------------------------------
| Y | 7 | 1 | 2 |
| Y | 7 | 2 | 1 |
| Y | 7 | 3 | 1 |
| Y | 7 | 4 | 3 |
| N | 5 | 1 | 3 |
| N | 5 | 2 | 2 |
--------------------------------------------------

What's the query to do that?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-07-23 : 05:31:43
Please test this:

-- create a sample table variable
declare @Sample table( A char(1), B char(1))

-- insert sample data
insert @Sample
select 'Y', '1' union all
select 'Y', '1' union all
select 'Y', '2' union all
select 'Y', '3' union all
select 'Y', '4' union all
select 'Y', '4' union all
select 'Y', '4' union all
select 'N', '1' union all
select 'N', '1' union all
select 'N', '1' union all
select 'N', '2' union all
select 'N', '2'

--I would like the output to be

----------------------------------------------------
--| Col A | Num of Col A | Col B | Num of Col B |
----------------------------------------------------
--| Y | 7 | 1 | 2 |
--| Y | 7 | 2 | 1 |
--| Y | 7 | 3 | 1 |
--| Y | 7 | 4 | 3 |
--| N | 5 | 1 | 3 |
--| N | 5 | 2 | 2 |
----------------------------------------------------

-- try a solution using derived tables
select
dt1.A,
dt1.NumOfColA,
dt2.B,
dt2.NumOfColB
from
(
select
A,
count(*) as NumOfColA
from @Sample
group by A
)dt1
left join
(
select
A,
B,
count(*) as NumOfColB
from @Sample
group by A,B
)dt2
on dt1.A = dt2.A

-- result
A NumOfColA B NumOfColB
---- ----------- ---- -----------
N 5 1 3
N 5 2 2
Y 7 1 2
Y 7 2 1
Y 7 3 1
Y 7 4 3

(6 row(s) affected)



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-23 : 10:22:33
quote:
Originally posted by 850t

hi all,

I have a question related to a table that I'm trying to query.

Table A
---------------------
| Col A | Col B |
---------------------
| Y | 1 |
| Y | 1 |
| Y | 2 |
| Y | 3 |
| Y | 4 |
| Y | 4 |
| Y | 4 |
| N | 1 |
| N | 1 |
| N | 1 |
| N | 2 |
| N | 2 |
---------------------

I would like the output to be

--------------------------------------------------
| Col A | Num of Col A | Col B | Num of Col B |
--------------------------------------------------
| Y | 7 | 1 | 2 |
| Y | 7 | 2 | 1 |
| Y | 7 | 3 | 1 |
| Y | 7 | 4 | 3 |
| N | 5 | 1 | 3 |
| N | 5 | 2 | 2 |
--------------------------------------------------

What's the query to do that?




SELECT ColA,
Occ AS [Num of ColA],
ColB,
COUNT(*) AS [Num of ColB]]
FROM
(
SELECT COUNT(1) OVER (PARTITION BY ColA) AS Occ,ColA,ColB
FROM table
)t
GROUP BY ColA,Occ,ColB


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -