I have the following result set:
Region
North
South
West
Reason
New Admission
New Admit
Other
Amount
5.00
6.00
8.00
What I am needing to do is combine just the "New Admission" and "New Admit" values from the Reason column with the corresponding amounts.
So what I would like to end up with is this result set:
Region
North
South
West
Reason
New Admission/New Admit
Other
Amount
11.00
8.00
The query that I have created to build the first result set is as follows:
SELECT Region, Reason, CAST(SUM(Amount) AS Decimal(18,2)) AS Amount FROM Table GROUP BY Region, Reason
Just wondering if anyone else has insight into this. I am using SQL Server 2019
This is a screen shot of the actual data from my table
Use a case expression:
SELECT Region,
(CASE WHEN Reason IN ('New Admission', 'New Admit') then 'New Admission/New Admit'
ELSE 'Other'
END) as Reason,
CAST(SUM(Amount) AS Decimal(18,2)) AS Amount
FROM Table
GROUP BY Region,
(CASE WHEN Reason IN ('New Admission', 'New Admit') then 'New Admission/New Admit'
ELSE 'Other'
END);
Related
I am working with a table that currently uses multiple CASE expressions to define the behavior of one of the columns, i.e.:
SELECT
Employee
,Company
,Department
,Area
,Flag = CASE
WHEN Company = 'Amazon' and Department in ('IT', 'HR')
THEN 0
WHEN Department = 'Legal'
THEN 1
WHEN Area = 'Cloud'
THEN 1
ELSE 0
END
FROM Table1
Which would result in something like the following dummy data:
Employee
Company
Department
Area
Flag
Cindy
Amazon
IT
Support
0
Jack
Amazon
HR
Support
0
Bob
Microsoft
Legal
Contracts
1
Joe
Amazon
Legal
Research
1
Lauren
Google
IT
Cloud
1
Jane
Apple
UX
Research
0
I am trying to simplify the Flag expression by using an auxiliary Mappings table that has the following structure, in order to get the value for the Flag column:
Company
Department
Area
Flag
Amazon
IT
NULL
0
Amazon
HR
NULL
0
NULL
Legal
NULL
1
NULL
NULL
Cloud
1
The NULL values mean the column could take any value. Is it possible to achieve this without falling into multiple CASE statements?
I have a simple table containing data of a batch of students and
their score for different years (the data may not be realistic, but it's
just an example).
Name Dept HOD Year1 Year2 Year3 Year4
Sam Science Christie 76.23 34.65 45.67 23.45
Mike Science Christie 57.987 26.98 43.98 78.34
Bonny Maths Christie 64.87 67.23 34.09 12.87
Ben English Simon 43.98 54.76 55.87 76.87
Now the requirement is, considering 2015 as point of reference, if the
user enters 2018 and wants data for Sam in Science department under
Christie's management, then the value from column Year3 (i.e.2018-2015)
is expected for all those conditions.
For example -
[case
when Name='Sam' and Dept='Science' and HOD='Christie' then Year*
when Name='Bonny' and Dept='Maths' and HOD='Christie' then Year*
when Name='Ben' and Dept='English' and HOD='Simon' then Year*
end]
I have already tried the sql -
select Value from (
select concat('Year', abs(2018-2015)) as Value from Class where
Name='Sam' and Dept='Science' and HOD='Christie')
So, when I am hardocoding Year3 in the above query instead of the formula,
it's working fine. When I separately firing computed Value, its is giving
output.
select concat('Year', abs(2018-2015) as Value from Class
But when I am integrating the two , my query is only giving Year3 as a
string. Whereas I want to pick the value for that column.
May be I am doing something wrong, I am not sure, but any suggestion is
welcome to solve this problem.
I came across a post advising use of coalesce() for dynamically calling a
column. so i tried that too -
select coalesce(Value, T.period,0) as Value from (
select concat('Year',(abs(2018-2015)))as period, Name, Dept, HOD from
Class where Name='Sam' and Dept='Science' and HOD='Christie') as T
where T.Name='Sam' and T.Dept='Science' and T.HOD='Christie'
But I am receiving error -
Error while compiling statement: FAILED: SemanticException [Error 10004]:
line 1:16 Invalid table alias or column reference 'Value': (possible
column names are: period, Name, Dept, HOD)
Is this what you want?
select (case when #year = 2016 then year1
when #year = 2017 then year2
when #year = 2018 then year3
when #year = 2019 then year4
end) as value
from class c
where hod = 'Christie'
Assume that we have a table where we have one field for zip code and the rest are binary fields (1 or NULL) with names corresponding to various places. For example, imagining the table has 201 fields with the first field titled "zip code" containing zip codes and the latter being 200 binary value fields titled with city names: Chicago, New York, Houston, etc.
Assume that row one contains zip code 11373. While one could use coalesce to find the first non-null value and return "New York" another value like "Elmhurst" may also be true.
zip_code new_york chicago elmhurst dover maspeth
10001 1 NULL NULL NULL NULL
07801 NULL NULL NULL 1 NULL
11373 1 NULL 1 NULL 1
The goal is to map the column names to each respective zip code and get an output like so:
zip_code city
10001 new_york
07801 dover
11373 new_york
11373 elmhurst
11373 maspeth
Any help is much appreciated.
This is a great use case for SQL UNPIVOT:
SELECT unpvt.*
FROM
#x UNPIVOT (v FOR statename IN (new_york, chicago,elmhurst, dover, maspeth)) AS unpvt
One method uses union all:
select zip_code, 'New York' as city from t where new_york = 1
union all
select zip_code, 'Chicago' as city from t where chicago = 1
union all
. . .
I have 4 types of values in one single column and looking to split those values in 4 different columns using SQL.
This is my table:
Name Car
---------------
John Tesla
John Renault
John Mercedes
John VW
And I would like have this result:
Name Car1 Car2 Car3 Car4
-----------------------------------------
John Tesla Renault Mercedes VW
Can anyone help?
Thanks
You can use PIVOT for this.
Your example almost exactly matches the Microsoft help page, comment if you need more help-
https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
Right, I figured it out. I didn't realise that when you execute a SELECT function in SQL, you are actually creating a new column. I thought you just merged columns together.
So my code was simply:
SELECT
(CASE WHEN car = "Tesla" THEN name ELSE Null END) as Car_type_1
(CASE WHEN car = "Renault" THEN name ELSE Null END) as Car_type_2
(CASE WHEN car = "Mercedes" THEN name ELSE Null END) as Car_type_3
(CASE WHEN car = "VW" THEN name ELSE Null END) as Car_type_4
Should have learnt the basics of SQL! Hope this helps anybody else in my situation.
I want to do some statistic for the Point in my appliation,this is the columns for Point table:
id type city
1 food NewYork
2 food Washington
3 sport NewYork
4 food .....
Each point belongs to a certain type and located at the certain city.
Now I want to caculate the numbers of points in different city for each type.
For example, there are two types here :food and sport.
Then I want to know:
how many points of `food` and `sport` at NewYork
how many points of `food` and `sport` at Washington
how many points of `food` and `sport` at Chicago
......
I have tried this:
select type,count(*) as num from point group by type ;
But I can not group the by the city.
How to make it?
Update
id type city
1 food NewYork
2 sport NewYork
3 food Chicago
4 food San
And I want to get something like this:
NewYork Chicago San
food 2 1 1
sport 1 0 0
I will use the html table and chart to display these datas.
So I need to do the counting, I can use something like this:
select count(*) from point where type='food' and city ='San'
select count(*) from point where type='food' and city ='NewYork'
....
However I think this is a bad idea,so I wonder if I can use the sql to do the counting.
BTW,for these table data,how do people organization their structure using json?
this's what you want:
SELECT city,
COUNT(CASE WHEN [type] = 'food' THEN 1 END) AS FoodCount,
COUNT(CASE WHEN [type] = 'sport' THEN 1 END) AS SportCount
FROM point
GROUP BY city
UPDATE:
To get the results in an aggregated row/column format you need to use a pivot table. In Access it's called a Crosstab query. You can use the Crosstab query wizard to generate the query via a nice UI or cut straight to the SQL:
TRANSFORM COUNT(id) AS CountOfId
SELECT type
FROM point
GROUP BY type
PIVOT city
The grouping is used to count the number of Id's for each type. The additional PIVOT clause groups the data by city and displays each grouping in a separate column. The end result looks something like this:
NewYork Chicago San
food 2 1 1
sport 1 0 0