3

私は2つのテーブルを持っています:

**OrgPhysicianMappingtbl**
org varchar
physician varchar

**PhysicianDatatbl**
physician varchar
names varchar
datapoint int

からのデータOrgPhysicianMappingtbl

+---------+-----------+
|   org   | physician |
+---------+-----------+
| ABC123  | B032      |
| ABC123  | B023      |
| ABC123  | A022      |
| ABB443  | A32       |
| ABB332  | F23       |
| BVD222  | G23       |
| BVD222  | GG2       |
| BOS5223 | G4        |
+---------+-----------+

からのデータPhysicianDatatbl

+------------+----------------------------------+-----------+
| physician  |              names               | datapoint |
+------------+----------------------------------+-----------+
| B032       | CASH                             | 68        |
| B032       | MEDICAID                         | 89        |
| B032       | ALL THIRD PARTY                  | 3,769     |
| B032       | WORKERS COMP                     | 39        |
| B032       | US SCRIPT (PROC-UNSP)            | 86        |
| B032       | MEDCO HLTH SOLUTIONS (PROC-UNSP) | 79        |
| B032       | BCBS WELLPOINT/ANTHEM/WEL UNSPEC | 76        |
| B032       | KY EMPLOYEES HLTH PLN/KEHP (KY)  | 62        |
| B032       | UHC/PAC/AARP MED PDP GENERAL(KY) | 52        |
| B032       | WELLCARE OF KENTUCKY (KY)        | 42        |
| B032       | CCRX MED PDP GENERAL (KY)        | 39        |
| B032       | WORKERS COMP - EMPLOYER          | 37        |
| B032       | HUMANA/CAREPLS MED D GENERAL(KY) | 27        |
| B032       | CIGNA MEDICARE RX PDP GNRL (KY)  | 26        |
| B023       | CASH                             | 167       |
| B023       | MEDICAID                         | 34        |
| B023       | ALL THIRD PARTY                  | 3,165     |
| B023       | WORKERS COMP                     | 56        |
| B023       | WORKERS COMP - EMPLOYER          | 50        |
| B023       | BLUE CHOICE PPO (TX)             | 48        |
| B023       | TRICARE HUMANA MILITARY SOUTH    | 47        |
| B023       | MEDCO HLTH SOLUTIONS (PROC-UNSP) | 32        |
| B023       | BROADSPIRE INC                   | 27        |
| B023       | ADVANCEPCS (PROC-UNSP)           | 27        |
| B023       | UNITED HLTHCARE-(TX) TEXAS       | 23        |
| B023       | HEALTHSPRING PDP (TX)            | 18        |
| B023       | AETNA INC.-(TX) HOUSTON          | 13        |
| B023       | WELLCARE MEDICARE D GENERAL(TX)  | 12        |
+------------+----------------------------------+-----------+

望ましい結果の例:

+-----------+-----------------+-----------+
| org       |      names      | sum(datapoints|
+-----------+-----------------+-----------+
| ABC123    | CASH            | 236       |
| ABC123    | MEDICAID        | 123       |
| ABC123    | ALL THIRD PARTY | 6,933     |
| ABC123    | WORKERS COMP    | 94        |
| …         | …               | …         |
+-----------+-----------------+-----------+

データをORGレベルにロールアップする必要があります。ORGごとに複数の医師がいます。すべての医師が持っておりnames、それにdatapoints関連付けられています。私が欲しい結果は、レベルにロールアップされたすべてのdatapoints合計です。namesorg

誰かが私にこれを始めさせてもらえますか?

これが私が試したことです:

select o.org,p.names,sum(p.datapoint)
from OrgPhysicianMappingtbl o
join PhysicianDatatbl p
on o.physician=p.physician
group by o.org,p.names
4

4 に答える 4

4

あなたが使用することができますGROUPING SETS

select o.org, p.names, sum(p.datapoint)
from OrgPhysicianMappingtbl o 
inner join PhysicianDatatbl p
  on o.physician = p.physician
GROUP BY GROUPING SETS((o.org), (p.names))

SQL FiddlewithDemoを参照してください

于 2012-09-13T10:42:52.030 に答える
2

上手!

SELECT o.org, p.names, sum(p.datapoint)
FROM OrgPhysicianMappingtbl o INNER JOIN PhysicianDatatbl p
     ON o.physician = p.physician
GROUP BY o.org, p.names

http://www.sqlfiddle.com/#!2/f59c4/1

于 2012-09-13T04:03:30.487 に答える
2
select o.org,p.names,sum(p.datapoint)
from OrgPhysicianMappingtbl o
join PhysicianDatatbl p
on o.physician=p.physician
group by o.org,p.names with ROLLUP
于 2012-09-13T06:44:28.430 に答える
2

これでもうまくいくはずです

SELECT org,names,TotalDP 
FROM(
    SELECT 
        o.org
        ,p.names
        ,TotalDP = SUM(p.datapoint) OVER(PARTITION BY o.org,p.names)
        ,Rn = ROW_NUMBER() OVER(PARTITION BY o.org,p.names ORDER BY (SELECT 1))
    FROM OrgPhysicianMappingtbl o
    JOIN PhysicianDatatbl p on o.physician=p.physician)X WHERE X.Rn = 1
于 2012-09-13T10:27:42.570 に答える