The results of two queries into one table - sql

I have a SQL table that looks something like this:
| FileName | Category | Value | Number |
|:---------:|:--------:|:-----:|:------:|
| TAG File1 | First | 10 | 1 |
| TAG File1 | Second | 8 | 1 |
| TAG File1 | Third | 4 | 1 |
| TAG File2 | First | 13 | 1 |
| TAG File2 | Second | 5 | 1 |
| TAG File2 | Third | 6 | 1 |
| TAG File1 | First | 11 | 2 |
| TAG File1 | Second | 7 | 2 |
| TAG File1 | Third | 5 | 2 |
| TAG File2 | First | 14 | 2 |
| TAG File2 | Second | 6 | 2 |
| TAG File2 | Third | 5 | 2 |
| TAG File1 | First | 10 | 3 |
| TAG File1 | Second | 6 | 3 |
| TAG File1 | Third | 5 | 3 |
| TAG File2 | First | 12 | 3 |
| TAG File2 | Second | 7 | 3 |
| TAG File2 | Third | 4 | 3 |
| TAG File1 | First | 11 | 4 |
| TAG File1 | Second | 8 | 4 |
| TAG File1 | Third | 5 | 4 |
| TAG File2 | First | 13 | 4 |
| TAG File2 | Second | 5 | 4 |
| TAG File2 | Third | 5 | 4 |
I wanted to write a query that will only show the results for the two "most recent" values in the Numbercolumn. The number column is a counting value. Everytime this table is updated with a new set of data, the value in the Number column for that set of data is +1 from the max value. Ultimately, I want a query that would accomplish what this query would.
select FileName, Category, Value, (select max(Number) from Table) as Number
from Table;
while also having these results in the table as well:
select FileName, Category, Value, (select max(Number)-1 from Table) as Number
from Table;
The results should look something like this:
| FileName | Category | Value | Number |
|:---------:|:--------:|:-----:|:------:|
| TAG File1 | First | 10 | 3 |
| TAG File1 | Second | 6 | 3 |
| TAG File1 | Third | 5 | 3 |
| TAG File2 | First | 12 | 3 |
| TAG File2 | Second | 7 | 3 |
| TAG File2 | Third | 4 | 3 |
| TAG File1 | First | 11 | 4 |
| TAG File1 | Second | 8 | 4 |
| TAG File1 | Third | 5 | 4 |
| TAG File2 | First | 13 | 4 |
| TAG File2 | Second | 5 | 4 |
| TAG File2 | Third | 5 | 4 |

Use a subquery to find the max number
SELECT * FROM table WHERE number >= (SELECT MAX(number) FROM table) - 1

You can use a subquery to get the 2 largest, distinct numbers:
select FileName, Category, Value, Number
from Table
where Number in (SELECT DISTINCT TOP 2 Number FROM Table ORDER BY Number desc);

Try this
SELECT
FileName,
Category,
Value,
Number
FROM
TABLE T
WHERE
T.Number IN
(
SELECT DISTINCT TOP 2 Number
FROM Table IT
WHERE
IT.FileName = T.FileName AND
IT.Category = T.Category
ORDER BY IT.Number DESC
)

Related

How to add conditional count based on mutiple columns

