以下图片已上传,以显示我想要做的事情以及我想要的内容
任何人都可以帮我写Query以获得我想要的结果 请检查以下
SELECT *
FROM KPT
WHERE PROPERTY_ID IN (SELECT PROPERTY_ID
FROM khata_header
WHERE DIV_ID = 3
and RECORD_STATUS = 0)
and CHALLAN_NO > 42646
以上是我写的查询,我得到了以下结果集
ID CHALLAN_NO PROPERTY_ID SITE_NO TOTAL_AMOUNT
----- ------------- -------------- ------------------- ---------------
1242 42757 3103010141 296 595
1243 63743 3204190257 483 594
1244 63743 3204190257 483 594
1334 43395 3217010223 1088 576
1421 524210 3320050416 (null) (null)
1422 524210 3320050416 (null) (null)
1560 564355 3320021408 (null) (null)
1870 516292 3320040420 (null) (null)
1940 68357 3217100104 139 1153
1941 68357 3217100104 139 1153
2002 56256 3320100733 511 4430
2003 56256 3320100733 511 4430
2004 66488 3217040869 293 3094
2005 66488 3217040869 293 3094
2016 64571 3217040374 (null) (null)
2036 523122 3320020352 (null) (null)
2039 65682 3217040021 273 919
在我的结果集中,我重复了PropertyId,因为有多个条目,
关于桌子的小背景
我正在写一个子查询来获得结果,所以我被困住了我不知道如何得到我的结果请帮助
答案 0 :(得分:1)
这会让你更近一点吗?
SELECT
PROPERTY_ID, COUNT(*)
FROM
KPT
WHERE
PROPERTY_ID IN (SELECT PROPERTY_ID
FROM khata_header
WHERE DIV_ID = 3 AND RECORD_STATUS = 0)
AND CHALLAN_NO > 42646
GROUP BY
PROPERTY_ID
HAVING
COUNT(*) >= 2
答案 1 :(得分:0)
要确定重复了哪些属性ID,以下未经测试的查询应该有效。
SELECT PROPERTY_ID, COUNT(PROPERTY_ID) AS PROPERTY_COUNT
FROM KPT
WHERE PROPERTY_ID IN (SELECT PROPERTY_ID
FROM khata_header
WHERE DIV_ID = 3
AND RECORD_STATUS = 0)
AND CHALLAN_NO > 42646
GROUP BY PROPERTY_ID
要确定哪些属性ID重复了两次以上,请在上述查询的末尾添加以下HAVING
子句。
HAVING COUNT(PROPERTY_ID) > 2
答案 2 :(得分:0)
也许你可以试试这个
PROPERTY_ID重复计数可以通过count()和group by来实现,以获得PROPERTY_ID的重复次数超过2次,您可以创建第一个选择到临时表并在临时表上创建2个选择,在结束临时表。
- 创建临时表
select PROPERTY_ID , count(*) as hitcount
into #tmp
from kpt
WHERE PROPERTY_ID IN (SELECT PROPERTY_ID
FROM khata_header
WHERE DIV_ID = 3
and RECORD_STATUS = 0)
and CHALLAN_NO > 42646
group by PROPERTY_ID
- 问题1:
select count(*) from #tmp where hitcount > 1
- 和
联合所有
- 问题2:
select * from #tmp where hitcount > 2
drop table #tmp
希望这能回答你的问题。
祝你好运