连接语句中的sql double count [英] sql double count in join statement

查看:149
本文介绍了连接语句中的sql double count的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个SQL语句,我需要将它们加入一条语句中,但是当我这样做时,结果是不合适的。 :/

 选择
nieruchomosci.nieruchomoscnr,count(wynajecia.nieruchomoscNr)as wynajecia
from
nieruchomosci,wynajecia
其中
nieruchomosci.nieruchomoscnr = wynajecia.nieruchomoscNr
GROUP BY
GROUP BY
nieruchomosci.nieruchomoscnr;

选择
nieruchomosci.nieruchomoscnr,将(wizyty.nieruchomoscnr)计为
,从
nieruchomosci中选择,wizyty
其中
wizyty.nieruchomoscnr = nieruchomosci.nieruchomoscnr
GROUP BY
nieruchomosci.nieruchomoscnr;

这就是我加入他们的方式:

 选择
nieruchomosci.nieruchomoscnr,将
数(wynajecia.nieruchomoscNr)设置为wynajecia,将
数(wizyty.nieruchomoscnr)设置为来自$ wisty的

nieruchomosci,wynajecia,wiggyty
,其中
nieruchomosci.nieruchomoscnr = wynajecia.nieruchomoscNr
和wizyty.nieruchomoscnr = nieruchomosci.nieruchcisrr
GROUP BY
GROUP BY。 ;

与此同时, wynajecia和 wizyty中的数字相同,这是错误的。 :/



编辑:



使用以下代码,我得到:

  A14 8 8 
B16 6 6
B17 4 4
B18 4 4
B21 4 4
G01 6 6
L94 10 10

正确的输出应为:

  A14 2 4 
B16 3 2
B17 2 2
B18 2 2
B21 2 2
G01 3 2
L94 2 5

我设法以此矫正了一个代码:

  select nieruchomosci.nieruchomoscnr,
(从wynajecia选择计数(wynajecia.nieruchomoscNr),其中wynajecia.nieruchomoscNr = nieruchomosci.nieruchomoscnr)作为wynajecia,
(从wizyty选择计数(wizyty.nieruchomoscNr),其中wizyty.nieruchomoscNr = nieruchomosci.nieruchomoscnr)作为来自nieruchomosci
<$$ / pre>

但是我不是这样,这是解决问题的正确方法。

解决方案

请考虑以下场景:



TableA记录的ID:{1,2,3,4,5},



TableB记录的ID:{1,2,3,5},



TableC记录的ID:{1、4、5}



从TableA中选择*的结果将一个TableB (a.ID = b.ID)上的b是{1、2、3、5}



来自TableA的select * a的结果联接TableC c on(a .ID = c.ID)为{1,4,5}



从TableA a中选择* *的结果连接到TableB b上(a.ID = b.ID )在(a.ID = c.ID)上加入TableC c是{1,5}



因此,如果在Group By之后计数或计数,则记录是不同的



作为您的代码,您可以按以下代码使用左连接和sum(case end)AS:

 选择
nieruchomosci.nieruchomoscnr,将
sum(如果wynajecia.nieruchomoscNr为null则为0,否则1结束)为wynajecia,
sum(为wizyty时)。 nieruchomoscnr为null,然后为0,否则1结束)为wizyty
from
nieruchomosci
左加入
wynajecia上(nieruchomosci.nieruchomoscn r = wynajecia.nieruchomoscNr)
左加入
wizyty上(wizyty.nieruchomoscnr = nieruchomosci.nieruchomoscNr)

GROUP BY
NIeruchomosci.nieruchomoscnr;

问题更新的附加内容:



现在您有了具有记录ID的TableA:{1、2、3、4}



表B是

  TableAID值
1 B1-1
1 B1-2
2 B2-1
2 B2-2
4 B4 -1
4 B4-2

表C为

  TableAID值
1 C1-1
1 C1-2
1 C1-3
3 C3-1
3 C3-2
4 C4-1

三个表的结果加入

  select * from TableA a在(a.ID = b.ID)上加入TableB b(a。 ID = c.ID)

  TableAID TableB_Value TableC_Value 
1 B1-1 C1-1
1 B1-1 C1-2
1 B1-1 C1-3
1 B1-2 C1-1
1 B1-2 C1-2
1 B1-2 C1-3
4 B4-1 C4-1
4 B4-2 C4-1

因此,您得到了Count(B.Value):{1-> 6,4-> 2},Count(C.Value)相同。
您需要的是Count(B.Value):{1-> 2,2-> 2,4-> 2}和Count(C.Value):{1-> 3,3-> 2,4-> 1}



您更新的代码正确。但是为了获得更好的性能,您可以使用以下代码:

  select nieruchomosci.nieruchomoscnr,ifnull(wynajecia.wynajecia_count,0)作为wynajecia_count ,ifnull(wizyty.wizyty_count,0)来自nieruchomosci 

