0

I am trying to get this query to work

INSERT INTO [ImportedDateRange] ([DataSet],[DateRange]) 
'webshop', (select DISTINCT cast([OrderCreatedDate] as DATE) from webshop)

I basically want it to look something like this:

DataSet     DateRange
webshop     01/10/2013
webshop     02/10/2013
webshop     03/10/2013
webshop     03/10/2013

where webshop is entered each time but each date range is copied over in to a new row.

Also to check with the DateRange records for DataSet webshop already exist

thanks for any help and advice

2
  • 1
    Make sure you put all your requirements in the question... not as comments to answers!! So that everyone can see :) Commented Dec 18, 2013 at 9:43
  • sorry, will do in future, thank you for your help Commented Dec 18, 2013 at 10:04

4 Answers 4

2

To insert unique DateRange records for DataSet webshop in [ImportedDateRange] table from webshop table write as:

INSERT INTO [ImportedDateRange] 
([DataSet],[DateRange]) 
select DISTINCT 'webshop', cast(T2.[OrderCreatedDate] as DATE) from webshop T2
WHERE NOT EXISTS (select 1 from [ImportedDateRange] T1 where T1.[DateRange] = T2.[OrderCreatedDate])
Sign up to request clarification or add additional context in comments.

3 Comments

This query will only insert unique records in ImportedDateRange table from webshop table which are not already in ImportedDateRange table. This is what the user wants...
@AntoRajaPrakash: OP states this requirement on one of the comment not in the question!
'INSERT INTO [ImportedDateRange] ([DataSet],[DateRange]) select DISTINCT 'webshop', cast(T2.[OrderCreatedDate] as DATE) from webshop T2 left join [ImportedDateRange] T1 on t2.[OrderCreatedDate] = t1.[DateRange] where t1.[DateRange] is null ' check this too an alternate with improved performance
2
INSERT INTO [ImportedDateRange] ([DataSet],[DateRange]) 
SELECT DISTINCT  'webshop', CAST([OrderCreatedDate] as DATE) 
FROM   webshop

5 Comments

another quick question, would you know how to get it to check with the DateRange records for DataSet webshop already exist?
Sorry I don't understand what you are asking?
if I ran that query twice it would insert the same data twice, any way to prevent this?
INSERT INTO [ImportedDateRange] ([DataSet],[DateRange]) SELECT DISTINCT 'webshop', CAST([OrderCreatedDate] as DATE) FROM webshop WHERE DateRange NOT IN (SELECT DISTINCT CAST([OrderCreatedDate] as DATE) FROM webshop)
@KevinHolditch: No, you need to put a where condition on the select statement against the same table to ignore the existing values
1
INSERT INTO [ImportedDateRange] 
([DataSet],[DateRange]) 
select DISTINCT 'webshop', cast([OrderCreatedDate] as DATE) from webshop

Comments

1

'webshop' should be within the SELECT, try running the select on it's own as the output will be the same as will be inserted into the ImportedDateRange table.

INSERT INTO [ImportedDateRange] 
([DataSet], [DateRange]) 
select DISTINCT 'webshop', cast([OrderCreatedDate] as DATE) from webshop;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.