Simple SQL Query in access

ε祈祈猫儿з 提交于 2019-12-25 13:26:23

问题


I need some help with figuring out two queries I am trying out from my textbook. I can't seem to figure them out. Any help would be appreciated! The first query deals with getting casino numbers for the casinos whose cities are the first results of the alphabetical list of said cities. Confusing right?

SELECT Location
FROM Casino
GROUP BY Location
ORDER By Location;

This is what I have so far. What this will do is put AtlanticCity as the top result, which is what I want. Where i'm confused is how to solely get AtlanticCity and have the query display its Casino Number (which leads me to believe that I need a nested query).

The second query asks for PlayerNums who play the same slot machines at all casinos. I don't know how to approach this one.

Thanks for any help! Here are the tables: https://gyazo.com/a961956b95e04dde93e7dabbba215dcb


回答1:


Well you can actually use the 'TOP' keyword if you are using the JET Engine.

SELECT TOP 1 Location
FROM Casino
GROUP BY Location
ORDER By Location;

For the next one, can you post the table and attributes?




回答2:


(Please Note : This is an edited version of my original answer which was corrected to remove a bug I identified after posting which would list, for example, Players who had played at three Casinos one slot machine that had been played at all Casinos and at four Casinos a second similarly qualified Slot machine. It would also fail to list Players who had played a qualified Slot machine at all Casinos if they had also played at least one other qualified Slot machine.

This edited version has been tested on an expanded dataset. It will list list only Players who have Played any particular Slot at each of the Casinos at least once. It should also be easier to understand.)

As for your second problem, I suggest the following as a soultion...

SELECT DISTINCT PlayerNum
FROM (SELECT PlayerNum, SlotNum, Count(*) AS CountSlot
      FROM Plays
      WHERE SlotNum IN 
          (SELECT SlotNum
           FROM
               (SELECT DISTINCT SlotNum, CasinoNum
                FROM Plays) SlotCasino
           GROUP BY SlotNum
           HAVING COUNT(CasinoNum) =
              (SELECT COUNT(CasinoNum)
               FROM Casino))
      GROUP BY PlayerNum, SlotNum) EligiblePlayers
WHERE CountSlot = (SELECT COUNT(CasinoNum)
                   FROM Casino);

The logic that I followed was that I should determine which Slots had been played at all the Casinos and which Players had played those Slots at all of the Casinos.

To figure out which Slots had been played at all the Casinos I first isolated all combinations of Slot played and Casinos played at using -

SELECT DISTINCT SlotNum, CasinoNum
FROM Plays

I then found out which Slots had been played at each of the current number of Casinos using -

SELECT SlotNum
FROM
    (SELECT DISTINCT SlotNum, CasinoNum
    FROM Plays) SlotCasino
    GROUP BY SlotNum
    HAVING COUNT(CasinoNum) =
        (SELECT COUNT(CasinoNum)
        FROM Casino)

I then found out which Players had Played on these Slots and at how many Casinos using -

SELECT PlayerNum, SlotNum, Count(*) AS CountSlot
FROM Plays
WHERE SlotNum IN 
    (SELECT SlotNum
     FROM
         (SELECT DISTINCT SlotNum, CasinoNum
          FROM Plays) SlotCasino
     GROUP BY SlotNum
     HAVING COUNT(CasinoNum) =
        (SELECT COUNT(CasinoNum)
         FROM Casino))
GROUP BY PlayerNum, SlotNum

I refined this list to only Players who have Played a qualified Slot at each of the Casinos using -

SELECT DISTINCT PlayerNum
FROM (SELECT PlayerNum, SlotNum, Count(*) AS CountSlot
      FROM Plays
      WHERE SlotNum IN 
          (SELECT SlotNum
           FROM
               (SELECT DISTINCT SlotNum, CasinoNum
                FROM Plays) SlotCasino
           GROUP BY SlotNum
           HAVING COUNT(CasinoNum) =
              (SELECT COUNT(CasinoNum)
               FROM Casino))
      GROUP BY PlayerNum, SlotNum) EligiblePlayers
WHERE CountSlot = (SELECT COUNT(CasinoNum)
                   FROM Casino);

I hope that I have explained this adequately. If not, or if you have any other questions, then please feel free to reply.



来源:https://stackoverflow.com/questions/32939801/simple-sql-query-in-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!