Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have 2 table with data like this

Table1:

ID IP Account
1 172.22.22 SSIN
2 172.34.43 SSIN
3 1832.2.2.3 JJIN

Table2:

ID Account AccountName Flag Frequency
1 SSIN Healthy 1 15
2 SSIN Good 1 15
3 SSIN bad 1 15
4 JJIN Healthy 1 15
5 JJIN Good 1 15
6 JJIN bad 0 15

I want the output like


OutPut:

Account IP AccountName
SSIN 2 3
JJIN 1 2

Here the ip is the count of the account, and the accountname is also the count of account and checking with flag=1
Posted
Updated 14-Nov-11 23:57pm
v2
Comments
Shabana Parveen 15-Nov-11 6:01am    
What exactly you want, SQL/LINQ query to display the view?
inayat basha 15-Nov-11 6:02am    
sql query to display
Shabana Parveen 15-Nov-11 6:05am    
Are you sure JJIN 1 2 not JJIN 3 2?
inayat basha 15-Nov-11 6:07am    
JJIN count is 1 from table1 and 2 from Table2
Shabana Parveen 15-Nov-11 6:09am    
oops...

Hi, We can Get that output from his join query,



SQL
SELECT   distinct   table2.Account,count(distinct(table1.IP)) as IP,  count(distinct(table2.AccountName)) as AccountName
FROM         table2 inner JOIN
                      table1 ON table1.Account = table2.Account where table2.Flag=1
group by   table2.Account ORDER BY table2.Account DESC



Regards,
Pal
 
Share this answer
 
Try this

SQL
select t1.Account, t1.IP, t2.AccountName from
(select count(*) IP,Account from Table1 group by Account)t1
Inner Join
(select count(*) AccountName, Account from Table2 group by Account)t2
on t1.Account = t2.Account
 
Share this answer
 
v2
This T-SQL statement does exactly what you are after:

SQL
SELECT DISTINCT t2.Account, 
    (SELECT COUNT(DISTINCT t1.IP) 
     FROM Table1 t1
     WHERE t1.Account = t2.Account) as IPCount,
    (SELECT COUNT(DISTINCT t22.AccountName)
     FROM Table2 t22
     WHERE t22.Account = t2.Account AND t22.Flag = 1) as AccountNameCount       
FROM Table2 t2
ORDER BY t2.Account DESC


Best Regards,

—MRB
 
Share this answer
 
Also consider this one :

SQL
select a.Account, IP, AccountName from 
   (select Account, count(account) IP from table1 group by Account) a
left outer join
   (select Account, count(account) AccountName from table2 where flag=1 group by Account) b
on a.Account=b.Account


Attention that the join type is important when some accounts have records in Table2 and doesn't have any in Tabl1.

Hope It Helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900