Oracle
對於恰好接收 X 個項目(行)的狀態(col),按重量(col)對項目進行排序
我一直試圖弄清楚這個幾個小時,但還沒有得到它。表中的每一行都是一個包。
我能夠獲得具有確切數量的包裹的州,但不知道如何只查詢具有這些州之一的包裹。
SELECT State FROM testing GROUP BY State HAVING COUNT(State) = 7;
誰能指出我正確的方向?
因此,從下面的數據中,它將返回 GA 和 MI 的 10 個包裹,按重量排序。
------------------- | State | Weight | ------------------- | AL | 29.745 | | AZ | 54.4081 | | CA | 3.79151 | | CA | 94.0579 | | CT | 68.6374 | | CT | 80.6644 | | CT | 55.8415 | | FL | 5.49145 | | FL | 76.6451 | | FL | 7.02167 | | GA | 84.3802 | | GA | 5.51498 | | GA | 12.9253 | | GA | 88.4529 | | GA | 38.8692 | | MN | 82.009 | | MN | 33.893 | | MO | 10.9912 | | MS | 41.8613 | | MT | 21.3695 | | MI | 18.9998 | | MI | 71.6003 | | MI | 72.0666 | | MI | 67.8444 | | MI | 72.2397 | | ND | 5.66484 | | ND | 80.8316 | | NJ | 81.8038 | | NJ | 14.91 | | NJ | 55.238 |
我終於想通了!
SELECT State, Weight FROM testing WHERE State = ANY ( SELECT State FROM testing GROUP BY State HAVING COUNT(*) = 7 ) ORDER BY Weight DESC;
分析
你真的需要學習它們。
with data as ( select "State", "Weight" -- State is a reserved word. ,count(*) over (partition by state) as "Packages/State" from unnamed_table ) select "State", "Weight" from data where "Packages/State" = 7 order by "State", "Weight"