1

I have two tables

Table1

StudentID   FullName    HoursLogged
1           Mark Smith       1
2           Jack Lantern     2
3           Pink Hotdog      3

Table2

StudentID   FirstName   LastName
1              Mark      Smith
2              Jack     Lantern
3              Pink     Hotdog
4              Mike     Cantu
5              Jake     Myers
6              Nathan   Cheese
7              Peter    Jackson

How can I make the table like this? With 0 in logged hours if the user has no hours recorded?

StudentID   FullName    HoursLogged
1           Mark Smith             1
2           Jack Lantern           2
3           Pink Hotdog            3
4           Mike Cantu             0
5           Jake Myyers            0
6           Nathan Cheese          0
7           Peter Jackson          0

Any help would be much appreciated. Thanks.

vanillacoke9191
  • 175
  • 2
  • 2
  • 13
  • 3
    Please specify database type like MSSQL, MySQL, Access, SQLite, Oracle, PostgreSQL because of small SQL syntax differences and SQL function support. – Jan Sep 22 '17 at 05:36
  • Your table design is a bit odd. Don't store the names twice. – jarlh Sep 22 '17 at 06:45

4 Answers4

2

You can perform outer join and replace null values by 0 that is the way you can easily achieve this, below is example of outer join

SELECT t2.StudentID ,t2.FirstName + ' '+ t2.LastName as FullName
       ISNULL(HoursLogged,0) as HoursLogged,  
FROM Table2 t2
LEFT OUTER Table1 t1 
ON t1.studentID = t2.studentID
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • it's telling me studentid is ambigious? – vanillacoke9191 Sep 22 '17 at 05:17
  • @vanillacoke9191 - do like this t2.StudentID...i updated my answer – Pranay Rana Sep 22 '17 at 05:25
  • it's also giving me a "table2" is not a recognized join option for some reason :( – vanillacoke9191 Sep 22 '17 at 05:28
  • @vanillacoke9191 - it should as join is supported by all database . ...i suggest have look to name of table and look to basic tutorial of joining if you have problem with sql...w3schools you can have basic tutorial – Pranay Rana Sep 22 '17 at 06:46
  • Another product specific answer to a question with no dbms specified... – jarlh Sep 22 '17 at 06:46
  • @jarlh - its about sql so I written in my answer its example and as i think joins it supported by all database and another thing is I just want to tell OP to make use of outer join – Pranay Rana Sep 22 '17 at 06:48
  • The join is fine, but the `+` for concatenation and ISNULL are both product specific. ANSI SQL has `||` and `COALESCE()`. – jarlh Sep 22 '17 at 06:53
  • @jarlh - yes i do understand that ..but my point in answer is make use of join and change things according to database OP use.. that is why i written its example not actual correct answer ...Apologies if its created confusion – Pranay Rana Sep 22 '17 at 06:56
  • No problem at all, I'm not getting confused, and hopefully not OP either now. – jarlh Sep 22 '17 at 07:00
1

Try this Query:

select b.StudentID ,concat( FirstName,' ', LastName), ISNULL(HoursLogged,0) 
as HoursLogged FROM Table1 a LEFT OUTER Table2 b ON a.StudentID = b.StudentID;
santho
  • 366
  • 2
  • 16
  • it's giving me a concat_ws is not a regonized built in function name :( – vanillacoke9191 Sep 22 '17 at 05:21
  • 2
    concat_ws is a SQL function which is only supported by MySQL https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat-ws and PostgreSQL https://www.postgresql.org/docs/9.1/static/functions-string.html I think @santho is giving a MySQL example. – Jan Sep 22 '17 at 05:40
  • 1
    Well, then simply use `concat(firstName,' ',lastName)` instead. – Carsten Massmann Sep 22 '17 at 05:45
  • @jarlh: `concat()` works in SQL-Server, MySQL, MariaDB, Oracle and PostgreSQL. I personally have not worked with all of them. Maybe you can tell me which RDBMS does not have a `concat()` function? OP still has not unveiled which product he is using, so we can only guess ... – Carsten Massmann Sep 22 '17 at 07:34
  • I know most dbms support the concat() function. But why not stick to ANSI SQL, since the sql tag states "Answers to questions tagged with SQL should use ISO/IEC standard SQL." – jarlh Sep 22 '17 at 07:38
  • @Jarlh: Would the "standard" concatenation operator `||` really have been helpful here if it is neither supported by MS-Sql nor by MySql? – Carsten Massmann Sep 22 '17 at 07:43
  • @cars10m, SQL Server supports `||` too, and also `+`. – jarlh Sep 22 '17 at 07:45
