Oracle Pivot - converting values into columns - oracle11g

I have table in Oracle 11g with 3 fields:
STUDYID | STUDY_PARAMETER | STUDY_VALUE
5268 | Age Group | ADULT (18-65)
5269 | Age Group | ADULT (18-65)
5270 | Age Group | ADULT (18-65)
5271 | Age Unit | ADULT (18-65)
1668A | Trial Type | ADULT (18-65)
5273 | Trial Type | Dispensing
5345 | Age Unit | Years
1668AC | Age Group | ADULTS (18-39)
So, what I need is to display values in this order:
STUDY_ID | AGE_GROUP | AGE_UNIT | TRIAL_TYPE
5268 | ADULT (18-65) | Years | Dispensing
5269 | ADULT (18-65) | (null) | (null)
1668AC | ADULTS (18-39)| Years | Non - Dispensing
and so on.
What I have so far is:
SELECT *
FROM (
SELECT STUDYID, STUDY_VALUE, STUDY_PARAMETER
FROM RD.STUDY_INFO
)
PIVOT (
SUM(STUDY_VALUE)
FOR (STUDY_PARAMETER)
IN (
'Age Unit' AS AGE_UNIT,
'Age Group' AS AGE_GROUP,
'Trial Type' AS TRIAL_TYPE
)
);
I learned this from examples on the net but I am not sure if I can use SUM() like this...?!
I get this error:
ORA-01722: invalid number
01722. 00000 - "invalid number"
Does anyone see what I am doing wrong?

Since the STUDY_VALUE column appears to be a string, you will need to use either the max() or min() aggregate function on the values:
SELECT *
FROM
(
SELECT STUDYID, STUDY_VALUE, STUDY_PARAMETER
FROM STUDY_INFO
)
PIVOT
(
MAX(STUDY_VALUE)
FOR (STUDY_PARAMETER) IN ('Age Unit' AS AGE_UNIT,
'Age Group' AS AGE_GROUP,
'Trial Type' AS TRIAL_TYPE)
);
See SQL Fiddle with Demo

You can try this query.
SELECT
ID,
MAX(Case When parameter='Age Group' then Value else '0' end) AS AgeGroup,
MAX(Case When parameter='Trial Type' then Value else '0'end)AS TrialType,
MAX(Case When parameter='Age Unit' then Value else '0'end)AS AgeUnit
FROM teststack
GROUP BY ID
ORDER BY ID

Related

How can I create a multiple loop in R?

I am working with a database of daily deaths of a country, so I need to create a database that contains the aggregated data of daily deaths by day, month and state. My database (def_2020) is something like this:
|--------------|------------|-------|
| State | Month | Day |
|--------------|------------|-------|
| state1 | jan | 1 |
|--------------|------------|-------|
| state1 | jan | 1 |
|--------------|------------|-------|
| . | . | . |
|--------------|------------|-------|
| . | . | . |
|--------------|------------|-------|
| state2 | dic | 4 |
|--------------|------------|-------|
I have 24 states (100.000 obs), of diferent days and months of death. I need to get something like this:
|--------------|------------|-------|-------|
| State | Month | Day | Deaths|
|--------------|------------|-------|-------|
| state1 | jan | 1 | 25 |
|--------------|------------|-------|-------|
| state1 | jan | 2 | 35 |
|--------------|------------|-------|-------|
| . | . | . | |
|--------------|------------|-------|-------|
| . | . | . | |
|--------------|------------|-------|-------|
| state2 | dic | 4 | |
|--------------|------------|-------|-------|
I am new to R, so I create loop like this:
day <- c(1:31)
death_state1 <- NULL
for (i in day) {
death_state_1[i] <- sum(with(def2020 %>% filter(State == "state1", Month =="jan"), Day == i))
}
But I need to optimize this loop to get a dataframe by month (columns), days (rows) and states (also rows). Help me please, I'm still new with this.
It looks like you are using a mixture of base R and dplyr syntax (the pipe %>% and filter are exports from the dplyr package.)
dplyr has its own syntax for grouped operations that allows you to avoid defining explicit loops. You use group_by() to group your data and summarize() to define variables containing the results of dimension-reducing functions like mean(), min(), n(), etc.
def_2020 %>%
group_by(State, Month, Day) %>%
summarize(Deaths = n())
With base R, we can use aggregate
aggregate(Deaths ~ ., transform(def_2020, Deaths = 1), FUN = sum)

Split data in SQLite column

I have a SQLite database that looks similar to this:
---------- ------------ ------------
| Car | | Computer | | Category |
---------- ------------ ------------
| id | | id | | id |
| make | | make | | record |
| model | | price | ------------
| year | | cpu |
---------- | weight |
------------
The record column in my Category table contains a comma separated list of the table name and id of the items that belong to that Category, so an entry would look like this:
Car_1,Car_2.
I am trying to split the items in the record on the comma to get each value:
Car_1
Car_2
Then I need to take it one step further and split on the _ and return the Car records.
So if I know the Category id, I'm trying to wind up with this in the end:
---------------- ------------------
| Car | | Car |
---------------| -----------------|
| id: 1 | | id: 2 |
| make: Honda | | make: Toyota |
| model: Civic | | model: Corolla |
| year: 2016 | | year: 2013 |
---------------- ------------------
I have had some success on splitting on the comma and getting 2 records back, but I'm stuck on splitting on the _ and making the join to the table in the record.
This is my query so far:
WITH RECURSIVE record(recordhash, data) AS (
SELECT '', record || ',' FROM Category WHERE id = 1
UNION ALL
SELECT
substr(data, 0, instr(data, ',')),
substr(data, instr(data, ',') + 1)
FROM record
WHERE data != '')
SELECT recordhash
FROM record
WHERE recordhash != ''
This is returning
--------------
| recordhash |
--------------
| Car_1 |
| Car_2 |
--------------
Any help would be greatly appreciated!
If your recursive CTE works as expected then you can split each of the values of recordhash with _ as a delimiter and use the part after _ as the id of the rows from Car to return:
select * from Car
where id in (
select substr(recordhash, 5)
from record
where recordhash like 'Car%'
)

