#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Problem with multiple queries from single table


    Hello everyone!

    I'm pretty familliar with basics od SQL but this task gives me some hard time. I've still haven't mastered all possible commands and possibilities and that my biggest problem at the moment.

    I have an original table with multiple columns (about 15 of them) and I need data from five or six of this columns but through several filters and conditions. Here's a example with columns I need:

    RPT_ID TOT_AMT_KN NO_UNITS PACKAGE_TYPE AR_PPS_TP_GRP_DSC
    RCDF_CARDIF_KRE 10020.94 1 KRATKOROČNI
    RCDF_CARDIF_KRE 22268.76 2 KRATKOROČNI
    RCDF_CARDIF_KRE 9427.10 1 GOTOVINSKI
    RCDF_CARDIF_KRE 3191.85 1 KRATKOROČNI
    RCDF_CARDIF_KRE 31918.54 1 GOTOVINSKI
    RCDF_CARDIF_KRE 29691.67 1 GOTOVINSKI
    RCDF_CARDIF_FOS 0.00 0 B GOTOVINSKI
    RCDF_CARDIF_FOS 96376.25 1 B GOTOVINSKI
    RCDF_CARDIF_FOS 56501.50 1 B GOTOVINSKI
    RCDF_CARDIF_FOS 45349.20 1 B GOTOVINSKI
    RCDF_CARDIF_FOS 210269.74 1 B GOTOVINSKI
    RCDF_CARDIF_FOS 132342.16 1 B GOTOVINSKI
    RCDF_CARDIF_FOS 0.00 0 B GOTOVINSKI
    RCDF_CARDIF_FOS 369827.30 1 B STAMBENI

    The key is that I need to segment the data into two groups by RPT_ID column. But there's a problem that some corresponding data in other columns for one value of RPT_ID (RCDF_CARDIF_FOS) exist, and for other value (RCDF_CARDIF_KRE) doesn't. For example - RPT_ID values ending in FOS have package_type values A, B or C and RPT_ID values ending in KRE don't have any value in package_type column.

    Still, they need to be represented in single resulting table as shown in this screenshot from excel:

    That's my first problem - how to segment the data for two values that exists in one column.

    Second problem is how can I write a SELECT that will select some data from one column under one condition and put it one new resulting column. After that, I also need to select the same original column under second condition and put that data in another resulting column.

    Hope you can help with some input, I know it's a lot of garbled data.
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,261
    Rep Power
    4279
    Originally Posted by marky1607
    The key is that I need to segment the data into two groups by RPT_ID column.
    use ORDER BY rpt_id

    this will ensure that all rows with the same value in the rpt_id column are grouped together
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

IMN logo majestic logo threadwatch logo seochat tools logo