Multiple rows into one - sql

I'm working on a MS Access 2010 database and I'm struggling more than expected with this.
I have these tables:
tblBook:
IDBook (key)
Title
tblUser:
IDUser (key)
Username
tblOrder:
IDOrder (key)
IDUser (linked to tblUser)
Date
tblOrderBook:
IDOrderBook (key)
IDOrder (linked to tblOrder)
IDBook (linked to tblBook)
A user can pick up to 3 books per order. I made a query that displays them like this, by IDOrderBook:
IDOrderBook |IDOrder | Username | Date | Title
6 |3 | John | Aug 1| Harry Potter
5 |3 | John | Aug 1| Lord of the Rings
4 |2 | Susan | Jul 5| The Shining
3 |2 | Susan | Jul 5| Huck Finn
2 |2 | Susan | Jul 5| Peter Pan
1 |1 | Rita | Jul 4| Harry Potter
Now I want something to show them by IDOrder like this:
IDOrder | Username | Date | Title1 | Title2 | Title3
3 | John | Aug 1| Harry Potter | LoTR |
2 | Susan | Jul 5| The Shining | Huck Finn | Peter Pan
1 | Rita | Jul 4| Harry Potter | |
So with multiple titles in a single row. How do I build this query?
Thank you!

This is quite a difficult task. First, we create the column name. We do that by counting each record with an ID higher than the current one. Then, we pivot that column name, creating t
I'm going to base all these queries based on that query you've shared. We'll call that qry1.
The query creating the column names, qry2:
SELECT IDOrderBook, IDOrder, Username, Date, Title,
"Title" & (
SELECT Count(*)
FROM qry1 s
WHERE q.IDOrder = s.IDOrder AND q.IDOrderBook >= s.IDOrderBook
) As ColumnName
FROM qry1 q
Then, we use a pivot (crosstab) query to create your desired result:
TRANSFORM First(Title)
SELECT IDOrder, Username, Date
FROM qry2
GROUP BY IDOrder, Username, Date
Pivot ColumnName
I leave merging these queries using subqueries as an exercise to the reader, I've split them for clarity.

Related

Count string occurrences within a list column SQL/Grafana

I have a table in the following format:
| id | tags |
|----|-------------------------|
|1 |['Car', 'Plane', 'Truck']|
|2 |['Plane', 'Truck'] |
|3 |['Car', 'Plane'] |
|4 |['Plane'] |
|5 |['Boat', 'Truck'] |
How can I create a table that gives me the total number of occurrences of each item in all cells of the "tags" column? Items ideally do not include single quotes, but may if necessary.
The resulting table would look like:
| tag | count |
|-------|-------|
| Car | 2 |
| Plane | 4 |
| Truck | 3 |
| Boat | 1 |
The following does not work because it only counts identical "tags" entries rather than comparing list contents.
SELECT u.id, count(u.tags) as cnt
FROM table u
group by 1
order by cnt desc;
I am aware of this near-identical question, but they are using Snowflake/SQL whereas I am using MySQL/Grafana so the accepted answer uses functions unavailable to me.

Counting the Number of Rows That Have a Spesific Value in a Column in a Table in SQL

id | street | city | country | postal code |
--------------------------------------------
0 |a street|x city| Turkey | 12345 |
1 |b street|y city| Turkey | 12335 |
2 |c street|z city| USA | 12315 |
3 |d street|j city| Turkey | 32345 |
4 |e street|k city| Germany | 12135 |
5 |f street|l city| France | 13215 |
6 |g street|m city| Turkey | 42135 |
7 |h street|n city| Italy | 12135 |
8 |i street|z city| Spain | 32115 |
Hello. Let say we have a table like above named 'person_address'. In db there are lots of different tables like this. I want to find the number of rows whose values are "Turkey" in the "country" column in the person_address table which is 4. How can I translate this into postgresql?
SELECT * FROM person_address u WHERE u.country = 'Turkey';
With this code i can list what i want but i need number of that list. And after that i have to use this in Java spring boot project. Should to this with #Query annotation or is there a better way?
If you enter the output you want, you will get the answer very quickly. The following query returns the countries that have been listed 4 times.
select *
from
(select *,
count(*) over(partition by country order by country) as numberOfCountry
from person_address) t
where numberOfCountry = 4