1

Do you want the output all joined together, or are you trying to merge tables?

If you're looking just to combine tables in to a single output, I would suggest taking a look at JOIN clauses. Lasse Karlsen has an awesome visualization of joins posted over here. Specifically, I think you're after a left join, which will sorta of merge Table1, and Table2, and give you one output.

Also, since you have the full name split into two columns in Table2, you will have to throw in a CONCAT. Concat will merge multiple strings into a single string that you can then use for comparisons. E.g. CONCAT("string 1", " ", "string 3") gives you "string 1 string 3" (without the quotes).

Keep in mind too that Mark Smith in Table2 seems to have an extra space before his LastName. To take care of that, add in TRIM. This will remove leading, and trailing, spaces.

Depending on who/what/how the table was put together, the names still might not match exactly. If you do just a straight comparison (Table1.col = Table2.col), then the names would have to match up 100%, including case (FYI, I want to say this is not as important on a Windows hosted database, but I'm pretty sure it is important in Linux). You can toss in a LOWER or UPPER function to force everything to upper or lower case.

Here's my lousy pseudo-code take on it:

SELECT Table1.StudentID, Table1.FullName, Table2.HoursLogged FROM Table1 LEFT JOIN Table2 ON CONCAT(UPPER(Table2.FirstName), " ", UPPER(Table2.LastName)) = UPPER(Table1.FullName)

I've noticed sometimes databases get finicky, so you have to throw some graves in there. I'm honestly not certain why this matters sometimes, but I have had this resolve odd issues that have arisen in the past.

SELECT `Table1`.`StudentID`, `Table1`.`FullName`, `Table2`.`HoursLogged` FROM `Table1` LEFT JOIN `Table2` ON CONCAT(UPPER(`Table2`.`FirstName`), " ", UPPER(`Table2`.`LastName`)) = UPPER(`Table1`.`FullName`)

EDIT Totally forgot about the null part. Santho has the right idea.

SELECT Table1.StudentID, Table1.FullName, ISNULL(Table2.HoursLogged, 0) AS `HoursLogged` FROM Table1 LEFT JOIN Table2 ON CONCAT(UPPER(Table2.FirstName), " ", UPPER(Table2.LastName)) = UPPER(Table1.FullName)

EDIT2 You know, I looked at it again, and I feel silly now... If the StudentID's match between the two tables, you don't have to mess with all the CONCAT stuff.

SELECT Table1.StudentID, Table1.FullName, ISNULL(Table2.HoursLogged, 0) AS `HoursLogged` FROM Table1 LEFT JOIN Table2 ON Table1.StudentID = Table2.StudentID

EDIT3 Jarlh's right. I believe the standard to concatenate a string is a double vertical bar

"string 1" || "string 2"

https://msdn.microsoft.com/en-us/library/hh501243(v=sql.105).aspx

Trim leading/trailing spaces

trim(both ' ' from 'string 1')

https://msdn.microsoft.com/en-us/library/hh544564(v=sql.105).aspx

LEFT JOIN's are not standard, but you can add a WHERE clause to a (natural) JOIN

SELECT Table1.StudentID, Table1.FullName, Table2.HoursLogged FROM Table1, Table2 WHERE Table1.StudentID = Table2.StudentID

ISNULL is a function that's not supported by all database management systems, but COALESCE seems to be everywhere.

SELECT Table1.StudentID, Table1.FullName, COALESCE(Table2.HoursLogged, 0) AS HoursLogged FROM Table1, Table2 WHERE Table1.StudentID = Table2.StudentID
Matt
  • 11
  • 3
0

It depends on the type of the database you use. Check this, https://www.w3schools.com/sql/sql_isnull.asp, and use a correct function,

And CONCAT to concat 2 strings.

It would look something like this,

SELECT Ifnull(table1.studentid, table2.studentid) 
       StudentID, 
       Ifnull(table1.fullname, Concat(table2.firstname, ' ', table2.lastname)) 
       FullName, 
       Ifnull(table1.hourslogged, 0) 
       HoursLogged 
FROM   table1 
       LEFT JOIN table2 
              ON table1.studentid = table2.studentid 

Also, you tables design looks bad, not consistent. FirstName & LastName in 1 table, and the other table has FullName.

Better to just design it as such (or something similar)

  • Student table (StudentID, FirstName, LastName)
  • HourLogged table (StudentID, HourlyLogged)