SQL Server - Select with insert

Asked By Byron King on 08-Sep-06 05:42 PM
Hi.  I have 3 tables:

Table1: AccountInfo
AccountInfoID    bigint   <-- primary key
AccountNumber    char
PostingDate    datetime
TransactionReferenceNumber    char
SequenceNumber    numeric

Table2: LodgingSummary
LodgingSummaryID    bigint
AccountInfoID    bigint 
<-- foreign key
LoadTransactionCode    tinyint
NoShowIndicator    decimal
CheckInDate    datetime
DailyRoomRate    decimal
TotalOtherCharges    decimal

Table3: Load_LodgingSummary
LodgingSummaryID    bigint
LoadTransactionCode    tinyint
AccountNumber    varchar
PostingDate    datetime
TransactionReferenceNumber    varchar
SequenceNumber    numeric
NoShowIndicator    numeric
CheckInDate    datetime
DailyRoomRate    numeric
TotalOtherCharges    numeric

I need to insert the AccountInfoID from AccountInfo along with LodgingSummaryID, LoadTransactionCode,  NoShowIndicator, CheckInDate, DailyRoomRate, TotalOtherCharges from Load_LodgingSummary into the LodgingSummary table (which will be empty from the start).

I have devised the following query:

[CODE]
set identity_insert LodgingSummary on
insert into LodgingSummary
(
LodgingSummaryID,
AccountInfoID,
LoadTransactionCode,
NoShowIndicator,
CheckInDate,
DailyRoomRate,
TotalOtherCharges
 )
select
LodgingSummaryID,
 (select min(ai.AccountInfoID) from AccountInfo ai
    where ai.AccountInfoID not exists (select AccountInfoID from LodgingSummary l
        where l.AccountInfoID= ai.AccountInfoID)) as AccountInfoID,
LoadTransactionCode,
NoShowIndicator,
CheckInDate,
DailyRoomRate,
TotalOtherCharges,
TotalTaxAmount
from Load_LodgingSummary
set identity_insert lodgingsummary off
[/CODE]

When I run the query, I only get the first AccountInfoID from AccountInfo.  The data in LodgingSummary looks like (table shortened for brevity):

LodgingSummaryID   AccountInfoID   LoadTransactionCode
    1                              1                     4
    2                              1                     4
    3                              1                     4
    4                              1                     4
etc...

I want
LodgingSummary to look like:
LodgingSummaryID   AccountInfoID   LoadTransactionCode
    1                              1                     4
    2                              2                     4
    3                              3                     4
    4                              4                     4
etc...

How do I fix the subquery in the select statement (in red above) to get what I want?  Thanks!

check the updated query

K Pravin Kumar Reddy replied to Byron King on 09-Sep-06 12:14 AM

hello

[CODE]
set identity_insert LodgingSummary on
insert into LodgingSummary
(
LodgingSummaryID,
AccountInfoID,
LoadTransactionCode,
NoShowIndicator,
CheckInDate,
DailyRoomRate,
TotalOtherCharges
 )
select
LodgingSummaryID,
 (select min(ai.AccountInfoID) from AccountInfo ai
    where ai.AccountInfoID not exists (select AccountInfoID from LodgingSummary l
        where l.AccountInfoID= ai.AccountInfoID)) as AccountInfoID,
LoadTransactionCode,
NoShowIndicator,
CheckInDate,
DailyRoomRate,
TotalOtherCharges,
TotalTaxAmount
from Load_LodgingSummary
set identity_insert lodgingsummary off
[/CODE]

coz of min(ai.AccountInfoID) this ur query ur getting always 1 ..

OK, how do I fix it?

Byron King replied to K Pravin Kumar Reddy on 10-Sep-06 08:16 PM
I know why I'm only getting "1", but how do I fix that?

updated SQl query

K Pravin Kumar Reddy replied to Byron King on 11-Sep-06 12:58 AM

hello

try this one

[CODE]
set identity_insert LodgingSummary on
insert into LodgingSummary
(
LodgingSummaryID,
AccountInfoID,
LoadTransactionCode,
NoShowIndicator,
CheckInDate,
DailyRoomRate,
TotalOtherCharges
 )
select
LodgingSummaryID,
 (select ai.AccountInfoID from AccountInfo ai
    where ai.AccountInfoID not exists (select AccountInfoID from LodgingSummary l
        where l.AccountInfoID= ai.AccountInfoID)) as AccountInfoID,
LoadTransactionCode,
NoShowIndicator,
CheckInDate,
DailyRoomRate,
TotalOtherCharges,
TotalTaxAmount
from Load_LodgingSummary
set identity_insert lodgingsummary off
[/CODE]

Thanks, Praveen but I don't think this will work...
Byron King replied to K Pravin Kumar Reddy on 11-Sep-06 10:02 AM
I can see right away that this:

(select ai.AccountInfoID from AccountInfo ai
    where ai.AccountInfoID not exists (select AccountInfoID from LodgingSummary l
        where l.AccountInfoID= ai.AccountInfoID)) as AccountInfoID,

won't work because there are many AccountInfoID's in the table.  I need to return only the lowest unique
AccountInfoID from the AccountInfo table that doesn't yet exist in the LodgingSummary table.  I may have to either use a cursor or .NET code to insert the rows line-by-line.
updated one
K Pravin Kumar Reddy replied to Byron King on 11-Sep-06 10:57 PM

hello

according to the given information ur AccountInfoId is promarykey right and ur checking accountinfoid according to some condition

select AccountInfoID from LodgingSummary l
        where l.AccountInfoID= ai.AccountInfoID

put SELECT Distinct(AccountInfoId from LodgingSummary l
        where l.AccountInfoID= ai.AccountInfoID

Here's the answer...
Byron King replied to K Pravin Kumar Reddy on 12-Sep-06 08:41 AM
select min(ai.AccountInfoID) from AccountInfo ai
where not exists (select AccountInfoID from LodgingSummary l
    where l.AccountInfoID = ai.AccountInfoID)