TSQL help - merging three none-related table together

I'm looking to return one tsql statement, that contains four fields, from three separate, unrelated tables.

One table contains a list of objects, say fruits, and for each fruit, I want a sell buy date and best before date.

First statement would therefore look something like:

    select fruit from fruit table -- this returns multiple rows

Second statement would look something like:

    select sellbuyDate from sellTable -- this returns a single row

and the third would look something like:

    select bestbefore from bestTable -- this returns a single row

Don't get to hung up on the table names. I'm working on a legacy system, that we cant change, so need to combine the three table into one.

The underlining table needs to have all the fields returned in a single row, with the second and third results applied to the first statement.

Apples | 12-12-2008 | 12-12-2009

Pears  | 12-12-2008 | 12-12-2009

I've implemented the following temp table:

    CREATE TABLE #Fruits
    (
    Fruit VARCHAR(100),
    SellBuyDate DATETIME,
      BestBefore DATETIME
    )
    INSERT INTO #Fruits
       SELECT Fruit from fruit
    INSERT INTO #Fruits
      SELECT sellbuyDate from sellTable
    INSERT INTO #Fruits
      SELECT bestbefore from bestable
    SELECT * from #Fruits

This throws an error, because each insert doesn't contain the three fields specified.

any other suggestions would be well received.
LVL 1
Wayne AthertonProduct Analyst Financial Messaging LondonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
If you want every row joined to every other row, you can use a CROSS JOIN.  If you want to join to just the first row, you can use CROSS APPLY.  Here's the CROSS JOIN version:

SELECT f.Fruit, sd.sellbuyDate, bb.bestbefore
FROM fruit f
CROSS JOIN sellbuyDate sd
CROSS JOIN bestbefore bb

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wayne AthertonProduct Analyst Financial Messaging LondonAuthor Commented:
Hi ScottPletcher,

Is the cross joins the respective additional selects?
Scott PletcherSenior DBACommented:
One SELECT with CROSS JOINs will return the data results you said you wanted.  You can still INSERT them into another table if you need to.

INSERT INTO <table_name> ( <column_name>, ... )
SELECT f.Fruit, sd.sellbuyDate, bb.bestbefore
FROM fruit f
CROSS JOIN sellbuyDate sd
CROSS JOIN bestbefore bb
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

mankowitzCommented:
create table fruits_orig (
  fruit varchar(100)
);
insert into fruits_orig (fruit)
values ('apple'), ('pear'), ('radish');

create table sellbuydate_orig (
  sellbuydate datetime
);
insert into sellbuydate_orig (sellbuydate)
values ('2015-03-15');

create table bestbefore_orig (
  bestbefore datetime
);
insert into bestbefore_orig (bestbefore)
values ('2015-03-19');
 
CREATE TABLE Fruits
(
  Fruit VARCHAR(100),
  SellBuyDate DATETIME,
  BestBefore DATETIME
);
   
INSERT INTO Fruits (fruit, sellbuydate, bestbefore)
SELECT f.fruit, s.sellbuydate, b.bestbefore
      from fruits_orig f
       join sellbuydate_orig s
       join bestbefore_orig b;
       
select * from fruits;
mankowitzCommented:
Oops. I was working on answer when @scottpletcher posted.
Wayne AthertonProduct Analyst Financial Messaging LondonAuthor Commented:
I see, so create separate tables then Cross JOIN them all together.

makes sense - time to try it out.
Wayne AthertonProduct Analyst Financial Messaging LondonAuthor Commented:
Thanks for the help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.