Get an extra row with a join - sql

Assume I have the following tables
CHAPTERS
| ID | Title |
---------------------
| 1 | Introduction |
LINES
| ID | Chapter | Line |
--------------------------------------
| 2 | 1 | Fourscore and ... |
| 5 | 1 | In the beginning... |
What I'm looking for in my SQL result is the following
| Title | Line |
-----------------------------
| Introduction | null |
| Introduction | Fourscore |
| Introduction | In the beg |
So basically I want an extra row with just the Title and other rows with te matching lines.
All I've got now is just the 2 rows with the lines without the missing null-line with just the title.
Any ideas?

Try this
select title,null as Line from chapters
union
select title,line from chapters a join lines b on a.id=b.chapter

Related

How to upend query to a table as new column in Access?

As a follow up to my previous question I wanted to go further with it and do other calculations based on my Diff.
Sample table:
+-------+-------+--------+-------------------+------+------+------+
| ID | RowID | User | Godzina_transakcji| Diff | Diff2| Diff3|
+-------+-------+--------+-------------------+------+------+------+
| 3 | 1 | AAA | 14:23:03 | | | |
| 8 | 2 | AAA | 14:23:57 | | | |
| 11 | 3 | AAA | 14:25:03 | | | |
| 2 | 4 | BBB | 03:37:23 | | | |
| 893 | 5 | BBB | 03:39:21 | | | |
| 5 | 6 | BBB | 05:23:11 | | | |
+-------+-------+---------+------------------+------+------+------+
Working code:
SELECT
tblTemp2.RowID,
tblTemp2.User,
tblTemp2.Godzina_transakcji,
Format(( Select TimeValue(T.Godzina_transakcji)
From tblTemp2 As T
Where tblTemp2.RowID = T.RowID + 1 And tblTemp2.User = T.User ) - TimeValue([Godzina_transakcji]), "hh:nn:ss") As Diff
FROM
tblTemp2;
So I thought the best way will be append my tblTemp with Diff, and here is where it started to hit the fan.
When I tried to change query to "Make table" my Access freezes for up to 10min. When I try to use Update query I get error about "Operation must use an updatable query.".
So my question is: what is best way to do this having in mind that next fields will be simple ifs based on calculated Diff?
For example idea about Diff2 is to check if value of hours in Diff is bigger than 4.
Edit: Hope, now it's more precise and goes along with rules :)

SQL 'Sum' Text Fields, Delim with commas

I have a table like this:
+----+-------+-----------------+
| ID | Name | Email |
+----+-------+-----------------+
| 1 | Jane | Jane#doe.com |
| 2 | Will | Will#gmail.com |
| 3 | Will | wsj#example.com |
| 4 | Jerry | jj2#test.com |
+----+-------+-----------------+
Unfortunately I have records that are duplicates due to multiple emails. I would like to run a sql query to generate this:
+----+-------+---------------------------------+
| ID | Name | Email |
+----+-------+---------------------------------+
| 1 | Jane | Jane#doe.com |
| 2 | Will | Will#gmail.com, wsj#example.com |
| 4 | Jerry | jj2#test.com |
+----+-------+---------------------------------+
I know with numbers you'd do something like this, but I don't know how to 'sum' text fields:
SELECT *,
SUM(Number_Field) AS Number_Field,
FROM table
Thanks!
Edit: I am using MS Access

SQL Get cases related to a user and the number of files attached to that case

