Skip to content

Commit 86eba25

Browse files
committed
adding sql problem
1 parent 2d91654 commit 86eba25

7 files changed

+124
-1
lines changed

36.20 How to find strings with lower case characters Case Insensitive Collate.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
select upper(FirstName) as uper_first,FirstName from dbo.Emp
55
where upper(FirstName) COLLATE Latin1_Genral_CS_AS != FirstName;
66

7-
select SERVERPROPERTY('Collation')# CI MEANS CASE SENSITIVE
7+
select SERVERPROPERTY('Collation')# CI MEANS CASE SENSITIVE
8+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#How to find n consecutive date records | Sales for at least n consecutive days.
2+
#DATEADD(DAYS,SPECIFY WHAT VALUE OF DATE(HOW many days want to subtract), from which you want to subtract)
3+
select order_date ,
4+
DATEADD(D, - row_number() over(order by order_date), order_date) as col1
5+
from superstore_orders;
6+
7+
#subtract row no from oderd date col,so we get same value or date for consecutive records,perfrom count on col1 it has same value,use cte
8+
with cte as (
9+
select order_date ,
10+
DATEADD(D, - row_number() over(order by order_date), order_date) as col1
11+
from superstore_orders)
12+
select count(*) as cnt,MIN(order_date) as startdate as od,max(order_date) as enddate FROM cte group by col1
13+
14+
#atleast 3 consecutive date
15+
with cte as (
16+
select order_date ,
17+
DATEADD(D, - row_number() over(order by order_date), order_date) as col1
18+
from superstore_orders)
19+
select startdate,enddate from
20+
(select count(*) as cnt,MIN(order_date) as startdate as od,max(order_date) as enddate FROM cte group by col1) as tbla
21+
where cnt>=3
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#No sales for n consecutive days | Identify date gaps
2+
select order_date from superstore_orders
3+
order by order_date;
4+
5+
#we have to get the next record ,so we use lead(next consecutive date), #01-04-20 01-06-18 gap
6+
select order_date,
7+
lead(order_date) over(order by order_date) as Lead_date
8+
from superstore_orders
9+
order by order_date;
10+
11+
#now we need to find difference value
12+
#ssms only
13+
select order_date,
14+
datediff(day, order_date, (lead(order_date) over(order by order_date) ) ) as Gap
15+
from superstore_orders
16+
order by order_date;
17+
18+
#need to find whenn there is no sales,put above in subquerry
19+
select * from (
20+
select order_date,
21+
datediff(day, order_date, (lead(order_date) over(order by order_date) ) ) as Gap
22+
from superstore_orders) as NoSales
23+
where NoSales.gap > 1 # greatedr than 1
24+
25+
26+
27+
28+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#How to find First and Last day of week
2+
3+
select datediff(wk,'2021-08-05',GetDate());# get 1 week differnce from this
4+
5+
select dateadd(wk,datediff(wk,'2021-08-01',GetDate()),'2021-08-01');
6+
dateadd(wk,datediff(wk,'2021-08-01',GetDate()) + 1 ,'2021-08-01'); # to rrive at the last week
7+
8+
9+
select datediff('2021-08-05',date)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
select emp_name,department_id,salary from
2+
(select emp_name,department_id,salary
3+
,rank() over(partition by department_id order by salary desc) as maxsal
4+
from empm) as empsal
5+
where empsal.maxsal <= 2 #top 2 highest salary
6+
union all #union have impact on performance,union all
7+
select 'others' as emp_name,department_id,sum(salary) from
8+
(select department_id,salary
9+
,rank() over(partition by department_id order by salary desc) as maxsal
10+
from empm) as empsal
11+
where empsal.maxsal > 2 #>2 does not lie in top 2 salary of the dapartment
12+
group by department_id
13+
#union all combines duplicate record in two queries if exist,union combines only distict record
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#In this video, we write a SQL to compare monthly sales -
2+
#1) With previous month
3+
#2) same month previous year
4+
#3) first month of year
5+
6+
#Compare monthly sales with previous month, same month previous year, first month of year
7+
select order_date,sales
8+
from superstore_orders;
9+
10+
#sales of each month from different years ,LAG: bring data from previous row
11+
select year(order_date),Month(order_date),sum(sales) ,
12+
lag(sum(sales)) OVER(order by year(order_date),Month(order_date) )
13+
from superstore_orders
14+
where year(order_date) in (2012,2013)
15+
group by year(order_date),Month(order_date);
16+
17+
#to get month by month comparison,compare monthly data with revious month data
18+
select year(order_date),Month(order_date),sum(sales),
19+
lag(sum(sales)) OVER(order by year(order_date),Month(order_date) )
20+
from superstore_orders
21+
where year(order_date) in (2012,2013)
22+
group by year(order_date),Month(order_date);
23+
24+
#2compare same month from previous year,default offset is 1,lag(sum(sales),offset)
25+
select year(order_date),Month(order_date), sum(sales) -
26+
lag(sum(sales),12) OVER(order by year(order_date),Month(order_date) )
27+
from superstore_orders
28+
where year(order_date) in (2012,2013)
29+
group by year(order_date),Month(order_date);
30+
31+
#compare data of 1st month of same year,eg. august,subtract 7
32+
#october (10)-1=9
33+
#fetch month(order_date) and -1 from currnt month
34+
#3.compare monthly sales from 1st moth of that year
35+
select year(order_date),Month(order_date), sum(sales),
36+
lag(sum(sales),Month(order_date) - 1) OVER(order by year(order_date),Month(order_date) )
37+
from superstore_orders
38+
where year(order_date) in (2012,2013)
39+
group by year(order_date),Month(order_date);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#How to find number of emails from the same domain
2+
3+
select * from entries;
4+
#extract domian from charindex ,get position of occurence of '@'
5+
select CharIndex('@',email),email from entries;
6+
7+
#extract the string after @,total length of string - first 6 char
8+
select right(email,len(email)- CharIndex('@',email)) from entries;
9+
10+
#How to find number of emails from the same domain
11+
select count(*) , right(email,len(email)- CharIndex('@',email)) from entries
12+
group by right(email,len(email)- CharIndex('@',email));

0 commit comments

Comments
 (0)