Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:57:19+00:00 2026-06-09T09:57:19+00:00

I have a table called lottery_winners with the following useful colums: +—-+——+——+——–+———+ | id

  • 0

I have a table called lottery_winners with the following useful colums:

+----+------+------+--------+---------+
| id | plid | zbid | amount | numbers |
+----+------+------+--------+---------+

id is the unique, primary id for the table. plid refers to the past_lotteries table (i.e I can get all the lottery winners from a specific lottery in the past this way. zbid is the id of the member/user (the winner). amount is the sum of money they won in the lottery, and finally numbers is a VARCHAR CSV field with their lottery numbers.

Here’s an example of what rows could be in the table:

+----+------+------+--------+---------+
| id | plid | zbid | amount | numbers |
+----+------+------+--------+---------+
| 1  | 1    | 2    | 1      | 1,2,3   |
+----+------+------+--------+---------+
| 2  | 1    | 4    | 5      | 4,5,6   |
+----+------+------+--------+---------+
| 3  | 1    | 3    | 7      | 3,4,5   |
+----+------+------+--------+---------+
| 4  | 1    | 2    | 3      | 7,8,9   |
+----+------+------+--------+---------+
| 5  | 2    | 2    | 8      | 8,9,10  |
+----+------+------+--------+---------+

Now, I want to run a SELECT statement which will bring back all the rows but in a really specific order. The rows should be in grouped by zbid as such (in this case I have added a WHERE plid=1 clause):

+----+------+------+--------+---------+
| id | plid | zbid | amount | numbers |
+----+------+------+--------+---------+
| 1  | 1    | 2    | 1      | 1,2,3   |
+----+------+------+--------+---------+
| 4  | 1    | 2    | 3      | 7,8,9   |
+----+------+------+--------+---------+
| 2  | 1    | 4    | 5      | 4,5,6   |
+----+------+------+--------+---------+
| 3  | 1    | 3    | 7      | 3,4,5   |
+----+------+------+--------+---------+

Next criteria is that not only should they be grouped by zbid, but within this grouping they should be ordered by amount DESC. This is what it would now look like:

+----+------+------+--------+---------+
| id | plid | zbid | amount | numbers |
+----+------+------+--------+---------+
| 4  | 1    | 2    | 3      | 7,8,9   |
+----+------+------+--------+---------+
| 1  | 1    | 2    | 1      | 1,2,3   |
+----+------+------+--------+---------+
| 2  | 1    | 4    | 5      | 4,5,6   |
+----+------+------+--------+---------+
| 3  | 1    | 3    | 7      | 3,4,5   |
+----+------+------+--------+---------+

The top two rows have swapped around.

One more criteria. As you can see, although they are grouped by zbid, there’s no specific order to them. I want it to group by zbid, but the order should be based on sum(amount) for each group.

The following table shows the totals for each zbid in no specific order (taking into account that plid=1:

+------+-------------+
| zbid | sum(amount) |
+------+-------------+
| 2    | 4           |
+------+-------------+
| 3    | 7           |
+------+-------------+
| 4    | 5           |
+------+-------------+

So using this information the final result using the SELECT statement should be the following (with an added sum(amount) column):

+----+------+------+--------+---------+-------------+
| id | plid | zbid | amount | numbers | sum(amount) |
+----+------+------+--------+---------+-------------+
| 3  | 1    | 3    | 7      | 3,4,5   | 7           |
+----+------+------+--------+---------+-------------+
| 2  | 1    | 4    | 5      | 4,5,6   | 5           |
+----+------+------+--------+---------+-------------+
| 4  | 1    | 2    | 3      | 7,8,9   | 4           |
+----+------+------+--------+---------+-------------+
| 1  | 1    | 2    | 1      | 1,2,3   | 4           |
+----+------+------+--------+---------+-------------+

That’s it! Now I’ve tried a couple of things myself, but I’m not exactly sure how to get the full final result. I have tried:

SELECT id,plid,zbid,amount,numbers,sum(amount) FROM lottery_winners GROUP BY zbid ORDER BY sum(amount) DESC

Now that seemed to meet the final criterion, but it didn’t give me individual results for the table.

Please also note that as these results will be paginated, I will need to be adding LIMIT $start,$perpage to the end of the query.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:57:20+00:00Added an answer on June 9, 2026 at 9:57 am

    have this a try:

    SELECT  a.id, 
            a.plid, 
            a.zbid,
            a.amount, 
            a.numbers,
            c.totalAmount
    FROM    lottery_winners a
                INNER JOIN  (
                                SELECT  b.zbid,
                                        SUM(b.amount) totalAmount
                                FROM    lottery_winner b
                                WHERE   b.plid = 1
                                GROUP BY b.zbid
                            ) c 
                    ON a.zbid = c.zbid
    WHERE   a.plid = 1
    ORDER BY    c.totalAmount desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called 'survey_product' with the following structure: id int(11) order_id int(11)
I have table called Reporting with following columns OutletId CampaignId ItemId Qty 10 1
I have table name called Person with following column names P_Id(int), LastName(varchar), FirstName (varchar).
I have a table called tblAssetsInUse with the following structure: intPK intAssetID datCheckedOut datCheckedIn
I have a table called Transactions with the following schema: ColumnName DataType Constraints ----------
I have table called comments with following structure: id | entry_id | date |
We have a table called t_reading , with the following schema: MEAS_ASS_ID NUMBER(12,0) READ_DATE
I have table called stats . In am inserting yes or no in the
i have table called as Support, which have a field named Name and contains
I have table called Buttons. Buttons table i have column button_number . Table contain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.