Professional Web Applications Themes

Obtaining the total number of items in a column by the item's first letter. - MySQL

Hi everyone. I have a list of books in a table (pp_books) with 3 columns (book_id, book_name, book_type). I am trying to construct a single query that will give me a total of each of the books by there starting letter. For example, how many books there are beginning with 'a', how many beginning with 'b' etc etc... I need to add a lot more to the query, but I just need to try and figure out this initial query as I am certain it can be done, it's just a question of 'how'. Any ideas would be appraciated. Best ...

  1. #1

    Default Obtaining the total number of items in a column by the item's first letter.

    Hi everyone.

    I have a list of books in a table (pp_books) with 3 columns (book_id,
    book_name, book_type).

    I am trying to construct a single query that will give me a total of
    each of the books by there starting letter. For example, how many books
    there are beginning with 'a', how many beginning with 'b' etc etc...
    I need to add a lot more to the query, but I just need to try and
    figure out this initial query as I am certain it can be done, it's just
    a question of 'how'.

    Any ideas would be appraciated.

    Best regards.

    Daz.

    Daz Guest

  2. #2

    Default Re: Obtaining the total number of items in a column by the item's first letter.

    Daz wrote:
     



    SELECT SUBSTRING(book_name,1,1) AS letter,COUNT(*) as total FROM pp_books
    WHERE 1 GROUP BY letter ORDER BY letter ASC;



    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
    Brian Guest

  3. #3

    Default Re: Obtaining the total number of items in a column by the item's first letter.


    Brian Wakem wrote: 
    >
    >
    >
    > SELECT SUBSTRING(book_name,1,1) AS letter,COUNT(*) as total FROM pp_books
    > WHERE 1 GROUP BY letter ORDER BY letter ASC;
    >
    >
    >
    > --
    > Brian Wakem
    > Email: http://homepage.ntlworld.com/b.wakem/myemail.png[/ref]

    Utterly ingenious! Thanks Brian. I spent the best part of the day
    trying to figure it out, and just knew there was a simple solution.

    Daz Guest

  4. #4

    Default Re: Obtaining the total number of items in a column by the item's first letter.


    Daz wrote: 

    untestes, but it's got to be something like this...

    SELECT distinct(substr(pp1.book_name,1,1)) initial,count(pp2.book_name)
    total FROM pp_books pp1
    LEFT JOIN pp_books pp2 ON pp2.book_id = pp1.book_id
    GROUP BY initial;

    strawberry Guest

  5. #5

    Default Re: Obtaining the total number of items in a column by the item's first letter.


    Brian Wakem wrote: 
    >
    >
    >
    > SELECT SUBSTRING(book_name,1,1) AS letter,COUNT(*) as total FROM pp_books
    > WHERE 1 GROUP BY letter ORDER BY letter ASC;
    >
    >
    >
    > --
    > Brian Wakem
    > Email: http://homepage.ntlworld.com/b.wakem/myemail.png[/ref]

    oh, I guess that'll work too ;-)

    strawberry Guest

  6. #6

    Default Re: Obtaining the total number of items in a column by the item's first letter.


    Brian Wakem wrote: 
    >
    >
    >
    > SELECT SUBSTRING(book_name,1,1) AS letter,COUNT(*) as total FROM pp_books
    > WHERE 1 GROUP BY letter ORDER BY letter ASC;
    >
    >
    >
    > --
    > Brian Wakem
    > Email: http://homepage.ntlworld.com/b.wakem/myemail.png[/ref]

    Ugh... I am stuck again already.

    I would like to add another column, this one should could the books
    where the column type is equal to '1'. I will also need another 4
    columns each using a different type (1-5). I tried this for 1 column...

    SELECT LEFT( book_name, 1 ) AS letter, COUNT( * ) AS total, COUNT(
    SELECT WHERE book_type = '1' ) AS retired
    FROM pp_books
    WHERE 1
    GROUP BY letter
    ORDER BY letter ASC
    LIMIT 0 , 30;

    Once again, I know there is a simple solution, I just can't figure it
    out. I have spent a lot of today reading various advanced mysql query
    tutorials, too. I have learnt tons, but evidently not what I needed to.

    Daz Guest

  7. #7

    Default Re: Obtaining the total number of items in a column by the item's first letter.


    strawberry wrote: 
    > >
    > >
    > >
    > > SELECT SUBSTRING(book_name,1,1) AS letter,COUNT(*) as total FROM pp_books
    > > WHERE 1 GROUP BY letter ORDER BY letter ASC;
    > >
    > >
    > >
    > > --
    > > Brian Wakem
    > > Email: http://homepage.ntlworld.com/b.wakem/myemail.png[/ref]
    >
    > oh, I guess that'll work too ;-)[/ref]

    Hehe. Thanks strawberry. Your suggestion had been duly noted, as I know
    there's usually more than a single correct way to do things. :)

    Daz Guest

  8. #8

    Default Re: Obtaining the total number of items in a column by the item's first letter.

    Daz wrote:
     
    >>
    >>
    >>
    >> SELECT SUBSTRING(book_name,1,1) AS letter,COUNT(*) as total FROM pp_books
    >> WHERE 1 GROUP BY letter ORDER BY letter ASC;
    >>
    >>
    >>
    >> --
    >> Brian Wakem
    >> Email: http://homepage.ntlworld.com/b.wakem/myemail.png[/ref]
    >
    > Ugh... I am stuck again already.
    >
    > I would like to add another column, this one should could the books
    > where the column type is equal to '1'. I will also need another 4
    > columns each using a different type (1-5). I tried this for 1 column...
    >
    > SELECT LEFT( book_name, 1 ) AS letter, COUNT( * ) AS total, COUNT(
    > SELECT WHERE book_type = '1' ) AS retired
    > FROM pp_books
    > WHERE 1
    > GROUP BY letter
    > ORDER BY letter ASC
    > LIMIT 0 , 30;[/ref]


    Not entirely sure what you are trying to do here. Something like:

    SELECT SUBSTRING(book_name,1,1) AS letter,book_type,COUNT(*) as total FROM
    pp_books WHERE 1 GROUP BY letter,book_type ORDER BY letter ASC

    ?

    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
    Brian Guest

  9. #9

    Default Re: Obtaining the total number of items in a column by the item's first letter.


    Brian Wakem wrote: 
    > >
    > > Ugh... I am stuck again already.
    > >
    > > I would like to add another column, this one should could the books
    > > where the column type is equal to '1'. I will also need another 4
    > > columns each using a different type (1-5). I tried this for 1 column...
    > >
    > > SELECT LEFT( book_name, 1 ) AS letter, COUNT( * ) AS total, COUNT(
    > > SELECT WHERE book_type = '1' ) AS retired
    > > FROM pp_books
    > > WHERE 1
    > > GROUP BY letter
    > > ORDER BY letter ASC
    > > LIMIT 0 , 30;[/ref]
    >
    >
    > Not entirely sure what you are trying to do here. Something like:
    >
    > SELECT SUBSTRING(book_name,1,1) AS letter,book_type,COUNT(*) as total FROM
    > pp_books WHERE 1 GROUP BY letter,book_type ORDER BY letter ASC
    >
    > ?
    >
    > --
    > Brian Wakem
    > Email: http://homepage.ntlworld.com/b.wakem/myemail.png[/ref]

    Hi Brian.

    Sorry :(

    What I am looking for is something that will output a table a lot like
    this:

    +--------+-------+--------+--------+--------+--------+--------+
    | letter | total | type_1 | type_2 | type_3 | type_4 | type_5 |
    +--------+-------+--------+--------+--------+--------+--------+
    | A | 10 | 4 | 0 | 3 | 1 | 2 |
    +--------+-------+--------+--------+--------+--------+--------+
    | B | 28 | 19 | 1 | 4 | 1 | 3 |

    Daz Guest

  10. #10

    Default Re: Obtaining the total number of items in a column by the item's first letter.


    Daz wrote: 
    > >
    > >
    > > Not entirely sure what you are trying to do here. Something like:
    > >
    > > SELECT SUBSTRING(book_name,1,1) AS letter,book_type,COUNT(*) as total FROM
    > > pp_books WHERE 1 GROUP BY letter,book_type ORDER BY letter ASC
    > >
    > > ?
    > >
    > > --
    > > Brian Wakem
    > > Email: http://homepage.ntlworld.com/b.wakem/myemail.png[/ref]
    >
    > Hi Brian.
    >
    > Sorry :(
    >
    > What I am looking for is something that will output a table a lot like
    > this:
    >
    > +--------+-------+--------+--------+--------+--------+--------+
    > | letter | total | type_1 | type_2 | type_3 | type_4 | type_5 |
    > +--------+-------+--------+--------+--------+--------+--------+
    > | A | 10 | 4 | 0 | 3 | 1 | 2 |
    > +--------+-------+--------+--------+--------+--------+--------+
    > | B | 28 | 19 | 1 | 4 | 1 | 3 |[/ref]

    Using WHERE I don't seem to have any problems filtering the results.
    It's just getting the right results into the right columns, I am
    struggling with. :)

    Daz Guest

  11. #11

    Default Re: Obtaining the total number of items in a column by the item's first letter.


    "Daz" <com> schreef in bericht
    news:googlegroups.com... 
    >>
    >> Hi Brian.
    >>
    >> Sorry :(
    >>
    >> What I am looking for is something that will output a table a lot like
    >> this:
    >>
    >> +--------+-------+--------+--------+--------+--------+--------+
    >> | letter | total | type_1 | type_2 | type_3 | type_4 | type_5 |
    >> +--------+-------+--------+--------+--------+--------+--------+
    >> | A | 10 | 4 | 0 | 3 | 1 | 2 |
    >> +--------+-------+--------+--------+--------+--------+--------+
    >> | B | 28 | 19 | 1 | 4 | 1 | 3 |[/ref]
    >
    > Using WHERE I don't seem to have any problems filtering the results.
    > It's just getting the right results into the right columns, I am
    > struggling with. :)
    >[/ref]


    try:
    SELECT LEFT( book_name, 1 ) AS letter, COUNT( * ) AS total, SUM(IF(
    book_type = '1',1,0) ) AS retired
    FROM pp_books
    WHERE 1
    GROUP BY letter
    ORDER BY letter ASC
    LIMIT 0 , 30;


    suc6


    Luuk Guest

  12. #12

    Default Re: Obtaining the total number of items in a column by the item's first letter.


    Luuk wrote: 
    > >
    > > Using WHERE I don't seem to have any problems filtering the results.
    > > It's just getting the right results into the right columns, I am
    > > struggling with. :)
    > >[/ref]
    >
    >
    > try:
    > SELECT LEFT( book_name, 1 ) AS letter, COUNT( * ) AS total, SUM(IF(
    > book_type = '1',1,0) ) AS retired
    > FROM pp_books
    > WHERE 1
    > GROUP BY letter
    > ORDER BY letter ASC
    > LIMIT 0 , 30;
    >
    >
    > suc6[/ref]
    It works beautifully! Thanks Luuk. :)

    Daz Guest

Similar Threads

  1. Getting Total Item Count in Paging Datagrid
    By jason smith in forum ASP.NET Data Grid Control
    Replies: 6
    Last Post: October 28th, 07:26 PM
  2. Add Total of List Items
    By neilsytner in forum Coldfusion - Getting Started
    Replies: 5
    Last Post: June 29th, 01:34 PM
  3. total number of items in the datagrid control
    By buran in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: July 2nd, 06:52 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139