还剩
(选择wynajecia.nieruchomoscNr,count(*)为wynajecia group的wynajecia_count by wynajecia.nieruchomoscNr)作为wynajecia
on(wynajecia.nieruchomoscNr = nieruchomosci.nieruchomoscnr)
左加入
(选择wizyty.nieruchomoscNr,count(*)作为wizyty_count的wizyty_count由wizyty.nieruchomoscNr选择为wizyty
( .nieruchomoscNr = nieruchomosci.nieruchomoscnr)

ifnull函数用于mysql。对于Oracle,应为 nvl,对于MS SQL,应为 isnull。


I have these two SQL statements and what I need to do is join them into one statement, but when I do it, result is inappropriate. :/

select 
    nieruchomosci.nieruchomoscnr, count(wynajecia.nieruchomoscNr) as wynajecia 
from 
    nieruchomosci, wynajecia
where 
    nieruchomosci.nieruchomoscnr = wynajecia.nieruchomoscNr
GROUP BY 
    nieruchomosci.nieruchomoscnr;

select 
    nieruchomosci.nieruchomoscnr, count(wizyty.nieruchomoscnr) as wizyty 
from 
    nieruchomosci, wizyty
where 
    wizyty.nieruchomoscnr = nieruchomosci.nieruchomoscnr
GROUP BY 
    nieruchomosci.nieruchomoscnr;

This is how I have joined them:

select 
    nieruchomosci.nieruchomoscnr, 
    count(wynajecia.nieruchomoscNr) as wynajecia, 
    count(wizyty.nieruchomoscnr) as wizyty 
from 
    nieruchomosci, wynajecia, wizyty
where 
    nieruchomosci.nieruchomoscnr = wynajecia.nieruchomoscNr
    and wizyty.nieruchomoscnr = nieruchomosci.nieruchomoscNr
GROUP BY 
    nieruchomosci.nieruchomoscnr;

With this, numbers in 'wynajecia' and 'wizyty' are the same, which is wrong. :/

EDIT:

With this code I get:

A14 8   8
B16 6   6
B17 4   4
B18 4   4
B21 4   4
G01 6   6
L94 10  10

Correct output should be this:

A14 2   4
B16 3   2
B17 2   2
B18 2   2
B21 2   2
G01 3   2
L94 2   5

I managed to get currect one by this code:

select nieruchomosci.nieruchomoscnr,
(select count(wynajecia.nieruchomoscNr) from wynajecia where wynajecia.nieruchomoscNr = nieruchomosci.nieruchomoscnr) as wynajecia,
(select count(wizyty.nieruchomoscNr) from wizyty where wizyty.nieruchomoscNr = nieruchomosci.nieruchomoscnr) as wizyty
from nieruchomosci

But I don't this, that this is proper way to handle the problem.

解决方案

Consider the following scenes:

TableA's record's ID:{1, 2, 3, 4, 5},

TableB's record's ID:{1, 2, 3, 5},

TableC's record's ID:{1, 4, 5}

The result of select * from TableA a join TableB b on (a.ID = b.ID) is {1, 2, 3, 5}

The result of select * from TableA a join TableC c on (a.ID = c.ID) is {1, 4, 5}

The result of select * from TableA a join TableB b on (a.ID = b.ID) join TableC c on (a.ID = c.ID) is {1, 5}

So, if you count Or count after Group By, the record is different.

AS your code, You could used left join and sum(case end) AS following Code:

select 
    nieruchomosci.nieruchomoscnr, 
    sum(case when wynajecia.nieruchomoscNr is null then 0 else 1 end) as wynajecia, 
    sum(case when wizyty.nieruchomoscnr is null then 0 else 1 end) as wizyty 
from 
    nieruchomosci
left join 
    wynajecia on (nieruchomosci.nieruchomoscnr = wynajecia.nieruchomoscNr)
left join 
    wizyty on (wizyty.nieruchomoscnr = nieruchomosci.nieruchomoscNr)
GROUP BY 
    nieruchomosci.nieruchomoscnr;

Addition for your question update:

Now you have TableA with records' ID:{1, 2, 3, 4}

Table B is

TableAID Value
1        B1-1
1        B1-2
2        B2-1
2        B2-2
4        B4-1
4        B4-2

Table C is

TableAID Value
1        C1-1
1        C1-2
1        C1-3
3        C3-1
3        C3-2
4        C4-1

The result of three table join

select * from TableA a join TableB b on (a.ID = b.ID) join TableC c on (a.ID = c.ID)

is

TableAID  TableB_Value TableC_Value
1         B1-1         C1-1
1         B1-1         C1-2
1         B1-1         C1-3
1         B1-2         C1-1
1         B1-2         C1-2
1         B1-2         C1-3
4         B4-1         C4-1
4         B4-2         C4-1

So you get a Count(B.Value):{1->6,4->2}, Count(C.Value) is the same. What you want should be Count(B.Value):{1->2, 2->2, 4->2}, and Count(C.Value):{1->3, 3->2, 4->1}

The code you updated is right. But for better performance, you could use this code:

select nieruchomosci.nieruchomoscnr, ifnull(wynajecia.wynajecia_count, 0) as wynajecia_count, ifnull(wizyty.wizyty_count, 0)
from nieruchomosci
left join
(select wynajecia.nieruchomoscNr, count(*) as wynajecia_count from wynajecia group by  wynajecia.nieruchomoscNr) as wynajecia
on (wynajecia.nieruchomoscNr = nieruchomosci.nieruchomoscnr)
left join
(select wizyty.nieruchomoscNr, count(*) as wizyty_count from wizyty group by wizyty.nieruchomoscNr) as wizyty
on (wizyty.nieruchomoscNr = nieruchomosci.nieruchomoscnr)

The "ifnull" function is for mysql. It should be "nvl" for Oracle and "isnull" for MS SQL.

这篇关于连接语句中的sql double count的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