SQL指定组的值?

时间:2016-01-25 22:33:08

标签: sql sql-server

我正在使用此脚本从我的服务表中查找与service_code匹配的ada_code,然后查找将service_code作为ada_code的事务{1}}不在transactions

WITH dentures AS (
SELECT service_code
FROM services
WHERE (ada_code='D5110' OR ada_code='D5120' OR ada_code='D5130' OR ada_code='D5140' OR ada_code='REMAU' OR ada_code='REAML' OR ada_code='REMAK'))

SELECT t.clinic, COUNT(DISTINCT t.tran_num) AS quantity, d.service_code, SUM(t.amount) AS total, p.fiscal_year, p.period_id
FROM transactions t
INNER JOIN dentures d
ON(t.service_code=d.service_code)
CROSS JOIN period p
WHERE (t.time_ran BETWEEN p.period_start AND p.period_end)
AND status='A'
AND impacts='P'
GROUP BY t.clinic, d.service_code, p.fiscal_year, p.period_id

脚本工作原理查找,但我需要分成两个不同的组。 A组(所有服务代码来自D5110,D5120,D5130,D5140)和B组是剩余的。有没有办法从使用那些service_code的服务中查看ada_code并预先将它们预先确定为一组?在A组中的四个ada_code之间,每个大约有10个,我有35个不同的独特位置,它们也需要保持独立。感谢

1 个答案:

答案 0 :(得分:0)

不是按服务代码分组,而是按照

之类的case语句进行分组
CASE WHEN servicecode IN (group a codes) "Group A"
       ELSE  servicecode --or "Group B", I'm not sure which you want
END