1

I have a field name 'Title' from table 'Greeting'. Title field in UI might come as FirstName or LastName or combination of both.

Greeting table looks like:

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
</style>
</head>
<body>

<h2>Collapsed Borders</h2>
<p>If you want the borders to collapse into one border, add the CSS border-collapse property.</p>

<table style="width:50%">
  <tr>
    <th>Title</th>
  </tr>
  <tr>
    <td>FirstName</td>
  </tr>
  <tr>
    <td>LastName</td>
  </tr>
  <tr>
    <td>nickName</td>
  </tr>
  <tr>
    <td>preferredName</td>
  </tr>
</table>

</body>
</html>

I need to write a SQL Query to find which has Title FirstName only, LastName only and both FirstName and LastName and display in below table format. Create column name as FirstName and lastName and indicate by 'Yes' if that displayed and 'No' if not displayed.

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
</style>
</head>
<body>

<h2>Collapsed Borders</h2>
<p>If you want the borders to collapse into one border, add the CSS border-collapse property.</p>

<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
  </tr>
  <tr>
    <td>Yes</td>
    <td>No</td>
  </tr>
  <tr>
    <td>No</td>
    <td>Yes</td>
  </tr>
  <tr>
    <td>Yes</td>
    <td>Yes</td>
  </tr>
</table>

</body>
</html>

I wrote below query and got the individual information of the 'Title' but not able to get column values as table column name format. Could you please help me to get output data in that table format

Select distinct Title from Greeting  WHERE  Title in ('FirstName','LastName') 
Select distinct Title from Greeting  WHERE  Title ='FirstName'
Select distinct Title from Greeting  WHERE  Title = 'LastName'
7
  • 1
    can you paste your sample database data and what you want it to look like pls Commented Jun 6, 2019 at 18:42
  • 1
    Thank you @JosephDoggie Commented Jun 6, 2019 at 19:04
  • 1
    I only skimmed, but are you looking for the literal strings FirstName and LastName? Or are those column values? Commented Jun 6, 2019 at 19:50
  • 1
    @barrycarter those are just the column values not literal strings Commented Jun 6, 2019 at 20:29
  • 1
    Then they don't belong in quotes. (some SQL servers let you them in backticks, but not single quotes) Commented Jun 7, 2019 at 0:52

1 Answer 1

1

I tried 'pivot' and that helped me to get get output data in that table format

Sign up to request clarification or add additional context in comments.

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.