Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL to get X number of accounts from DB, which could be variable number of rows

I have a SQL Server table AccountAction which is denormalised. It is a flattened version of the Account and Action tables, which I'm hoping should be a lot quicker for reporting queries over millions of rows. One Account can have many Actions, so the table looks similar to:

Account     Action
account1    action1
account1    action2
account1    action10
account2    action5

However I'm having some trouble getting the information back for a restricted subset in a simple stored procedure.

select Account, Action
from AccountAction
where ???

What I'm looking for is to get the first X accounts, with all their actions. So this will be a dynamic number of rows. So using the example table above if I passed in 1, I would get 3 rows (i.e. give me all rows for the first account).

(I don't mind that the account name will be in each row - it is pivoted elsewhere)

Do I need to use a ROWNUM or similar to restrict the rows? I'm sure this must be a simpler issue than I've found so far.

EDIT

The answers using TOP won't work, in the example I'd be wanting 3 rows returned if I said 'give me one (the first) account'. But how do I know there will be 3? Its dynamic. Also they may not be sequential, what if account1's action99 was at position 55 million in the results.

like image 415
finoutlook Avatar asked Apr 30 '12 11:04

finoutlook


People also ask

How do you find the number of rows and records in SQL?

Use the COUNT aggregate function to count the number of rows in a table. This function takes the name of the column as its argument (e.g., id ) and returns the number of rows for this particular column in the table (e.g., 5).

Which SQL function is used to COUNT the number of rows in SQL query?

The SQL COUNT( ) function is used to return the number of rows in a table. It is used with the Select( ) statement.

How do I SELECT a specific row number in SQL?

To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.

How do you get the number of rows affected by a query?

MySQL ROW_COUNT() can be used to get the total number of rows affected by MySQL query. To illustrate it we are creating a procedure with the help of which we can insert records in a table and it will show us how many rows have been affected.


2 Answers

WITH
  SequencedData
AS
(
  SELECT
    DENSE_RANK() OVER (ORDER BY Account) AS account_sequence_id,
    *
  FROM
    AccountAction
)
SELECT
  *
FROM
  SequenceData
WHERE
  account_sequence_id = ???

Or, for multiples...

WHERE
  account_sequence_id BETWEEN 3 AND 5    -- For the 3rd, 4th and 5th accounts.
like image 112
MatBailie Avatar answered Sep 23 '22 06:09

MatBailie


SELECT *
FROM AccountAction
WHERE account IN  (SELECT account
    FROM AccountAction
    GROUP BY account HAVING account BETWEEN *start-account* AND *end-account*
    ORDER BY account
)

Explanation: The subquery groups by the distinct accounts (and allows for more fine-grained selection criteria than a simple DISTINCT) and returns only those accounts. The outer SELECT gets you a variable number of rows depending on the distinct accounts fetched by the subquery.

EDIT: The above assumes that one can filter by the account field in AccountAction table; this is usually the case in tables that join a M:N relationship at DB level.

like image 45
Joseph Victor Zammit Avatar answered Sep 26 '22 06:09

Joseph Victor Zammit