Page 1 of 2 12 LastLast
Results 1 to 10 of 13
  1. #1

    Default Need help regarding sql query


    I have this problem

    Vehicle:
    Motorcycle

    Engine:
    4-Stroke
    V-Twin


    Unson nako pag.gama na pag.retrieve kay maconcatenate kanang resulta sa Engine so the Result will be like this

    Vehicle | Engine
    Motorcycle | 4-Stroke, V-Twin

    instead of

    Vehicle | Engine
    Motorcycle | 4-Stroke
    Motorcycle | V-Twin

    Thanks mga master

  2. #2
    can you post the complete attributes? it is remarkably simple but I need to be certain with the solution.

  3. #3
    look up "JOIN" statements

  4. #4
    Vehicle has: VehicleID, Name
    Engine has: EngineID, Name
    VehicleEngine has: ID, VehicleID, EngineID
    ___

    And any join statement will just produce this

    Vehicle | Engine
    Motorcycle | 4-Stroke
    Motorcycle | V-Twin

  5. #5
    use the string functions and alias

  6. #6
    Try to use GROUP_CONCAT e.g.

    SELECT vehicle, GROUP_CONCAT(engine) from vehicle_table GROUP BY vehicle

  7. #7
    Sorry, MS SQL doesnt recognize that command. Any other ideas?

  8. #8
    Sorry I didnt recognize that you are using MSSQL. That only works on Mysql5.
    Anyway, heres the solution using MSSQL cursors

    BEGIN
    -- Start the cursor
    DECLARE _cursor CURSOR
    FOR
    SELECT Vehicle, Engine
    FROM VehicleTable
    -- Open the cursor
    OPEN _cursor
    -- Declare Local Variables
    DECLARE @Vehicle varchar(10)
    DECLARE @Engine varchar(10)
    -- Create temporary table
    CREATE TABLE #Result (
    Vehicle VARCHAR(10) NOT NULL,
    Engine VARCHAR(8000) NULL
    )
    -- Load the first row into the cursor

    FETCH NEXT FROM _cursor INTO @Vehicle, @Engine
    -- check for final row
    WHILE (@@FETCH_STATUS <> -1)
    BEGIN
    IF (@@FETCH_STATUS <> -2) -- check for error
    BEGIN
    IF EXISTS(SELECT * FROM #Result WHERE Vehicle = @Vehicle)
    BEGIN
    -- Row Exists so append result
    UPDATE #Result
    SET Engine = Engine + ',' + @Engine
    WHERE Vehicle = @Vehicle
    END
    ELSE
    BEGIN
    -- Row didn't exist so new record
    INSERT INTO #Result ( Vehicle, Engine )
    VALUES ( @Vehicle, @Engine )
    END
    END
    -- Load the next row into the cursor
    FETCH NEXT FROM _cursor INTO @Vehicle, @Engine
    END
    CLOSE _cursor
    DEALLOCATE _cursor
    -- Show results
    SELECT * FROM #Result
    -- Clean up
    DROP TABLE #Result
    END

  9. #9
    You need two things to achieve that:

    First create a function that concatenates the values of the engine:

    create function getEngineStr(@vehicleID as varchar(50)) --any name will do

    returns varchar (800)

    as

    Begin

    DECLARE @engine VARCHAR(8000)

    SELECT @engine = COALESCE(@engine + ', ', '') + cast(EngineID as varchar)

    FROM Engine

    where vehicleID=@vehicleID

    GROUP BY EngineID

    return @engine

    end


    --Second , you do the query

    Select Vehicle.Name,dbo.getEngineStr(Vehicle.vehicleid)as Engine from dbo.Vehicle

  10. #10
    to make to make two columns into one
    kani lang nga format gamita --> (table1.colA + ', ' + table2.colB) as newColumn

    SELECT (table1.colA + ', ' + table2.colB) as newColumn, table1.colC, table3.colD
    FROM ....the rest of the sql statement

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. need help...regarding my GR dog
    By death_strike101 in forum Pet Discussions
    Replies: 4
    Last Post: 01-10-2009, 12:24 AM
  2. need help regarding core 2 duo temp
    By salibongkogon in forum Computer Hardware
    Replies: 3
    Last Post: 05-12-2008, 02:58 AM
  3. Need Help regarding Credit Cards...
    By evilbugoy in forum General Discussions
    Replies: 11
    Last Post: 04-18-2008, 04:49 AM
  4. need help regarding working capital requirement
    By progmetal in forum Business, Finance & Economics Discussions
    Replies: 2
    Last Post: 03-01-2008, 10:32 PM
  5. I need help regarding 5.1 speaker system...
    By whoopz in forum Computer Hardware
    Replies: 21
    Last Post: 06-01-2006, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top