I have the following table
EURtoUSD column always shows the same value for all of the dates across the different groups in car.
How can I create the following table?
Use conditional aggregation:
select date, eurtousd,
max(case when car = 'Audi' then priceeur end) as audi,
max(case when car = 'BMW' then priceeur end) as bmw,
max(case when car = 'MB' then priceeur end) as mb
from t
group by date, eurtousd;
Related
I need to create a View from a table that sums the total from a certain column (DT_Units) and creates 3 new columns with the correct sum for Total_Hourly_Units, Total_Standby_Units and Total_Footage_Units. How do I combine this query to make the 3 new columns on the same view with the correct value for each new column.
This is what I have.
select DT_Company,CollarYear_D, DT_Month ,Sum(convert(float,DT_Units)) as Total_Hourly_Units
from dbo.DRILLTRACKING
where DT_Category = 'Hourly'
group by DT_Company, CollarYear_D, DT_Month
select DT_Company,CollarYear_D, DT_Month,Sum(convert(float,DT_Units)) as Total_Standby_Units
from
dbo.DRILLTRACKING
where DT_Category= 'Standby'
group by DT_Company, CollarYear_D, DT_Month
select DT_Company,CollarYear_D, DT_Month,Sum(convert(float,DT_Units)) as Total_Footage_Units
from
dbo.DRILLTRACKING
where DT_Category= 'Footage'
group by DT_Company, CollarYear_D, DT_Month
You want a conditional aggregation. Most DBMSes will understand the query
select DT_Company,CollarYear_D, DT_Month ,
Sum(case when DT_Category = 'Hourly' then convert(float,DT_Units) end) as Total_Hourly_Units,
Sum(case when DT_Category = 'Standby' then convert(float,DT_Units) end) as Total_Standby_Units,
Sum(case when DT_Category = 'Footage' then convert(float,DT_Units) end) as Total_Footage_Units,
from dbo.DRILLTRACKING
where DT_Category = in ('Hourly', 'Standby', 'Footage')
group by DT_Company, CollarYear_D, DT_Month
I am trying to create a new table by using the dataset in the link below. The return I want to have is like the picture below. Could someone please help?
http://sqlfiddle.com/#!18/766e3/2
You can use conditional aggregation:
SELECT Employeeid,
SUM(CASE WHEN BusinessUniteID = 1 then Scores END) as score_1,
SUM(CASE WHEN BusinessUniteID = 2 then Scores END) as score_2,
SUM(CASE WHEN BusinessUniteID = 3 then Scores END) as score_3
FROM t_d
GROUP BY EmployeeId;
This assumes that you know what all the business unit ids are.
Here is a db<>fiddle.
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.
I have a sql code which I am using to do pivot. Code is as follows:
SELECT DISTINCT PersonID
,MAX(pivotColumn1)
,MAX(pivotColumn2) --originally these were in 2 separate rows)
FROM(SELECT srcID, PersonID, detailCode, detailValue) FROM src) AS SrcTbl
PIVOT(MAX(detailValue) FOR detailCode IN ([pivotColumn1],[pivotColumn2])) pvt
GROUP BY PersonID
In the source data the ID has 2 separate rows due to having its own ID which separates the values. I have now pivoted it and its still giving me 2 separate rows for the ID even though i grouped it and used aggregation on the pivot columns. Ay idea whats wrong with the code?
So I have all my possible detailCode listed in the IN clause. So I have null returned when the value is none but I want it all summarised in 1 row. See image below.
If those are all the options of detailCode , you can use conditional aggregation with CASE EXPRESSION instead of Pivot:
SELECT t.personID,
MAX(CASE WHEN t.detailCode = 'cas' then t.detailValue END) as cas,
MAX(CASE WHEN t.detailCode = 'buy' then t.detailValue END) as buy,
MAX(CASE WHEN t.detailCode = 'sel' then t.detailValue END) as sel,
MAX(CASE WHEN t.detailCode = 'pla' then t.detailValue END) as pla
FROM YourTable t
GROUP BY t.personID
I'm currently working with a data set that has a large number of unique groups, and within each group there could be one to many unique rows, to describe the type of transaction applicable to that group. There are a limited number of types of transactions, and each transaction has a(n):
Amount
Location
Date
The data set would look something like this:
What I would like it to do is combine all groups into a single line, and have three columns for each type of transaction. I am trying to get the end result to look like this:
The closest I have gotten is to try a number of joins, based on the claim number while looking for the unique key. Unfortunately my results end up looking like this:
Any suggestions on how to get each unique Group to have only one row in the results, with the three Types spread out, having three columns each?
You can do this all with conditional aggregation:
select grp,
sum(case when type = 'S' then amount else null end) as type_s_amt,
min(case when type = 'S' then location else null end) as type_s_loc,
min(case when type = 'S' then date else null end) as type_s_dt,
sum(case when type = 'O' then amount else null end) as type_o_amt,
min(case when type = 'O' then location else null end) as type_o_loc,
min(case when type = 'O' then date else null end) as type_o_dt,
sum(case when type = 'F' then amount else null end) as type_f_amt,
min(case when type = 'F' then location else null end) as type_f_loc,
min(case when type = 'F' then date else null end) as type_f_dt
from tbl
group by grp
Fiddle: http://sqlfiddle.com/#!3/f7fae/5/0