I have a dataset in the format;
And need to it in the format;
I have had various attempts at pivot and unpivot for the last week but am not a real programmer and know when to ask a grownup for help.
The database is running on MSSQL 2012 and the dataset will consist of 14 Mode_Antibiotics, ModeQualifier and ModeMIC entries per ClinicalTrialID with a total of approximately 3000 ClinicalTrialIDs.
It's going to be hard to do it in pure SQL dynamically. If I new T-SQL, I would write a table function returning the required dataset. However, in a pure ANSI SQL, since you don't have that many columns, you could use something like this:
SELECT
ClinicalTrialID,
MAX(CASE
WHEN Mode_Antibiotic = 'Amikacin'
THEN Mode_Antibiotic
END) Antibiotic1,
MAX(CASE
WHEN Mode_Antibiotic = 'Amikacin'
THEN Mode_Qualifier
END) Qualifier1,
MAX(CASE
WHEN Mode_Antibiotic = 'Amikacin'
THEN Mode_MIC
END) MIC1,
MAX(CASE
WHEN Mode_Antibiotic = 'Ampicillin'
THEN Mode_Antibiotic
END) Antibiotic2,
MAX(CASE
WHEN Mode_Antibiotic = 'Ampicillin'
THEN Mode_Qualifier
END) Qualifier2,
MAX(CASE
WHEN Mode_Antibiotic = 'Ampicillin'
THEN Mode_MIC
END) MIC2,
-- repeat 12 times with the remaining antibiotics
FROM t
GROUP BY ClinicalTrialID;
Hope that helps.
Related
I have data as below :
and I'm trying to get it into this shape:
I've seen this solution Multi Column Pivot SQL Server but it uses hardcoding values, which is not my case, hence I think it would be safer to use dynamic pivot.
Can anyone help?
You can use conditional aggregation:
select id_card_no,
max(case when city_rank = 1 then city end) as city_1,
max(case when city_rank = 1 then visitratio end) as visitratio_1,
max(case when city_rank = 2 then city end) as city_2,
max(case when city_rank = 2 then visitratio end) as visitratio_2
from t
group by id_card_no;
This is standard SQL and should work in any database.
I have an access to Oracle server. There is a table on the Oracle server called Transactions which contains the following data:
I don't known the number of values, so we need to implement dynamic sql in Oracle.
I need to pivot that data so the results are:
Any suggestions?
You can use conditional aggregation:
select subno,
sum(case when offer = 'offer1' then 1 else 0 end) as offer1,
sum(case when offer = 'offer2' then 1 else 0 end) as offer2,
sum(case when offer = 'offer3' then 1 else 0 end) as offer3
from t
group by subno;
I need to subtract the raw data using same column.
The formula is
(provisioned port-shelf x 16 ) - (provisioned summary - provisioned RCMM).
Anyone have idea so that i can improve my query.
Assuming I'm understanding your question correctly, given your sample data, your expected result should be 16 for A. Assuming so, here's one option using conditional aggregation:
select ne,
max(case when type = 'port-shelf' then 16 * provisioned else 0 end) -
max(case when type = 'summary' then provisioned else 0 end) -
max(case when type = 'rcmm' then provisioned else 0 end) as free_slot,
insert_date
from yourtable
group by ne, insert_date
I am having trouble formulating a query to get the desired output.
This query involves one table and two columns.
First column bld_stat has 4 different values Private, public, Public-Abandoned, Private-Abandoned the other column bld_type, single_flr, multi_flr, trailer, Whs.
I need to get results that look like this:
So far I can get the first two columns but after that I have not been able to logically get a query to work
SELECT bld_stat, COUNT(grade) AS single_flr
FROM (SELECT bld_stat,bld_type
FROM bld_inventory WHERE bld_type = 'single_flr') AS grade
GROUP BY bld_stat,bld_type,grade
The term you are going for is pivoting. I think this should work...no need for the subquery, and I've changed your group by to only bld_stat
SELECT bld_stat,
sum(case when bld_type = 'singl_flr' then 1 else 0 end) AS single_flr,
sum(case when bld_type = 'multi_flr' then 1 else 0 end) AS multi_flr,
sum(case when bld_type = 'trailer' then 1 else 0 end) AS trailer,
sum(case when bld_type = 'whs' then 1 else 0 end) AS WHS
FROM bld_inventory
GROUP BY bld_stat
I have data that looks much like this (3 columns of data), unfortunately I can't get it to display properly
NCR NO LU_NAME KEY_REF
100001 Project PROJECT_ID=ID#^
100001 SupplierInfo SUPPLIER_ID=UNIQUESUPPLIERNUMBER^
100001 PurchaseOrder ORDER_NO=UNIQUEORDERNO^
100196 PurchaseReceipt UNIQUE PURCHASE RECEIPT
100511 InventoryPart CONTRACT=UNIQUECONTRACTNO
What I want is to have one record for each NCR number and a column of data for Project, SupplierInfo, etc, which contains the unique Key_Ref. Let's say the table name is OC. Can someone assist with the code to do this?
This type of data transformation is called a pivot, some database products have a function that can do this for you.
If you are working in a database that does not have a pivot function, then you can use an aggregate function with a CASE expression:
select ncr_no,
max(case when LU_NAME = 'Project' then KEY_REF end) Project,
max(case when LU_NAME = 'SupplierInfo' then KEY_REF end) SupplierInfo,
max(case when LU_NAME = 'PurchaseOrder' then KEY_REF end) PurchaseOrder,
max(case when LU_NAME = 'PurchaseReceipt' then KEY_REF end) PurchaseReceipt,
max(case when LU_NAME = 'InventoryPart' then KEY_REF end) InventoryPart
from yourtable
group by ncr_no
See SQL Fiddle with Demo.
The above will work great with a known number or finite number of LU_NAME values, if you will have an unknown number then you will need to implement dynamic SQL but that code will vary depending on your database.