How to query dates using sqlite between

Im trying to query a range between dates but
i have tried using the date datatype,store the values in the date column as string and also use the date function but not getting the desired results
CREATE TABLE PvcTable (
date TEXT NOT NULL,
Wardname TEXT NOT NULL,
Puname TEXT NOT NULL,
PvcReceived TEXT,
PRIMARY KEY (
date,
Wardname,
Puname
)
);
the expected result is when i query let say
SELECT * from pvctable
where date between '2019-1-1' and '2019-12-1'
order by WARDNAME
i should get all the records between jan - dec 2019, but instead i get
this.only 3 records return.
date Wardname Puname PvcReceived
2019-10 01Alagarno 010KANGARWAPRISCHII 58
2019-11 02Baga 001MILEFOUR 58
2019-12 02Baga 002DARBASHATA 58
It is important to make sure that the dates in the table have the proper format YYYY-MM-DD which is comparable.
From the sample data you posted I see that there is no DD part in the dates, which is fine if you don't need it, because YYYY-MM is also comparable.
But if there is no DD part then in your query you should not compare the date column with dates containing this part, but with dates in the format YYYY-MM.
So change to this:
SELECT * from pvctable
where date between '2019-01' and '2019-12'
order by WARDNAME
See the demo.
Results:
| date | Wardname | Puname | PvcReceived |
| ------- | ---------- | ------------------- | ----------- |
| 2019-01 | 01Alagarno | 001ALAGARNOPRISCH | 58 |
| 2019-10 | 01Alagarno | 010KANGARWAPRISCHII | 58 |
| 2019-11 | 02Baga | 001MILEFOUR | 58 |
| 2019-12 | 02Baga | 002DARBASHATA | 58 |

How to make a query for getting the specific rows with the latest time column value

Below is my sample data, I would like to get the host:value pair with the latest time.
+------+-------+-------+
| HOST | VALUE | TIME |
+------+-------+-------+
| A | 100 | 13:40 |
| A | 150 | 13:00 |
| A | 222 | 13:23 |
| B | 210 | 13:55 |
| B | 300 | 13:44 |
+------+-------+-------+
Wanted to get only rows with the latest time value for the each host column value.
The result should be like:
A 150 13:40
B 210 13:55
I think there are several analytical function to achieve this requirement in Oracle but I'm not sure what can I do in SQLite.
Can you let me know how I can make a query?
Here is an ANSI-compliant way of performing your query which should run on all versions of SQLite. For a potentially shorter solution see the answer by #CL.
SELECT t1.HOST || '-' || t1.VALUE || '-' || t1.TIME AS HOSTVALUETIME
FROM table t1 INNER JOIN
(
SELECT HOST, MAX(TIME) AS MAXTIME
FROM table
GROUP BY HOST
) t2
ON t1.HOST = t2.HOST AND t1.TIME = t2.MAXTIME
ORDER BY t1.HOST DESC
Output:
+---------------+
| HOSTVALUETIME |
+---------------+
| A-100-13:50 |
| B-210-13:55 |
+---------------+
In SQLite 3.7.11 or later, MAX() selects from which row in a group the other column values come:
SELECT Host,
Value,
MAX(Time)
FROM TheNameOfThisTableIsSoSecretThatICantTellYou
GROUP BY Host;

use gather of a column with Group By in sql

I have a table in sql that store statistics of visits, and have below columns:
referredURL
searchedWord(words that lead the user to this page)
Hittime
I want a report of this table with below column:
- refferedURL
- recenReferDate
- allSearchedWors
select ReferredURL , MAX (HitTime) as 'Recent refer date'
from statTbl
GROUP BY ReferredURL
I have problem for gathering allSearchedWords, how can I gather all of the searchedWord of a group and use it as a column?
If you're using SQL Server try
SELECT t.ReferredURL,
MAX (t.HitTime) 'Recent refer date',
(
STUFF((SELECT DISTINCT ',' + SearchedWord
FROM statTbl
WHERE ReferredURL = t.ReferredURL
FOR XML PATH('')) , 1 , 1 , '' )
) 'Searched Words'
FROM statTbl t
GROUP BY t.ReferredURL;
If you need all words to be present even duplicates ditch DISTINCT from the subquery.
Here is SQLFiddle example for SQL Server
If it is MySql then
SELECT t.ReferredURL,
MAX(t.HitTime) 'Recent refer date',
GROUP_CONCAT(DISTINCT(SearchedWord)) 'Searched Words'
FROM statTbl t
GROUP BY t.ReferredURL;
Here is SQLFiddle example for MySql
Sample data
| REFERREDURL | HITTIME | SEARCHEDWORD |
-----------------------------------------------------------
| url1 | May, 22 2013 00:00:00+0000 | apple |
| url1 | May, 22 2013 12:00:00+0000 | banana |
| url1 | May, 22 2013 18:00:00+0000 | pear |
| url1 | May, 22 2013 18:05:00+0000 | apple |
| url2 | May, 22 2013 23:00:00+0000 | apple |
Sample output
| REFERREDURL | RECENT REFER DATE | SEARCHED WORDS |
----------------------------------------------------------------
| url1 | May, 22 2013 18:05:00+0000 | apple,banana,pear |
| url2 | May, 22 2013 23:00:00+0000 | apple |

Resources