Hi everyone got a little stuck on an sql query. I have four tables
users
+----+------------+-----------+--------+
| id | first_name | last_name | active |
+----+------------+-----------+--------+
| 1 | Joe | Bloggs | 1 |
| 2 | John | Doe | 1 |
| 3 | Dave | Smith | 1 |
+----+------------+-----------+--------+
cases
+----+-----------+-------------+
| id | case_code | case_name |
+----+-----------+-------------+
| 1 | THEC12C | Test Case 1 |
| 2 | ABCD23A | Test Case 2 |
+----+-----------+-------------+
case_creditors
+----+---------+-------------+
| id | case_id | creditor_id |
+----+---------+-------------+
| 1 | 1 | 3 |
| 2 | 2 | 1 |
+----+---------+-------------+
case_files
+----+---------+----------+-----------+
| id | case_id | filename | file type |
+----+---------+----------+-----------+
| 1 | 1 | test.pdf | pfd |
| 2 | 2 | file.txt | txt |
| 3 | 2 | word.doc | doc |
+----+---------+----------+-----------+
When a user logs in i need to show a table with the users accociated cases the number of files attached to that case so if Joe Blogs loged in head see the following table
+-----------+-------------+-------+
| Case Code | Case Name | Files |
+-----------+-------------+-------+
| ABCD23A | Test Case 2 | 2 |
+-----------+-------------+-------+
ive been trying to write the sql statement to do this but am getting stuck on the query and wandered if someone could help give me some pointers. the sql ive gor so far
SELECT * FROM cases
(SELECT COUNT(*) FROM case_files WHERE case_files.case_id = cases.id) as Files
JOIN case_creditors ON cases.id = case_creditors.case_id
WHERE case_creditors.creditor_id = 1
managed to sort this with
SELECT
ips_case.*,
COUNT(case_files.file_id) AS Files
FROM
ips_case
LEFT JOIN case_files ON ips_case.id = case_files.case_id
JOIN case_creditors ON ips_case.id = case_creditors.case_id
WHERE
case_creditors.creditors_id = 4
GROUP BY
ips_case.id

Showing data from another table if it exists

I am having a hard time trying to get the correct data out of my DB.
I have a couple of tables:
events_template laser_events
| id | something | | id | extid | added |
================== ===========================
| 1 | something | | 1 | 7 | added |
| 2 | something | | 2 | 4 | added |
| 3 | something | | 3 | 2 | added |
| 4 | something | | 4 | 1 | added |
| 5 | something | | 5 | 9 | added |
| 6 | something | | 6 | 3 | added |
| 7 | something |
| 8 | something |
| 9 | something |
| 10 | something |
| 11 | something |
| 12 | something |
| 13 | something |
| 14 | something |
What I am trying to do is get some output that will show me the results of both tables together linked by id and extid, but still show the results from events_template even if there isn't a matching laser_events row.
I've tried something like
SELECT
id,
extid
FROM
events_template,
laser_events
WHERE
events_template.id = laser_events.ext_id;
But that doesn't show me the events_template rows if there isn't a matching laser_events row.
Any help would be appreciated!
You have to use LEFT JOIN:
SELECT e.id, l.ext_id
FROM events_template e
LEFT JOIN laser_events l ON e.id = l.ext_id;

Turn Parts of Rows into a Separate Column in SQL Server

I'm not sure if Pivot is the way to go with this, but I am looking to take part of a row and create a new column with it.
This is my example:
+--------+------------+--------+
| Person | PetName | PetAge |
+--------+------------+--------+
| 1 | Apple | 2 |
| 1 | Banana | 6 |
| 1 | Grapefruit | 3 |
| 2 | Red | 53 |
| 2 | Blue | 8 |
+--------+------------+--------+
This is my result/goal:
+--------+---------+--------+---------+--------+------------+--------+
| Person | PetName | PetAge | PetName | PetAge | PetName | PetAge |
+--------+---------+--------+---------+--------+------------+--------+
| 1 | Apple | 2 | Banana | 6 | Grapefruit | 3 |
| 2 | Red | 53 | Blue | 8 | | |
+--------+---------+--------+---------+--------+------------+--------+
How can I get the result from my example?
UPDATE: I just noticed that your table just had the Person in the first row.
I've done something similar. What I did was add a RowNumber per pet by person (OVER PARTITION BY PERSON) to the data. This will allow the data to be broken up and an order of numbers for each pet per person.
Make your normal table with just the PetName and PetAge.
Add a Tablix with just one column and row and put the previous table in it.
For the Column grouping, use ROW_NUM. For Row use Person.