SQL query conditioned by another table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Beginner with SQL in need of help here is the problem. Can be SQL stmt for any DB i am testing on postgresql.
I have the following issue for 2 SQL tables:
Table number 1
.....................
|Boy Name| Girl Name |
|--------------------|
|Michael | Anne |
|Michael | Misty |
|Michael | Simone |
|Michael | Diane |
|Michael | Ariel |
|Jack | Misty |
|Jack | Simone |
|Jack | Anne |
|Sam | Simone |
|Sam | Misty |
|Sam | Anne |
|Sam | Mini |
|Sam | Valery |
----------------------
Table number 2
..................................
|Boy Name | Anne | Misty | Simone|
|--------------------------------|
|Michael | yes | no | no |
|Jack | yes | yes | yes |
|Sam | no | no | yes |
..................................
(IDK is like a question: does a girl like a specific boy. Can be yes, no or nothing).
Desired result
A table where the query returns 2 columns:
- a row for every "no" a boy gets from a girl via table number 2.
.....................
|Boy Name| Girl Name |
|--------------------|
|Michael | Misty |
|Michael | Simone |
|Sam | Misty |
|Sam | Anne |
----------------------
You can think of the result as a new row a with a column for the boy's name and another one with the name of the girl that had "no" in table number 2 .
First of all, I would suggest different table design:
CREATE TABLE person
(
id INT PRIMARY KEY,
person_name VARCHAR(200),
sex CHAR(1)
);
-- possible record: 1, 'John', 'M'; 2, 'Mary', 'F';
CREATE TABLE person_symphaty
(
person_id INT, -- FK to person table
symphaty_person_id INT -- FK to person table
);
-- you will store here if the person likes another one. 1 point here is that you can store that person doesn't like somebody, then you need some flag, for example is_symphaty INT (then 1 will mean likes, 0 will mean dislike)
then the query itself
SELECT pm.name AS male_name, pf.name AS female_name
FROM person pm
CROSS JOIN person pf
WHERE pm.sex = 'M' AND pf.sex = 'F'
NOT EXISTS (
SELECT * FROM person_symphaty ps WHERE ps.person_id = pm.id AND ps.symphaty_person_id = pf.id
)
There could be mistakes as I didn't test these queries at all.

Return rows with duplicate value in column if one of the rows contains specific value

I'm attempting to query against a table to determine where a value is assigned multiple times but one of assignees must equal a determined value.
For example :
EmployeeName |EmployeeNumber|EmployeeDept|EmployeeBadgeCode|
--------------+--------------+------------+-----------------+
Dante Hicks |1 |200 |1 pair A
Randall Graves|5 |201 |2 no pair
Brody Bruce |1 |555 |3 pair A
Banky Edwards |20 |004 |4 pair B
Gwen Turner |7 |200 |5
Holden McNeil |20 |450 |1 pair B
TS Quint |5 |105 |10 no pair
I want to return where all rows where there is a duplicate EmployeeNumber but only if one of the duplicate's value is 1 for EmployeeBadgeCode.
So I would want to return 'Dante Hicks' and 'Brody Bruce' as one pair of duplicates and 'Banky Edwards' and 'Holden McNeil' as another pair of duplicates.
'Randall Graves' and 'TS Quint' would not be returned because they have duplicate EmployeeNumber but neither of EmployeeBadeCode values are equal to 1.
Any help would be appreciated.
Thanks.
SQL Fiddle Demo
e1.[EmployeeName] < e2.[EmployeeName]
This is to make sure you dont try to match with yourself and also avoid reverse pair.
SELECT e1.*, e2.*
FROM employee e1
JOIN employee e2
ON e1.[EmployeeNumber] = e2.[EmployeeNumber]
AND e1.[EmployeeName] < e2.[EmployeeName]
WHERE
e1.EmployeeBadgeCode = 1
or e2.EmployeeBadgeCode = 1
In this solution both employess can have 1. You need add one aditional validation if want only one of the employee be 1
OUTPUT
| EmployeeName | EmployeeNumber | EmployeeDept | EmployeeBadgeCode | EmployeeName | EmployeeNumber | EmployeeDept | EmployeeBadgeCode |
|---------------|----------------|--------------|-------------------|---------------|----------------|--------------|-------------------|
| Brody Bruce | 1 | 555 | 3 | Dante Hicks | 1 | 200 | 1 |
| Banky Edwards | 20 | 4 | 4 | Holden McNeil | 20 | 450 | 1 |
This will return both those rows. If there was only one EmployeeNumber 1 then it would not return anything.
SELECT *
FROM YourTable
WHERE EmployeeNumber =
(SELECT EmployeeNumber
FROM YourTable
WHERE EmployeeNumber = 1
GROUP BY EmployeeNumber
HAVING COUNT(1) > 1)

Increment value when the field is the same

First, I'm sorry for the ambiguous title.
Here's my problem :
I'm using Access and I have this table :
+--------+-----------+
| PARENT | CHILD |
+--------+-----------+
| JOHN | TANIA |
| JOHN | ROBERT |
| JOHN | APRIL |
| HELEN | TOM |
| HELEN | GABRIELLE |
+--------+-----------+
And I would like to add a column like this with queries or VBA code :
+--------+-----------+---------+
| PARENT | CHILD | LIST |
+--------+-----------+---------+
| JOHN | TANIA | CHILD 1 |
| JOHN | ROBERT | CHILD 2 |
| JOHN | APRIL | CHILD 3 |
| HELEN | TOM | CHILD 1 |
| HELEN | GABRIELLE | CHILD 2 |
+--------+-----------+---------+
I want to do this because at the end, I want to run a cross tab query. I'm only missing that last column to create that query.
I tried to do it in a recordset, but my database starts bloating after a couple of rst.Update (I have 700k+ rows)
I created a temporary table and used UPDATE queries but it just takes too much time.
I think there might be a SQL code that would do what I need, but I just can't figure it out. I hope you could help me, thanks :)
You can do something like the below, but it would be much better with some sort of IDs:
SELECT Parent.PARENT,
Parent.CHILD,
(SELECT Count(*)
FROM Parent p
WHERE p.Parent=Parent.Parent
AND p.Child<=Parent.Child) AS ChildNo
FROM Parent
ORDER BY Parent.PARENT, Parent.CHILD;
Parent is the name of the table.