FAQ
Suppose I have following table in my database :

activity1 | activity2 | activity3 | activity4 | activity5
6 | 7 | 5 | 9 | 8

The first row are the field names and the second row are the data
entries. How can I get 3 maximum values out of each row of this table?
Like from this row, I should get 7,8,9.
Eagerly waiting fro your reply.
Thank you.

--
Sandeep Kaur
E-Mail: mkaurkhalsa@gmail.com
Blog: sandymadaan.wordpress.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Christian Erhardt at Jul 24, 2013 at 7:09 am
    Just do be clear... you are mixing strings and numbers in one table on
    different rows? I think your data structure is wrong and you will not be
    able to achieve what you're trying.

    I'd would be easy if the table would look like this:

    +-----------------+
    Activities |
    +---------+-------+
    Activity | Number|
    +---------+-------+
    activity1| 0|
    activity2| 1|
    activity3| 2|
    activity4| 3|
    activity5| 4|
    +---------+-------+

    then you would be able to do something like this:

    Activities.objects.all().order_by("-Number")[:3]

    Am Mittwoch, 24. Juli 2013 08:08:42 UTC+2 schrieb sandy:
    Suppose I have following table in my database :

    activity1 | activity2 | activity3 | activity4 | activity5
    6 | 7 | 5 | 9 | 8

    The first row are the field names and the second row are the data
    entries. How can I get 3 maximum values out of each row of this table?
    Like from this row, I should get 7,8,9.
    Eagerly waiting fro your reply.
    Thank you.

    --
    Sandeep Kaur
    E-Mail: mkaur...@gmail.com <javascript:>
    Blog: sandymadaan.wordpress.com
    --
    You received this message because you are subscribed to the Google Groups "Django users" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
    To post to this group, send email to django-users@googlegroups.com.
    Visit this group at http://groups.google.com/group/django-users.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dennis Lee Bieber at Jul 25, 2013 at 12:37 am
    On Wed, 24 Jul 2013 11:38:42 +0530, Sandeep kaur <mkaurkhalsa@gmail.com>
    declaimed the following:
    Suppose I have following table in my database :

    activity1 | activity2 | activity3 | activity4 | activity5
    6 | 7 | 5 | 9 | 8

    The first row are the field names and the second row are the data
    entries. How can I get 3 maximum values out of each row of this table?
    Like from this row, I should get 7,8,9.
    Eagerly waiting fro your reply.
      Well, I'd start by normalizing that... One should not have repeating
    groups in a relational database; it is often easier to extract data when
    the "column name" is treated as a key in a two column table

    Activity_Order | Activity_Value
    1 | 6
    2 | 7
    3 | 5
    4 | 9
    5 | 8

      Then one can do something like (SQL, not Django ORM)

    select Activity_Order, Activity_Value from Activities
    order by Activity_Value desc
    limit 3

    (works with SQLite3)
    "4","9"
    "5","8"
    "2","7"

      Granted, to really be useful, you'll need a foreign key linking back to
    the parent record:

    primarykey|some|stuff|related|to|activity1|activity2|activity3|...

    =>

    primarykey|some|stuff|related|to

    and

    fkeytoprimarykey|activityorder|activityvalue
    ...
    --
      Wulfraed Dennis Lee Bieber AF6VN
         wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/

    --
    You received this message because you are subscribed to the Google Groups "Django users" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
    To post to this group, send email to django-users@googlegroups.com.
    Visit this group at http://groups.google.com/group/django-users.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdjango-users @
categoriesdjango, python
postedJul 24, '13 at 6:09a
activeJul 25, '13 at 12:37a
posts3
users3
websitedjangoproject.com

People

Translate

site design / logo © 2022 Grokbase