Oracle SQL - Pivoting multiple rows into single column - sql

To all the Oracle SQL pros, please, I'm trying to use a pivot to convert multiple rows into a single column. I tried looking through past posting but could not find anything related to what I want to do, or maybe I just don't know how to search for it properly. I want to take the rows HAND, PECO, CHEP and make that into 1 column called EXTRA, while leaving STORAGE and RENEWAL as their own separate columns. I'm stumped, any help would be greatly appreciated. Thanks in advance!
My current sql is,
select * from (
select company, customer, rev_code, sum(rev_amt) amt
from revenue_anal
where company='01'
and rev_date between to_date('20\01\01','YY\MM\DD') and sysdate
group by company, customer, rev_code
order by 2,1 )
pivot (sum(amt) for rev_code in ('HAND', 'PECO', 'CHEP', 'STORAGE', 'RENEWAL'))
Query,
COMPANY | CUSTOMER | REV_CODE | REV_AMT
---------------------------------------------------
01 | 101962 | HAND | 253.377
01 | 101962 | PECO | 60
01 | 101962 | CHEP | 1632
01 | 101962 | STORAGE | 2700
01 | 101962 | RENEWAL | 60
---------------------------------------------------
Output with my current query,
COMPANY | CUSTOMER | HAND | PECO | CHEP | STORAGE | RENEWAL
--------------------------------------------------------------------------
01 | 101962 | 253.377 | 60 | 1632 | 2700 | 60
Trying to get the output to show as
COMPANY | CUSTOMER | EXTRA | STORAGE | RENEWAL
------------------------------------------------------------------
01 | 101962 | 1945.377 | 2700 | 60
Thank you for taking the time to assist.

