在SQL中分组 - 帐户和费用 [英] Grouping in SQL - accounts and charges
问题描述
尝试编写优化查询,因为表格很大。
让我们看看你有病人和收费
Acctnumber | ChargeCode
A | 103567
A | 105678
A | 103567
A | 105678
B | 345356 >
B | 105678
B | 234035
C | 105678
D | 403567
D | 505678
我需要查询来挑选所有只收费10开头的账户。结果将是
Acctnumber | ChargeCode
A | 103567
A | 105678
A | 103567
A | 105678
C | 105678
B账户还有10个收费代码,但也有其他代码。我们只想要10个代码。
我尝试过:
我尝试在(chargecode10)中编写查询而不存在,但这是一种缓慢的方法。
如果有人知道更好的方法,请告诉我
Trying to write an optimized query because table is huge.
Lets see you have patients and charges
Acctnumber |ChargeCode
A |103567
A |105678
A |103567
A |105678
B |345356
B |105678
B |234035
C |105678
D |403567
D |505678
I need the query to pick all the accounts that only have charges starting with 10. So the result would be
Acctnumber |ChargeCode
A |103567
A |105678
A |103567
A |105678
C |105678
The B account also has 10 charge code but it has other codes as well. We only want 10 codes.
What I have tried:
I tried writing the query through in (chargecode10) and not exist but that's a slow way to do it.
Let me know if someone knows a better way
推荐答案
ChargeCode是否为字符串?
SELECT Acctnumber,ChargeCode FROM PatientCharges WHERE ChargeCode LIKE '10%';
如果是数字
SELECT Acctnumber,ChargeCode FROM PatientCharges WHERE LTRIM(STR(ChargeCode,10))LIKE '10%';
Is ChargeCode a string?
SELECT Acctnumber, ChargeCode FROM PatientCharges WHERE ChargeCode LIKE '10%';
If it's a number
SELECT Acctnumber, ChargeCode FROM PatientCharges WHERE LTRIM(STR(ChargeCode,10)) LIKE '10%';
这篇关于在SQL中分组 - 帐户和费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!