• 0

Help on an SQL Query


Question

Say I have 2 tables:

Table Employee

=================

| id | firstname | surname |

--------------------------------

| 1 | John | Smith |

--------------------------------

Table Allowances

=============================

| employeeid | type | amount

-----------------------------------------------------

| 1 | communication | 100

| 1 | car | 1500

------------------------------------------------------

My current SQL is

SELECT e.id, e.firstname, a.employeeid, a.type, a.amount

FROM employee e, allowances a

WHERE e.id = a.employeeid

This will gives me some rows of "semi-duplicates" like:

Row 1: 1, John, Smith, communication, 100

Row 2: 1, John, Smith, car, 1500

Would it be possible to have an SQL query where the result is like:

|id|firstname|surname|communication_allowance|car_allowance|

1, John, Smith, 100, 1500

Link to comment
https://www.neowin.net/forum/topic/449949-help-on-an-sql-query/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

you need to use a subquery.

Something like:

Select id, firstname, lastname, (select amount from Allowances where

employeeid =id and type='communication') as communication_allowance,

(select amount from Allowances where

employeeid =id and type='car') as car_allowance

FROM employee

Just realized yours was a MySQL question.

I don't know MySQL -- only MS SQL Server.

So this might not work.

  • 0
  :: Lyon :: said:

Say I have 2 tables:

Table Employee

=================

| id | firstname | surname |

--------------------------------

| 1 | John | Smith |

--------------------------------

Table Allowances

=============================

| employeeid | type | amount

-----------------------------------------------------

| 1 | communication | 100

| 1 | car | 1500

------------------------------------------------------

My current SQL is

SELECT e.id, e.firstname, a.employeeid, a.type, a.amount

FROM employee e, allowances a

WHERE e.id = a.employeeid

This will gives me some rows of "semi-duplicates" like:

Row 1: 1, John, Smith, communication, 100

Row 2: 1, John, Smith, car, 1500

Would it be possible to have an SQL query where the result is like:

|id|firstname|surname|communication_allowance|car_allowance|

1, John, Smith, 100, 1500

I not sure whether this works, I'm trained on Oracle SQL though I used mySQL a number of times.

SELECT e.id, e.firstname,

e.surname,

(select a.amount from allowances a where a.employeeid = e.id and a.type = 'communication' and rownum = 1) "communication_allowance",

(select a.amount from allowances a where a.employeeid = e.id and a.type = 'car' and rownum = 1) "car_allowance"

from employee e

Note that this works only when there is only 1 row per allowance type, per employee id in allowances. (Hence the rownum = 1).

  • 0

It depends. If you already know the employee types are constant then it's pretty easy with the subquery methods mentioned before, but it also means that your data model is flawed... But if the employee types are not constant, then the subquery method won't work since it only looks for "car" and "communication". Any other types wouldn't show up. It's still possible, but I only know of a way to do it in MS SQL 2005 with the PIVOT option. I don't know if Oracle or MySQL have a similar feature. Basically it just makes the rows the columns and the columns the rows.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Interesting. Seems to promote or invite more people to use whatsapp.
    • AI is going to destroy online as well as offline worlds.
    • QOwnNotes 25.8.0 by Razvan Serea QOwnNotes is a open source (GPL) plain-text file notepad with markdown support and todo list manager for GNU/Linux, Mac OS X and Windows, that (optionally) works together with the notes application of ownCloud (or Nextcloud). So you are able to write down your thoughts with QOwnNotes and edit or search for them later from your mobile device (like with CloudNotes) or the ownCloud web-service. The notes are stored as plain text files and you can sync them with your ownCloud sync client. Of course other software, like Dropbox, Syncthing, Seafile or BitTorrent Sync can be used too. Features: the notes folder can be freely chosen (multiple note folders can be used) sub-string searching of notes is possible and search results are highlighted in the notes application can be operated with customizable keyboard shortcuts external changes of note files are watched (notes or note list are reloaded) older versions of your notes can be restored from your ownCloud server trashed notes can be restored from your ownCloud server differences between current note and externally changed note are showed in a dialog markdown highlighting of notes and a markdown preview mode notes are getting their name from the first line of the note text (just like in the ownCloud notes web-application) and the note text files are automatically renamed, if the the first line changes compatible with the notes web-application of ownCloud and mobile ownCloud notes applications compatible with ownCloud's selective sync feature by supporting an unlimited amount of note folders with the ability to choose the respective folder on your server manage your ownCloud todo lists (ownCloud tasks or Tasks Plus / Calendar Plus) or use an other CalDAV server to sync your tasks to encryption of notes (AES-256 is built in or you can use custom encryption methods like Keybase.io (encryption-keybase.qml) or PGP (encryption-pgp.qml)) dark mode theme support theming support for the markdown syntax highlighting all panels can be placed wherever you want, they can even float or stack (fully dockable) support for freedesktop theme icons, you can use QOwnNotes with your native desktop icons and with your favorite dark desktop theme support for hierarchical note tagging and note subfolders support for sharing notes on your ownCloud server portable mode for carrying QOwnNotes around on USB sticks Evernote import QOwnNotes is available in many different languages like English, German, French, Polish, Chinese, Japanese, Russian, Portuguese, Hungarian, Dutch and Spanish QOwnNotes 25.8.0 changelog: More warning log messages were ignored for Qt 6.9.1 Download: QOwnNotes 25.8.0 | 71.4 MB (Open Source) Download: QOwnNotes for Other Operating Systems View: QOwnNotes Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Patch My PC - Home Updater 5.3 Final is out.
    • 7-Zip developer is quite stubborn as he refuses to support Windows 11 right click context menu.
  • Recent Achievements

    • Collaborator
      bullgod69 earned a badge
      Collaborator
    • Enthusiast
      Ed B went up a rank
      Enthusiast
    • Reacting Well
      Xinotema earned a badge
      Reacting Well
    • Dedicated
      Edward266 earned a badge
      Dedicated
    • First Post
      Markvens earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      760
    2. 2
      ATLien_0
      187
    3. 3
      +FloatingFatMan
      151
    4. 4
      Xenon
      118
    5. 5
      wakjak
      113
  • Tell a friend

    Love Neowin? Tell a friend!