tags:

views:

30

answers:

1

i have following 3 tables

CREATE TABLE [dbo].[dspartner](
[dspartnerid] [bigint] IDENTITY(1,1) NOT NULL,
[name] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[priority] [int] NULL)

CREATE TABLE [dbo].[hotels](
[hotelid] [int] PRIMARY KEY  IDENTITY(1,1) NOT NULL,
[name] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[DSPartnerid] [bigint] NULL,
)

CREATE TABLE [HotelSourceMap](
[hotelsourcemapid] [bigint] PRIMARY KEY  IDENTITY(1,1) NOT NULL,
[dspartnerid] [bigint] NOT NULL,
[hotelid] [bigint] NOT NULL,
    [dshotelid] bigint
)

situation is like this:

i want to table:dspartner contain details about datasource partner. table:hotels contain details of hotels table:HotelSourceMap contain record of hotels.hotelid, dsparnter.dspartnerid

new i want to update hotels table so that set hotels.dspartner = hotelsourcemap.dspartnerid where hotels.hotelid = hotelsourcemap.hotelid and hotelsourcemap.dspartnerid = dspartner.dspartnerid (where dspartnerid's priorirty is high)

Note:-priority contain int value and the max int value will be considerd as max priority. if two dspartner have same priority then get any one of them

sql server 2005

+1  A: 

Use:

UPDATE HOTELS
   SET dspartners = (SELECT x.dspartnerid
                       FROM (SELECT hsm.dspartnerid,
                                    ROW_NUMBER() OVER(PARTITION BY hsm.hotelid 
                                                      ORDER BY p.priority, p.name DESC) AS rank
                               FROM HOTELSOURCEMAP hsm
                               JOIN DSPARTNER p ON p.dspartnerid = hsm.dspartnerid
                              WHERE hsm.hotelid = hotelid) x
                      WHERE x.rank = 1)

To make sure things are working properly, run:

SELECT x.dspartnerid
  FROM (SELECT hsm.dspartnerid,
               ROW_NUMBER() OVER(PARTITION BY hsm.hotelid 
                                 ORDER BY p.priority, p.name DESC) AS rank
          FROM HOTELSOURCEMAP hsm
          JOIN DSPARTNER p ON p.dspartnerid = hsm.dspartnerid
         WHERE hsm.hotelid = ?) x
 WHERE x.rank = 1

Replace the ? with any hotelid of your choice.
It will only return one dspartnerid value, even if there were more than one dspartnerid's with the same high priority.

OMG Ponies
getting this error:Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Rajesh Rolen- DotNet Developer
and in query where we are joining Hotels.hotelid = Hotelsourcemap.hotelid ?????
Rajesh Rolen- DotNet Developer
Doesn't make sense - did you forget the `WHERE x.rank = 1` ?
OMG Ponies
Re: correlation, see `WHERE hsm.hotelid = hotelid`, right after the JOIN...
OMG Ponies
ya right.... but its giving me error
Rajesh Rolen- DotNet Developer
i think is should be like this, but still error:UPDATE HOTELS SET dspartnerid = a.dspartneridfrom (SELECT x.dspartnerid,hsm.hotelid FROM (SELECT hsm.dspartnerid,hsm.hotelid ROW_NUMBER() OVER(PARTITION BY hsm.hotelid ORDER BY p.priority, p.name DESC) AS rank FROM HOTELSOURCEMAP hsm JOIN DSPARTNER p ON p.dspartnerid = hsm.dspartnerid) x WHERE x.rank = 1 ) AS a WHERE a.hotelid = hotels.hotelid
Rajesh Rolen- DotNet Developer
I want to update complete table:hotels.. not one record..
Rajesh Rolen- DotNet Developer
There is no `SET dspartnerid = a.dspartnerid FROM (SELECT...` - that's invalid syntax.
OMG Ponies
I understand you want to update the entire `HOTELS` table; I'm trying to demonstrate to you that the error you claim doesn't make any sense based on my answer based on the information you provided. The correlation is what enables updating all `HOTEL` records.
OMG Ponies
i applied your answer and it updated all 121500 records of my tables because we dont have any were clouse in it.. i want to update only those record which have hotels.hotelid = hotelsourcemap.hotelid
Rajesh Rolen- DotNet Developer
please give me solution
Rajesh Rolen- DotNet Developer
Because of the correlation, `HOTEL` records that do not have entries in HOTELSOURCEMAP will have a NULL `dspartnerid`. You can back that up by running: `SELECT h.hotelid FROM HOTELS h WHERE h.hotelid NOT IN (SELECT DISTINCT hsm.hotelid FROM HOTELSOURCEMAP hsm)` ...to list all the hotels that don't have `HOTELSOURCEMAP` records.
OMG Ponies
its done.. thanks lot:WITH hs AS (SELECT x.dspartnerid,x.hotelid FROM (SELECT hsm.dspartnerid,hsm.hotelid, ROW_NUMBER() OVER(PARTITION BY hsm.hotelid ORDER BY p.priority DESC) AS rank FROM HOTELSOURCEMAP hsm JOIN DSPARTNER p ON p.dspartnerid = hsm.dspartnerid ) x WHERE x.rank = 1)UPDATE hotels SET DSPartnerid = hs.dspartnerid FROM hs where hs.hotelid = hotels.hotelid and hotels.dspartnerid is null
Rajesh Rolen- DotNet Developer