Instead of select * from ( at the very beginning of your query, write
select company, customer, hand + peco + chep as extra, storage, renewal
from (
.........
If you expect that any of HAND, PECO or CHEP may be NULL, wrap each of them individually within NVL(...., 0) (if, in fact, NULL is to be interpreted as zero; otherwise, leave as is, and the result will be NULL if at least one of the terms is NULL).

Related

Filter on date relative to today but the dates are in separate fields

I have a table where the date parts are in separate fields and I am struggling to put a filter on it (pulling all the data is so much that it basically times out).
How can I write a sql query to pull the data for only the past 7 days?
| eventinfo | entity | year | month | day |
|------------|-------------------------|------|-------|-----|
| source=abc | abc=030,abd=203219,.... | 2022 | 08 | 07 |
| source=abc | abc=030,abd=203219,.... | 2022 | 08 | 05 |
| source=abc | abc=030,abd=203219,.... | 2022 | 07 | 33 |
Many thanks in advance.
You can use concatenation on your columns, convert them to date and then apply the filter.
-- Oracle database
select *
from event
where to_date( year||'-'||month||'-'||day,'YYYY-MM-DD') >= trunc(sysdate) - 7;

SQL Query to select specific records from dates

I have a table that has like a list of codes and the start and end date that the code was active. I want to select the most recent active codes. Which is simple enough but the part I'm getting stuck with is that you can have the same code with overlapping dates which means both are active and I'd need to select all these records. Or you can have the same codes but the dates follow on which means the previous one is no longer active and I want to ignore this.
See example of table below:
In the table below I'd essentially need to say okay this table if you have two of the same codes but the dates follow on then take most recent, if the dates overlap then select...
ID | Code | Start Date | End Date | I need to select
01 | A110 | 15/01/21 | NULL | select
02 | A110 | 14/05/19 | NULL | select
03 | A110 | 10/10/18 | 13/05/19 | Ignore
03 | B200 | 15/01/21 | NULL | select
04 | B200 | 10/12/20 | 14/01/21 | Ignore
05 | C600 | 15/01/21 | NULL | Select
to me it looks like
SELECT *
FROM TABLE
WHERE END_DATE IS NULL
but maybe i got the question wrong, it really isnt clear at the moment

How can I count replicate values across columns?

I have a fairly simple problem that I need to solve by SQL>
There is a database that has several columns of values that are duplicate. I am trying to count the number of values that occurs across those columns. I need to know how many values for each category. For example, if there is a column for different types of cookies that one bakery sells; chocolate and raisin and another column has types of cookies that other bakery sells including chocolate and raisin, I want to know how many chocolate cookies all the bakeries sell.
My query:
SELECT types_cookies_store01. COUNT(*) FROM cookies
GROUP BY types_cookies_store01
This code is returning only one column's count but how can I know the count across all the columns?
Thank you!
Your data model is poor!
Assuming it is equal to this:
| types_cookies_store01 | types_cookies_store02 | types_cookies_store03 |
|-----------------------|-----------------------|-----------------------|
| A | | |
| C | | |
| | A | |
| A | | |
| B | | |
| | B | |
| | | B |
| C | | |
| | C | |
Don't use a new column for each store, use one column to specify the store:
| Cookietype | Store |
|------------|-------|
| A | 01 |
| A | 03 |
| B | 02 |
| B | 01 |
| A | 02 |
| C | 02 |
| D | 02 |
| B | 03 |
| B | 01 |
| A | 03 |
| C | 03 |
Now you can group by Cookietype to get sales of all stores.
Select Cookietype, Count(Cookietype) as CountOfCookieType
From SalesTable
Group By Cookietype
Or to get sales by store add the Store to the group by:
Select Cookietype, Store, Count(Cookietype) as CountOfCookieType
From SalesTable
Group By Cookietype, Store
With that data model you can add (almost) unlimited new stores.
With your data model you need a new column for each new store and tables are limited to 255 columns!
You can work around your poor model, by using a union query:
SELECT UnionCols.Cookietype, COUNT(*)
FROM (
SELECT types_cookies_store01 As Cookietype, 'Store01' As Store
FROM cookies
WHERE Not types_cookies_store01 Is Null
UNION ALL
SELECT types_cookies_store02 As Cookietype, 'Store02' As Store
FROM cookies
WHERE Not types_cookies_store02 Is Null
UNION ALL
SELECT types_cookies_store03 As Cookietype, 'Store03' As Store
FROM cookies
WHERE Not types_cookies_store03 Is Null
) As UnionCols
Group By UnionCols.Cookietype
But performance will be poor on large data and new stores are hard to add.
It is strongly recommended to improve your data model instead of working around it!
Some links to read:
Database design basics
Access All In One

Access 2016 & SQL: Totaling two columns, then subtracting them

Say I have a MoneyIN and a MoneyOUT column. I wish to total these entire columns up so I have a sum of each, then I wish to subtract the total of the MoneyOUT column from the total of the MoneyIN column. I also want to display a DateOF column and possibly a description (I think I can do that by myself).
This would be the original database where I get my information from:
+-------------+------------------+---------+----------+-----------+
| Location ID | Location Address | Date Of | Money In | Money Out |
+-------------+------------------+---------+----------+-----------+
| 1 | blah | date | 10.00 | 0.00 |
| 2 | blah | date | 2,027.10 | 27.10 |
| 2 | blah | date | 0.00 | 2000.00 |
| 1 | blah | date | 0.00 | 10.00 |
| 3 | blah | date | 5000.00 | 0.00 |
+-------------+------------------+---------+----------+-----------+
I would like to be able to type in a location ID and then have results show up (in this example I type 2 for the location)
+---------+----------+-----------+------+
| Date Of | Money In | Money Out | |
+---------+----------+-----------+------+
| date | 2027.10 | 27.10 | |
| date | 0 | 2000 | |
| Total: | 2027.10 | 2027.10 | 0 |
+---------+----------+-----------+------+
I have tried other solutions (One of which was pointed out below), however, they don't show the sum of each entire column, they simply subtract MoneyOUT from MoneyIN for each row. As of now, I am trying to do this in a query, but if there is a better way, please elaborate.
I am extremely new to SQL and Access, so please make the explanation understandable for a beginner like me. Thanks so much!
This is a table referred to below.
+-------------+-------+----------+-----------+-----------+
| Location ID | Date | Money IN | Money Out | Total Sum |
+-------------+-------+----------+-----------+-----------+
| 1 | date | 300 | 200 | |
| 1 | date | 300 | 200 | |
| 1 | date | 300 | 200 | |
| 1 | total | 900 | 600 | 300 |
+-------------+-------+----------+-----------+-----------+
The following should give you what you want:
SELECT DateOf, MoneyIn, MoneyOut, '' AS TotalSum FROM YourTable
UNION
SELECT 'Total', SUM(MoneyIn) AS SumIn, SUM(MoneyOut) AS SumOut,
SUM(MoneyIn - MoneyOut) AS TotalSum FROM YourTable
Edit:
You do not need to alter very much to achieve what you want. In order to get Access to prompt for a parameter when running a query, you give a name for the parameter in square brackets; Access will then pop-up a window prompting the user for this value. Also this parameter can be used more than once in the query, without Access prompting for it multiple times. So the following should work for you:
SELECT DateOf, MoneyIn, MoneyOut, '' AS TotalSum
FROM YourTable
WHERE LocationID=[Location ID]
UNION
SELECT 'Total', SUM(MoneyIn) AS SumIn, SUM(MoneyOut) AS SumOut,
SUM(MoneyIn - MoneyOut) AS TotalSum FROM YourTable
WHERE LocationID=[Location ID];
However, looking at your table design, I strongly encourage you to change it. You are including the address on every record. If you have three locations, but 100 records, then on average you are unnecessarily repeating each address more than 30 times. The "normal" way to avoid this would be to have a second table, Locations, which would have an ID and an Address field. You then remove address from YourTable, and in its place create a one-to-many relationship between the ID in Locations and the LocationID in YourTable.
It's a little unclear exactly what you expect without sample data, but I think this is what you want:
SELECT DateOf, SUM(MoneyIN) - SUM(MoneyOut)
FROM YourTable
GROUP BY DateOf
This will subtract the summed total of MoneyOut from MoneyIn at each distinct DateOf
Updated Answer
A UNION will let you append a 'Totals' record to the bottom of your result set:
SELECT *
FROM (
SELECT CAST(DateOf as varchar(20)) as DateOf, MoneyIn, MoneyOut, '' as NetMoneyIn
FROM YourTable
UNION
SELECT 'Total:', SUM(MoneyIn), SUM(MoneyOut), SUM(MoneyIN) - SUM(MoneyOut)
FROM YourTable
) A
ORDER BY CASE WHEN DateOf <> 'Total:' THEN 0 ELSE 1 END, DateOf
Some notes.. I used a derived table to ensure that the 'Total' record is last. Also casted DateOf to a string (assuming it is a date), otherwise you will have issues writing the string 'Total:' to that column.

T-SQL aggregate over contiguous dates more efficiently

I have a need to aggregate a sum over contiguous dates. I've seen solutions to similar problems that will return the start and end dates, but don't have a need to aggregate the data between those ranges. It's further complicated by the extremely large amounts of data involved, to the point that a simple self join takes an impractical amount of time (especially since the start and end date fields are unindexed)
I have a solution involving cursors, but I've generally been led to believe that cursors can always be more efficiently replaced with joins that will execute faster, but so far every solution I've tried with a query anywhere close to giving me the data I need takes an hour at least, and my cursor solutions takes about 10 seconds. So I'm asking if there is a more efficient answer.
And the data includes both buy and sell transactions and each row of aggregated contiguous dates returned also needs to list the transaction ID of the last sell that occurred before the first buy of the contiguous set of buy transactions.
An example of the data:
+------------------+------------+------------+------------------+--------------------+
| TRANSACTION_TYPE | TRANS_ID | StartDate | EndDate | Amount |
+------------------+------------+------------+------------------+--------------------+
| sell | 100 | 2/16/16 | 2/18/18 | $100.00 |
| sell | 101 | 3/1/16 | 6/6/16 | $121.00 |
| buy | 102 | 6/10/16 | 6/12/16 | $22.00 |
| buy | 103 | 6/12/16 | 6/14/16 | $0.35 |
| buy | 104 | 6/29/16 | 7/2/16 | $5.00 |
| sell | 105 | 7/3/16 | 7/6/16 | $115.00 |
| buy | 106 | 7/8/16 | 7/9/16 | $200.00 |
| sell | 107 | 7/10/16 | 7/13/16 | $4.35 |
| sell | 108 | 7/17/16 | 7/20/16 | $0.50 |
| buy | 109 | 7/25/16 | 7/29/16 | $33.00 |
| buy | 110 | 7/29/16 | 8/1/16 | $75.00 |
| buy | 111 | 8/1/16 | 8/3/16 | $0.33 |
| sell | 112 | 9/1/16 | 9/2/16 | $99.00 |
+------------------+------------+------------+------------------+--------------------+
Should have results like the following:
+------------+------------+------------------+--------------------+
| Last_Sell | StartDate | EndDate | Amount |
+------------+------------+------------------+--------------------+
| 101 | 6/10/16 | 6/14/18 | $22.35 |
| 101 | 6/29/16 | 7/2/16 | $5.00 |
| 105 | 7/8/16 | 7/9/16 | $200.00 |
| 108 | 7/25/16 | 8/3/16 | $108.33 |
+------------------+------------+------------+--------------------+
Right now I use queries to split the data into buys and sells, and just walk through the buy data, aggregating as I go, inserting into the return table every time I find a break in the dates, and I step through the sell table until I reach the last sell before the start date of the set of buys.
Walking linearly through cursors gives me a computational time of n. Even though cursors are orders of magnitude less efficient, it's still calculating in n, while I suspect the joins I would need to do would give me at least n log n. With the ridiculous amount of data I'm working with, the inefficiencies of cursors get swamped if it goes beyond linear time.
If I assume that the transaction id increases along with the dates, then you can get the last sales date using a cumulative max. Then, the adjacency can be found by using similar logic, but with a lag first:
with cte as (
select t.*,
sum(case when transaction_type = 'sell' then trans_id end) over
(order by trans_id) as last_sell,
lag(enddate) over (partition by transaction_type order by trans_id) as prev_enddate
from t
)
select last_sell, min(startdate) as startdate, max(enddate) as enddate,
sum(amount) as amount
from (select cte.*,
sum(case when startdate = dateadd(day, 1, prev_enddate) then 0 else 1 end) over (partition by last_sell order by trans_id) as grp
from cte
where transaction_type = 'buy'
) x
group by last_sell, grp;