Getting the count from two tables as rows not columns.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • new2sql
    New Member
    • Apr 2009
    • 9

    #1

    Getting the count from two tables as rows not columns.

    I am building a site that uses a redemption card system and thus need to display some redemption data in a chart on an ASPX page. The chart needs the data in the following format.

    Status | Count
    -------------------------------------------------
    NotReemed | 25000
    Redeemed | 14000

    I am using currently using the following SQL Statement to obtain the data

    ---
    SELECT
    NotRedeemed =(SELECT count(*)
    FROM tbl_codes INNER JOIN
    tbl_batches ON tbl_codes.Batch _ID = tbl_batches.Bat ch_ID
    WHERE (tbl_codes.IsUs ed = 0 AND LEFT(tbl_codes. Redeem_Code_Pre fix = 'WHATEVER')

    Redeemed =(SELECT count(*)
    FROM tbl_fans
    WHERE LEFT(Redeem_Cod e_Prefix = 'WHATEVER'
    ---
    Using this obtains the correct data but the data is obviously in the following format:

    NotRedeemed | Redeemed
    ------------------------------------------------
    25000 | 14000

    If someone could give me an idea of how to get the data in the correct format (as per the first example) it would be much appriciated.
  • gpl
    New Member
    • Jul 2007
    • 152

    #2
    Try something like this

    Code:
    Select 'NotRedeemed' As [Status],
           (Select Count(*)
            From   Tbl_codes
                   Inner Join Tbl_batches
                     On Tbl_codes.Batch_id = Tbl_batches.Batch_id
            Where  Tbl_codes.Isused = 0
                   And Left(Tbl_codes.Redeem_code_prefix, 8) = 'WHATEVER') As [count]
    Union All
    Select 'Redeemed' As [Status],
           (Select Count(*)
            From   Tbl_fans
            Where  Left(Redeem_code_prefix, 8) = 'WHATEVER') As [count]

    Comment

    • new2sql
      New Member
      • Apr 2009
      • 9

      #3
      Thanks heaps, that is exactly what I was looking for:)

      Comment

      Working...