Jump to content



Photo

SQL newbie question :)


  • Please log in to reply
9 replies to this topic

#1 +Graimer

Graimer

    Neowinian

  • Joined: 08-October 04
  • Location: Norway
  • OS: Win8.1
  • Phone: iPhone 5

Posted 06 September 2012 - 15:27

Hey. I'm new(started today) With SQL for a school assignment(bad teacher :p ) and I've already met a problem. The task is to take multiple Excel files and create a normalized sql db. Now here's my problem:

I have one table importedOrders which includes the varchar Fields "FirstName" and "LastName" that indicates the employee(!) that is responsible. I also have a table importedEmployees With a EmployeeID, FirstName and LastName. I'm migrating some data from the importedTables into some normalized tables. The table Orderline will need to have an employeeID Foreign key(primary key is in a new Employee table) and in my migration-query, I need to find the right employeeID based on the FirstName and LastName. I got the following:

INSERT INTO Orderline (ProductID, Quantity, .... , EmployeeID)
SELECT ...., (SELECT EmployeeID FROM dblab2.importEmployee E WHERE importOrders.FirstName = E.FirstName AND importOrders.LastName = E.LastName)


Is this right(can't test it right now) or do I need to join the tables(which kind of join?)? Am I even close? :p Would appreciate all help I can get.


#2 Kami-

Kami-

    ♫ d(-_-)b ♫

  • Tech Issues Solved: 2
  • Joined: 28-July 08
  • Location: SandBox

Posted 06 September 2012 - 17:02


INSERT INTO OrderLine (ProductID, Quantity, ...., EmployeeID)

SELECT

  importOrders.ProductID

,importOrders.Quantity

,....

,importEmployee.EmployeeID

FROM importOrders

INNER JOIN importEmployee

ON importOrders.FirstName = importEmployee.FirstName

AND importOrders.LastName = importEmployee.LastName



#3 OP +Graimer

Graimer

    Neowinian

  • Joined: 08-October 04
  • Location: Norway
  • OS: Win8.1
  • Phone: iPhone 5

Posted 06 September 2012 - 18:29

INSERT INTO OrderLine (ProductID, Quantity, ...., EmployeeID)
SELECT
  importOrders.ProductID
,importOrders.Quantity
,....
,importEmployee.EmployeeID
FROM importOrders
INNER JOIN importEmployee
ON importOrders.FirstName = importEmployee.FirstName
AND importOrders.LastName = importEmployee.LastName

Thanks. Works perfect :) Guess I gotta start Reading about all different joins. Is there a way to tell when you need to use subquery and when to use join?

#4 Kami-

Kami-

    ♫ d(-_-)b ♫

  • Tech Issues Solved: 2
  • Joined: 28-July 08
  • Location: SandBox

Posted 06 September 2012 - 19:43

Many reasons; performance is a big concern for me as to if I use a join, a derived (sub) query, temporary table or CTE.

#5 OP +Graimer

Graimer

    Neowinian

  • Joined: 08-October 04
  • Location: Norway
  • OS: Win8.1
  • Phone: iPhone 5

Posted 08 September 2012 - 22:46

OK, I've got another followup question here if you(or anyone else) got time :) How can I Group multiple records to one(per Group)? What I'm gonna do is take all orderlines, and create an 'Order' table for them. But before I could add en OrderID Foreign key to the 'orderline' table I Guess I have to export all orders to a 'Order' table. All orderlines for the same order has identical values in orderdate, buyername, freight(that should be enough i Guess). Is it possible to make a Query that Groups all orderlines that belong together so I could copy out the identical parts of the orderlines to make a record in the new 'Order' table?

Would appreciate all help I could get :) At least a Clue that tells me what to Google :p

EDIT: Is it as easy as this?:

SELECT Buyername, freight, ..... , OrderDate
FROM Orderlines
GROUP BY OrderDate, Buyername, freight
ORDER BY OrderDate;

And is "order by" required by MySQL? I think I read somewhere that the order of Your "Group By" values would determine how it was sorted. Btw, sorry for these dumb questions. Doing this without the sql db since the only copy is at School.

#6 Kami-

Kami-

    ♫ d(-_-)b ♫

  • Tech Issues Solved: 2
  • Joined: 28-July 08
  • Location: SandBox

Posted 08 September 2012 - 23:50

OK, I've got another followup question here if you(or anyone else) got time :) How can I Group multiple records to one(per Group)? What I'm gonna do is take all orderlines, and create an 'Order' table for them. But before I could add en OrderID Foreign key to the 'orderline' table I Guess I have to export all orders to a 'Order' table. All orderlines for the same order has identical values in orderdate, buyername, freight(that should be enough i Guess). Is it possible to make a Query that Groups all orderlines that belong together so I could copy out the identical parts of the orderlines to make a record in the new 'Order' table?

Would appreciate all help I could get :) At least a Clue that tells me what to Google :p

EDIT: Is it as easy as this?:

SELECT Buyername, freight, ..... , OrderDate
FROM Orderlines
GROUP BY OrderDate, Buyername, freight
ORDER BY OrderDate;

And is "order by" required by MySQL? I think I read somewhere that the order of Your "Group By" values would determine how it was sorted. Btw, sorry for these dumb questions. Doing this without the sql db since the only copy is at School.


That looks about right; alternatively you can use a SELECT DISTINCT to return the columns you need only once per matching set.

#7 OP +Graimer

Graimer

    Neowinian

  • Joined: 08-October 04
  • Location: Norway
  • OS: Win8.1
  • Phone: iPhone 5

Posted 09 September 2012 - 00:03

Nice. :) What do you mean? Isn't the point of Group by to just return one record for each order(matching set).Or did you mean that "SELECT DISTINCT OrderDate, Buyername" would return the same as "Group by"?

EDIT: Oh, right.. I think I understand what you said. Since I just need to remove duplicate rows, I could use Select distinct instead, right? I thoght that would just return a buyername one time(even it it had multiple orderdates), but I Guess that was wrong :p

While I got you here: is this solution the best for my problem? To first export/migrate orderlines to orders and then use the same Query as With employees to link up orderid after, or is there a better solution?

#8 Kami-

Kami-

    ♫ d(-_-)b ♫

  • Tech Issues Solved: 2
  • Joined: 28-July 08
  • Location: SandBox

Posted 09 September 2012 - 00:31

SELECT DISTINCT <fields> - would return all unique combinations of the fields selected.

#9 OP +Graimer

Graimer

    Neowinian

  • Joined: 08-October 04
  • Location: Norway
  • OS: Win8.1
  • Phone: iPhone 5

Posted 09 September 2012 - 00:57

Awesome. This is actually not as hard as it seemed. If only my teacher made SQL sound so simple(yet Advanced and powerful) ^^
Thanks alot for all the help.

#10 Kami-

Kami-

    ♫ d(-_-)b ♫

  • Tech Issues Solved: 2
  • Joined: 28-July 08
  • Location: SandBox

Posted 09 September 2012 - 21:56

I'm a Senior Developer; I mentor newer developers all the time, so I'm used to making people understand :) - If you have any questions going forward; just let me know I review my messages on here at least 3-4 times a week.