计算列中值的不同实例数
Calculate number of distinct instances of value in column
长期潜伏者。尽管 none 的解决方案对我有用,但我搜索了又搜索。
我在 Sybase (ASE) 数据库中工作(大多数 mssql/mysql 事务性数据库解决方案都可以正常工作)
在我的示例中,我尝试 calculate/count 特定 'party_id' 在列中列出的次数。我遇到的问题是它只计算每一行 - 所以当然计数总是 1。
查看输出:
(我希望 party_id 130568 在 refs 列中显示“2”,125555 显示“5”,等等。)
output
这是我的查询:
select
count(distinct p.party_id) as refs,
p.party_id,
sp_first_party(casenum),
c.casenum,
mld.mailing_list,
p.our_client
from cases c
inner join party p on c.casenum=p.case_id
inner join names n on n.names_id=p.party_id
inner join mailing_list_defined mld on n.names_id=mld.names_id
where
mld.mailing_list like 'Mattar Stars'
and mld.addr_type like 'Home'
and n.deceased='N'
and p.our_client='Y'
group by p.party_id, c.casenum, mld.mailing_list, p.our_client
order by sp_first_party(casenum) asc
如有任何提示,我们将不胜感激。
谢谢
听起来您需要使用 APPLY 语句。不确定 APPLY 语句中的连接条件是否正确,但您应该能够推断逻辑。看看这是否适用于 Sybase。
SELECT pic.PartyInstanceCount AS refs
,p.party_id
,sp_first_party(casenum)
,c.casenum
,mld.mailing_list
,p.our_client
FROM cases AS c
INNER JOIN party AS p ON c.casenum = p.case_id
INNER JOIN names AS n ON n.names_id = p.party_id
INNER JOIN mailing_list_defined AS mld ON n.names_id = mld.names_id
OUTER APPLY (
SELECT COUNT(1) AS PartyInstanceCount
FROM party p2
WHERE p2.case_id = c.casenum
) pic
WHERE mld.mailing_list LIKE 'Mattar Stars'
AND mld.addr_type LIKE 'Home'
AND n.deceased = 'N'
AND p.our_client = 'Y'
ORDER BY
sp_first_party(casenum) ASC
长期潜伏者。尽管 none 的解决方案对我有用,但我搜索了又搜索。 我在 Sybase (ASE) 数据库中工作(大多数 mssql/mysql 事务性数据库解决方案都可以正常工作)
在我的示例中,我尝试 calculate/count 特定 'party_id' 在列中列出的次数。我遇到的问题是它只计算每一行 - 所以当然计数总是 1。
查看输出: (我希望 party_id 130568 在 refs 列中显示“2”,125555 显示“5”,等等。) output
这是我的查询:
select
count(distinct p.party_id) as refs,
p.party_id,
sp_first_party(casenum),
c.casenum,
mld.mailing_list,
p.our_client
from cases c
inner join party p on c.casenum=p.case_id
inner join names n on n.names_id=p.party_id
inner join mailing_list_defined mld on n.names_id=mld.names_id
where
mld.mailing_list like 'Mattar Stars'
and mld.addr_type like 'Home'
and n.deceased='N'
and p.our_client='Y'
group by p.party_id, c.casenum, mld.mailing_list, p.our_client
order by sp_first_party(casenum) asc
如有任何提示,我们将不胜感激。 谢谢
听起来您需要使用 APPLY 语句。不确定 APPLY 语句中的连接条件是否正确,但您应该能够推断逻辑。看看这是否适用于 Sybase。
SELECT pic.PartyInstanceCount AS refs
,p.party_id
,sp_first_party(casenum)
,c.casenum
,mld.mailing_list
,p.our_client
FROM cases AS c
INNER JOIN party AS p ON c.casenum = p.case_id
INNER JOIN names AS n ON n.names_id = p.party_id
INNER JOIN mailing_list_defined AS mld ON n.names_id = mld.names_id
OUTER APPLY (
SELECT COUNT(1) AS PartyInstanceCount
FROM party p2
WHERE p2.case_id = c.casenum
) pic
WHERE mld.mailing_list LIKE 'Mattar Stars'
AND mld.addr_type LIKE 'Home'
AND n.deceased = 'N'
AND p.our_client = 'Y'
ORDER BY
sp_first_party(casenum) ASC