My data look like this.
There are three branches, ABC, PQR, and XYZ and each one do three types of businesses, Counter Sale, Online Sale and Van sale. I know how to show this data in a column chart as it is, but my requirement is, I want to sum up the counter sale and the van sale by branch and date, let's call it ABC - Offline Sale, PQR - Offline Sale and XYZ - Offline Sale and then show this in a column chart. I have been after this for two days and couldn't find any help online.
I tried to create calculated fields using case statements but it didn't work as expected and I couldn't really accomplish what I wanted.
Date
Branch
SALE
PROFIT
01-Jan-23
ABC - COUNTER SALE
184518
184518
01-Jan-23
ABC - VAN SALE
0
0
01-Jan-23
ABC - ONLINE
0
0
01-Jan-23
XYZ - COUNTER SALE
144425
144425
01-Jan-23
XYZ - VAN SALE
38163
38163
01-Jan-23
XYZ - ONLINE
0
0
01-Jan-23
PQR - COUNTER SALE
261219
261219
01-Jan-23
PQR - VAN SALE
0
0
01-Jan-23
PQR - ONLINE
53851
53851
02-Jan-23
ABC - COUNTER SALE
280046
280046
02-Jan-23
ABC - VAN SALE
97992
97992
02-Jan-23
ABC - ONLINE
0
0
02-Jan-23
XYZ - COUNTER SALE
276917
276917
02-Jan-23
XYZ - VAN SALE
15815
15815
02-Jan-23
XYZ - ONLINE
0
0
02-Jan-23
PQR - COUNTER SALE
310918
310918
02-Jan-23
PQR - VAN SALE
0
0
02-Jan-23
PQR - ONLINE
129857
129857
03-Jan-23
ABC - COUNTER SALE
197445
197445
03-Jan-23
ABC - VAN SALE
39976
39976
03-Jan-23
ABC - ONLINE
0
0
03-Jan-23
XYZ - COUNTER SALE
206751
206751
03-Jan-23
XYZ - VAN SALE
24715
24715
03-Jan-23
XYZ - ONLINE
0
0
03-Jan-23
PQR - COUNTER SALE
303617
303617
03-Jan-23
PQR - VAN SALE
0
0
03-Jan-23
PQR - ONLINE
189570
189570
04-Jan-23
ABC - COUNTER SALE
203264
203264
04-Jan-23
ABC - VAN SALE
84817
84817
04-Jan-23
ABC - ONLINE
30793
30793
04-Jan-23
XYZ - COUNTER SALE
214984
214984
04-Jan-23
XYZ - VAN SALE
23149
23149
04-Jan-23
XYZ - ONLINE
0
0
04-Jan-23
PQR - COUNTER SALE
287748
287748
04-Jan-23
PQR - VAN SALE
0
0
04-Jan-23
PQR - ONLINE
540781
540781
05-Jan-23
ABC - COUNTER SALE
219093
219093
05-Jan-23
ABC - VAN SALE
44327
44327
05-Jan-23
ABC - ONLINE
104569
104569
05-Jan-23
XYZ - COUNTER SALE
167817
167817
05-Jan-23
XYZ - VAN SALE
96145
96145
05-Jan-23
XYZ - ONLINE
0
0
05-Jan-23
PQR - COUNTER SALE
297488
297488
05-Jan-23
PQR - VAN SALE
0
0
05-Jan-23
PQR - ONLINE
348895
348895
you can create a calculated field as such:
REGEXP_REPLACE(Branch,"VAN|COUNTER","OFFLINE")
Related
users
id
first_name
last_name
email
1
John
Doe
john.doe#john.doe.com
2
Mary
Doe
mary.doe#mary.doe.com
user_details
id
user_id
ethnicity_id
education_id
location_id
1
John
1
1
1
2
Mary
4
3
2
locations
id
location
lat
long
1
London, UK
53.2321
-1.2321
2
Berlin, Germany
52.2321
-1.5345
I have following data table
id - name - city
- 1 George Seattle
- 2 Sam Boston
- 3 John Los Angeles
- 4 Amy Milwaukee
- 5 Eric Chicago
- 6 Nick New York
here i want to consider chicago and Seattle as same city
so the order i want is
id - name - city
- 1 George Seattle
- 5 Eric Chicago
- 6 Nick New York
- 4 Amy Milwaukee
- 3 John Los Angeles
- 2 Sam Boston
so what condition i should use when using order by city Desc
You can use a case when then condition in the order by clause
select id,name, city from
yourtable
order by
(case when city=N'Chicago' then N'Seattle' else city end) desc
I'd suggest that if you need to do this often then consider using a mapping table
I have a dataset which is of following nature. I would like to replace the names in "MAKE" column if the column contains "PQR" per unique country.
country MAKE
1 USA PQR
2 USA ABC
3 UK PQR
4 UK DEF
5 JPN DEF
6 JPN LMN
Desired Output:
country MAKE
1 USA PQR
2 USA PQR
3 UK PQR
4 UK PQR
5 JPN OTHERS
5 JPN OTHERS
One option is conditional aggregation with analytic functions:
SELECT
country,
CASE WHEN SUM(CASE WHEN MAKE = 'PQR' THEN 1 ELSE 0 END) OVER (PARTITION BY country) > 0
THEN 'PQR' ELSE 'OTHERS' END AS MAKE
FROM yourTable
ORDER BY
country;
Demo
I have a table that lists results by sport and start and end dates and win/loss result. I need to calculate the percentage of wins per sport per season_start. This is my table:
table1
******
id sport won playerid season_start season_end updated_date
--- ----- ---- -------- ------------ ------------ ------------
1 rugby Y kato23 2016-01-01 2016-01-31 2016-02-01
2 rugby Y king54 2016-01-01 2016-01-31 2016-02-01
3 rugby N robby1 2016-03-01 2016-03-28 2016-04-01
4 rugby Y kelly2 2016-03-01 2016-03-28 2016-04-01
5 soccer Y kato23 2016-01-01 2016-01-31 2016-02-01
6 soccer Y jeri44 2016-01-01 2016-01-31 2016-02-01
7 soccer N matt24 2016-06-01 2016-06-30 2016-07-01
8 tennis Y kray43 2016-01-01 2016-01-31 2016-02-01
9 tennis Y jeri44 2016-01-01 2016-01-31 2016-02-01
10 tennis N jeri44 2016-01-01 2016-01-31 2016-02-01
So I'd like to run a query that produces the following result:
sport season_start success_percent
----- ------------ ---------------
rugby 2016-01-01 100.00
rugby 2016-03-01 50.00
soccer 2016-01-01 100.00
soccer 2016-06-01 0.00
tennis 2016-01-01 66.66
I tried :
select sport,won, season_start, count(sport) as sportcount
from table1 where won = 'Y'
group by sport, won, season_start
But this just give me the entire count but not the actual success percent. I tried using case statement but I'm not too good with tsql, or with using 'Sum' to tally the wins and the losses and then calculate the win percentage out of the total.
Can someone provide a tsql query that does this?
You can use conditional counts for this:
select sport, season_start,
sportcount = count(case when won = 'Y' then 'X' end) * 100.0 / count(*)
from table1
group by sport, season_start
Please try the following query:
SELECT sport, season_start,
(CAST(COUNT(CASE won WHEN 'Y' THEN '1' ELSE NULL END) AS DECIMAL(10,2))) / CAST(COUNT(*) AS DECIMAL(10,2)))*100 AS sportcount
FROM table1
GROUP BY sport, season_start
In fact inside the COUNT aggregate function we check the value of won column and if it is equal to 'Y' we will count it otherwise we will ignore it. As you may know Count aggregate function ignore the NULL values, so inside the Count if won is not equal to 'Y' we produce a NULL value that will make the Count to ignore it. After we Count the won counts, we simply need to divide it to total matches count per sport and season(Count(*) will count this).
You have placed won in group by, that will cause problem. Each records with same Groups will be presented in result set just once. If you need to have won in your select list you may try the CROSS APPLY or Window Aggregate Functions. If you need so, please update your question and after that I will update my answer based on it.
Please take note that you may need to CAST or CONVERT the percentage calculation part to decimal or numeric or etc data type if you will need to have the precise result.(This is why I have cast the both Counts to DECIMAL)
I have two table employee and Native leave I want count employee’s Department wise and count employee in native
Employee Table:-
Empno Name Depart Addre Age Exisisting_No
-----------------------------------------
1 Abc Acc Kkk 20 1
2 Efg Hr Hhh 22 2
3 ijk Acc Yyy 21 3
4 Mno Hr Zzz 20 4
Native Leave Table:-
Exisisting_No Name From To Reason
---------------------------------
2 Efg 30/01/14 04/02/14 personal
Want Output Like:-
Depart EmpNo NL
---------------
Hr 2 1
Acc 2 0
So please any one can help me and sorry for my English
SELECT e.Dept
, count(Empno)
, count(NL.Exisisting_No)
FROM Employee e LEFT JOIN NativeLeave NL ON e.Exisisting_No = NL.Exisisting_No
GROUP BY e.Dept