I'm trying to summarise a T-SQL output that looks a little like this:
+---------+---------+-----+-------+
| perf_no | section | row | seat |
+---------+---------+-----+-------+
| 7128 | 6 | A | 4 |
| 7128 | 6 | A | 5 |
| 7128 | 6 | A | 7 |
| 7128 | 6 | A | 9 |
| 7128 | 6 | A | 28 |
| 7129 | 6 | A | 29 |
| 7129 | 6 | A | 8 |
| 7129 | 6 | A | 9 |
| 7129 | 8 | A | 6 |
| 7129 | 8 | B | 3 |
| 7129 | 8 | B | 4 |
+---------+---------+-----+-------+
Comparing one row to the row(s) below, if the perf_no, section, and row values are the same, and the difference between the seat values is 1, then I want to consider them a group, and count the number of rows in that group.
To give you a real world example, these are seats in a theatre! I'm trying to summarise what seats are available.
Using the table above to illustrate:
rows 1 & 2 show that seats 4 & 5 in section 6, row 8 for performance 7128 are available. So that's 2 seats together
row 3 shows that 7 in sectino 6, row 8 for performance 7128 is available on its own. So that's a single seat (1)
rows 5 & 6 have the same section and row, and the seats are consecutive, but you can see the performance is different. So that's a single seat too.
So the output for the table above would look a little like...
(I've left in the spaces just so visually you can see the groupings more easily - obviously the final version will have none)
+---------+---------+----------+-------+
| perf_no | section | seat_row | total |
+---------+---------+----------+-------+
| 7128 | 6 | A | 2 |
| | | | |
| 7128 | 6 | A | 1 |
| 7128 | 6 | A | 1 |
| 7128 | 6 | A | 1 |
| 7129 | 6 | A | 1 |
| 7129 | 6 | A | 2 |
| | | | |
| 7129 | 6 | A | 1 |
| 7129 | 8 | B | 2 |
+---------+---------+----------+-------+
I've been trying to use some conditional case statements to not much avail. Any assistance very gratefully received!
This is a type of gaps-and-islands problem. You can generate a grouping by subtracting a sequence (generated by row_number()) from the seat:
select perf_no, section, row, count(*) as num_seats,
min(seat) as first_seat, max(seat) as last_seat
from (select t.*,
row_number() over (partition by perf_no, section, row order by seat) as seqnum
from t
) t
group by perf_no, section, row, (seat - seqnum);

Oracle SQL - Generate aggregate row using select

I have a table like below.
| FILE | CATEGORY1 | CATEGORY2 | CATEGORY3 |
| File1.txt | 3 | 2 | 6 |
| File2.txt | 4 | 7 | 3 |
| File2.txt | 3 | 1 | 1 |
Now, Is it possible to add a new row as part of select query which will provide the below row added to the resultset.
| Total | 10 | 10 | 10 |
Expected final output:
| FILE | CATEGORY1 | CATEGORY2 | CATEGORY3 |
| File1.txt | 3 | 2 | 6 |
| File2.txt | 4 | 7 | 3 |
| File2.txt | 3 | 1 | 1 |
| Total | 10 | 10 | 10 |
Any help on achieving the above results is highly appreciated.
Thank you.
You can use this:
SELECT FILE, CATEGORY1, CATEGORY2, CATEGORY3 From TestTable
union all
SELECT 'Total', Sum([CATEGORY1]), Sum([CATEGORY2]), Sum([CATEGORY3]) From TestTable
If values in your column allow nulls, then can use function like nvl.
You can also try rollup also:
SELECT File, Sum([CATEGORY1]), Sum([CATEGORY2]), Sum([CATEGORY3]) From
TestTable
group by rollup (File);

Joining two files in AWK - Join Column at different positions

File A has following 3 columns:
AccountNumber, CustomerName, Balance
File B has the following 5 columns
SystemID, AccountNumber, Product, OpeningDate
I am using the AWK code below to join the two files. It is not working
FNR==NR{a[$1]=$0;next}$2 in a{ print $0, a[$1]}
The combined output of both the files is expected as below
SystemID, AccountNumber, Product, OpeningDate,CustomerName, Balance
Request help in AWK Code only. Am aware that similar functionality is available through SORT and JOIN but am looking for AWK solution only.
File A:
+---------------+---------------+----------+
| AccountNumber | CustomerName | Balance |
+---------------+---------------+----------+
| 3 | C | 100 |
| 4 | A | 200 |
| 5 | B | 300 |
+---------------+---------------+----------+
File B:
+-----------+----------------+----------+--------------+
|  SystemID | AccountNumber | Product | OpeningDate |
+-----------+----------------+----------+--------------+
| 1 | 6 | RD | 12-05-17 |
| 2 | 4 | SB | 15-05-17 |
| 3 | 3 | TD | 02-04-17 |
| 4 | 5 | SB | 15-01-17 |
+-----------+----------------+----------+--------------+
Output after joining:
+-----------+----------------+----------+--------------+--------------+---------+
|  SystemID | AccountNumber | Product | OpeningDate | CustomerName | Balance |
+-----------+----------------+----------+--------------+--------------+---------+
| 2 | 4 | SB | 15-05-17 | A | 200 |
| 3 | 3 | TD | 02-04-17 | C | 100 |
| 4 | 5 | SB | 15-01-17 | B | 300 |
+-----------+----------------+----------+--------------+--------------+---------+
If you can remove pipe,plus and - symbol below is the solution -
cat file1
1 6 RD 12-05-17
2 4 SB 15-05-17
3 3 TD 02-04-17
4 5 SB 15-01-17
cat file2
3 C 100
4 A 200
5 B 300
awk 'NR==FNR{a[$1]=$2FS$3;next} $2 in a {print $0, a[$2]}' file2 file1
2 4 SB 15-05-17 A 200
3 3 TD 02-04-17 C 100
4 5 SB 15-01-17 B 300

SQL - only display rows that have the max value

I have this table that is already sorted but I want it to only display the maximum values... so instead of this table:
+------+-------+
| id | value |
+------+-------+
| 1 | 3 |
| 5 | 3 |
| 4 | 3 |
| 9 | 2 |
| 8 | 2 |
| 3 | 2 |
| 2 | 1 |
| 6 | 1 |
| 7 | 1 |
+------+-------+
I want this:
+------+-------+
| id | value |
+------+-------+
| 1 | 3 |
| 5 | 3 |
| 4 | 3 |
+------+-------+
I'm using SQLite. thanks for any help.
You can do this using a subquery. Here is one way:
select t.*
from t
where t.value = (select max(value) from t);

Selecting the maximum value only for another maximum value

If I have two int data type columns in SQL Server, how can I write a query so that I get the maximum number, at the maximum number of the other column?
Let me give an example. Lets say I have this table:
| Name | Version | Category | Value | Number | Replication |
|:-----:|:-------:|:--------:|:-----:|:------:|:-----------:|
| File1 | 1.0 | Time | 123 | 1 | 1 |
| File1 | 1.0 | Size | 456 | 1 | 1 |
| File2 | 1.0 | Time | 312 | 1 | 1 |
| File2 | 1.0 | Size | 645 | 1 | 1 |
| File1 | 1.0 | Time | 369 | 1 | 2 |
| File1 | 1.0 | Size | 258 | 1 | 2 |
| File2 | 1.0 | Time | 741 | 1 | 2 |
| File2 | 1.0 | Size | 734 | 1 | 2 |
| File1 | 1.1 | Time | 997 | 2 | 1 |
| File1 | 1.1 | Size | 997 | 2 | 1 |
| File2 | 1.1 | Time | 438 | 2 | 1 |
| File2 | 1.1 | Size | 735 | 2 | 1 |
| File1 | 1.1 | Time | 786 | 2 | 2 |
| File1 | 1.1 | Size | 486 | 2 | 2 |
| File2 | 1.1 | Time | 379 | 2 | 2 |
| File2 | 1.1 | Size | 943 | 2 | 2 |
| File1 | 1.2 | Time | 123 | 3 | 1 |
| File1 | 1.2 | Size | 456 | 3 | 1 |
| File2 | 1.2 | Time | 312 | 3 | 1 |
| File2 | 1.2 | Size | 645 | 3 | 1 |
| File1 | 1.2 | Time | 369 | 3 | 2 |
| File1 | 1.2 | Size | 258 | 3 | 2 |
| File2 | 1.2 | Time | 741 | 3 | 2 |
| File2 | 1.2 | Size | 734 | 3 | 2 |
| File1 | 1.3 | Time | 997 | 4 | 1 |
| File1 | 1.3 | Size | 997 | 4 | 1 |
| File2 | 1.3 | Time | 438 | 4 | 1 |
| File2 | 1.3 | Size | 735 | 4 | 1 |
How could I write a query so that I selected the maximum Replication value at the maximum Number value? As you can see, in this table, the maximum value in Number is 4 but the maximum number in Replication where Number = 4 is 1
All I can think to do is this:
SELECT MAX(Replication) FROM Table
WHERE Number IS MAX;
which is obviously wrong and doesn't work.
You can try Group By and Having
select max(Replication) from Table_Name group by [Number] having
[Number]=(select max([Number]) from Table_Name)
Just use a subquery to find the max number in the where clause. If you just want one single number as the result there is no need to use group by and having (which would make the query a lot more expensive):
select max([replication]) from tab
where number = (select max(number) from tab)