Sum and Group problem with MSSQL

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Sum and Group problem with MSSQL

    I do a quiet complex query based on users form input. The result looks
    something like this (simplified, irrelevant data and fields truncated)

    cat_id | producttext
    -------------------------------------
    1002 | Parma Schinken
    1013 | Bordeaux
    5006 | Parma Schinken
    1005 | Something
    1002 | Another Product from Kat 1002

    As you can see the first and last record have the same cat_id.

    What I want in the end is to know the percentages of products in the same
    range (1001-2000, 2001-3000...)
    and the percentage of products within the same cat (like 1002)

    I do it simply by doing a subquery on each range, see attached code (query1).
    The problem is I don't only need the cat_id but it's language dependent name.

    I can get it with query like this (see query2) but, it wont function if the
    FROM is another query. It only works if I Select from a real table.

    Any hints what to do different?

    What I actually want in the end is a table like this:

    2 1002 Fleisch
    1 1005 Beilagen, Pasta, Reis, Konserven
    1 1013 Anderes








    -- query1 --
    SELECT COUNT(*) as Anzahl,cat_id
    FROM qgetAuswertung1
    WHERE cat_id > 1000 AND cat_id< 2000
    GROUP BY cat_id

    -- query2 --
    SELECT COUNT(*) AS Anzahl,
    qgetAuswertung1.users_statdata_productsubcategorie ID,

    tCmsElementCustomLieblingsproduktProductSubcategor ie.productsubcategorie_descrip
    tion_DE
    FROM qgetAuswertung1 INNER JOIN
    tCmsElementCustomLieblingsproduktProductSubcategor ie ON
    qgetAuswertung1.users_statdata_productsubcategorie ID =
    tCmsElementCustomLieblingsproduktProductSubcategor ie.productsubcategorieID
    WHERE (qgetAuswertung1.users_statdata_productsubcategori eID > 1000) AND
    (qgetAuswertung1.users_statdata_productsubcategori eID <
    2000)
    GROUP BY qgetAuswertung1.users_statdata_productsubcategorie ID,

    tCmsElementCustomLieblingsproduktProductSubcategor ie.productsubcategorie_descrip
    tion_DE

    zu Guest

  2. Similar Questions and Discussions

    1. table alias mssql problem
      I get this error: The column prefix 'F' does not match with a table name or alias name used in the query. with the query below Any ideas as...
    2. MSSQL Stored Procedure Problem
      I am trying to pass some variables into a MSSQL stored procedure and for some reason it is truncating the last variable. If I move the order of the...
    3. Problem with MSSQL connection and PHP
      I've written a PHP file to connect to an SQL database. When I use the mssql_connect function, I get this error: Warning: mssql_connect(): Unable...
    4. [PHP - MSSQL]: Problem with mssql_connect
      Please help!... I have to use 'apache+php' connect to MSSQL Server 2000 in Windows Server 2003, but there are problem. Computer A: Windows...
    5. Problem with mssql driver
      Hi Guillaume! On Sun, 24 Aug 2003 10:38:30 +0200, "Guillaume" <nospam_guillaume.rouchon@free.fr> wrote: What is the output of mssql_error()...
  3. #2

    Default Re: Sum and Group problem with MSSQL

    You can't use the keyword join in Q of Q. You have to do it the old fashioned way.

    select somefields
    from query1, query2
    where query1.field = query2.field
    Dan Bracuk Guest

  4. #3

    Default Re: Sum and Group problem with MSSQL

    This have I done.
    I actually didi the Join in the 'base'-query.

    Thanks

    zu Guest

Posting Permissions

  • You may not post new threads
  • You may 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