I have a table t1
**id** **Date** **Sales**
102 20180101 50
102 20180102 60
102 20180103 70
102 20180104 90
102 20180105 10
102 20180105 100
102 20180106 100
102 20180107 30
102 20180108 20
102 20180109 34
102 20180110 40
102 20180111 50
102 20180112 60
Now I want the previous10 records for each row like for 20180111 sum of sale should be 464 which is the sum of the previous 10 records sale and for 20180112 it should be 474.
do you think the following is what you need ?
WITH temp
AS (SELECT id,
date,
SUM(sales) AS n_Sale
FROM dbo.t
GROUP BY id,
date)
SELECT *,
SUM(t.n_Sale) OVER (ORDER BY date ASC ROWS 9 PRECEDING)
FROM temp AS t;
I think you could add a consecutive index and then group by truncate(index/N), and get the sum of values.
for instance, to get the sum every 3 elements (mysql):
SELECT truncate((row_number-1)/3,0) grp, sum(sales) total
FROM (
SELECT #row_number:=#row_number+1 as row_number,
sales
FROM (
SELECT 452 as sales UNION ALL
SELECT 324 as sales UNION ALL
SELECT 342 as sales UNION ALL
SELECT 342 as sales UNION ALL
SELECT 342 as sales UNION ALL
SELECT 232 as sales
) as t1, (SELECT #row_number:=0) AS t2) as tf group by grp;
http://sqlfiddle.com/#!9/3149e4/524
Related
Good morning. SQL new guy here seeking some help. I'm attempting to get the AVG of my resultset from a subquery. The subquery works just fine and gives me the resultset I need, but I just cannot get the AVG of the resultset to work. Any guidance would be greatly appreciated.
SELECT AVG(COUNT) FROM
(SELECT COUNT(DISTINCT(table2.item_no))
FROM table1
JOIN table2 ON table1.order_numb = table2.order_numb
WHERE user_so = 'paul'
AND order_date BETWEEN '9/20/2017'AND '9/20/2018'
GROUP BY table1.order_numb);
Here is a sample of the resultset from the subquery on its own that I'm trying to then turn around and get an AVG of:
216
181
163
156
144
144
143
133
129
129
120
114
113
112
112
109
108
104
103
99
98
98
98
98
98
97
97
97
96
96
94
94
94
93
93
I think you want something like this:
SELECT AVG(a_number) FROM
(SELECT COUNT(DISTINCT(table2.item_no)) AS a_number
FROM table1
JOIN table2 ON table1.order_numb = table2.order_numb
WHERE user_so = 'paul'
AND order_date BETWEEN '9/20/2017'AND '9/20/2018'
GROUP BY table1.order_numb) AS the_subquery
I don't have the same tables with data, so here is a cut down working example:
SELECT AVG(a_number) FROM (
SELECT 100 AS a_number
UNION
SELECT 200 AS a_number
UNION
SELECT 300 AS a_number
UNION
SELECT 400 AS a_number
) AS the_subquery
It looks like you were missing that you need to alias the subquery and you want to AVG the alias of the value being returned in the subquery.
update
As per the comment, if you'd like the answer to be rounded to 2 decimal points you will need to CAST it to a different data type, like this:
SELECT ROUND(AVG(CAST(a_number AS FLOAT)), 2) FROM
(SELECT COUNT(DISTINCT(table2.item_no)) AS a_number
FROM table1
JOIN table2 ON table1.order_numb = table2.order_numb
WHERE user_so = 'paul'
AND order_date BETWEEN '9/20/2017'AND '9/20/2018'
GROUP BY table1.order_numb) AS the_subquery
or for others without access to the table:
SELECT ROUND(AVG(CAST(a_number AS FLOAT)), 2) FROM (
SELECT 100 AS a_number
UNION
SELECT 200 AS a_number
UNION
SELECT 300 AS a_number
UNION
SELECT 403 AS a_number
) AS the_subquery
You can read more about how ROUND, AVG, and CAST work here: How do I retrieve decimals when rounding an average in SQL
I trying to create a table that will support a simple event study analysis, but I'm not sure how best to approach this.
I'd like to create a table with the following columns: Customer, Date, Time on website, Outcome. I'm testing the premise that the outcome for a particular customer on any give day if a function of the time spent on the website on the current day as well as the preceding five site visits. I'm envisioning a table similar to this:
I'm hoping to write a T-SQL query that will produce an output like this:
Given this objective, here are my questions:
Assuming this is indeed possible, how should I structure my table to accomplish this objective? Is there a need for a column that refers to the prior visit? Do I need to add an index to a particular column?
Would this be considered a recursive query?
Given the appropriate table structure, what would the query look like?
Is it possible to structure the query with a variable that determines the number of prior periods to include in addition to the current period (for example, if I want to compare 5 periods to 3 periods)?
Not sure I understand analytic value of your matrix
Declare #Table table (id int,VisitDate date,VisitTime int,Outcome varchar(25))
Insert Into #Table (id,VisitDate,VisitTime,Outcome) values
(123,'2015-12-01',100,'P'),
(123,'2016-01-01',101,'P'),
(123,'2016-02-01',102,'N'),
(123,'2016-03-01',100,'P'),
(123,'2016-04-01', 99,'N'),
(123,'2016-04-09', 98,'P'),
(123,'2016-05-09', 99,'P'),
(123,'2016-05-14',100,'N'),
(123,'2016-06-13', 99,'P'),
(123,'2016-06-15', 98,'P')
Select *
,T0 = VisitTime
,T1 = Lead(VisitTime,1,0) over(Partition By ID Order By ID,VisitDate Desc)
,T2 = Lead(VisitTime,2,0) over(Partition By ID Order By ID,VisitDate Desc)
,T3 = Lead(VisitTime,3,0) over(Partition By ID Order By ID,VisitDate Desc)
,T4 = Lead(VisitTime,4,0) over(Partition By ID Order By ID,VisitDate Desc)
,T5 = Lead(VisitTime,5,0) over(Partition By ID Order By ID,VisitDate Desc)
From #Table
Order By ID,VisitDate Desc
Returns
id VisitDate VisitTime Outcome T0 T1 T2 T3 T4 T5
123 2016-06-15 98 P 98 99 100 99 98 99
123 2016-06-13 99 P 99 100 99 98 99 100
123 2016-05-14 100 N 100 99 98 99 100 102
123 2016-05-09 99 P 99 98 99 100 102 101
123 2016-04-09 98 P 98 99 100 102 101 100
123 2016-04-01 99 N 99 100 102 101 100 0
123 2016-03-01 100 P 100 102 101 100 0 0
123 2016-02-01 102 N 102 101 100 0 0 0
123 2016-01-01 101 P 101 100 0 0 0 0
123 2015-12-01 100 P 100 0 0 0 0 0
With fixed columns you can do it like this with lag:
select
time,
lag(time, 1) over (partition by customer order by date desc),
lag(time, 2) over (partition by customer order by date desc),
lag(time, 3) over (partition by customer order by date desc),
lag(time, 4) over (partition by customer order by date desc)
from
yourtable
If you need dynamic columns, then you'll have to build it using dynamic SQL.
I'm trying to group a set of data and for some of the fields I need to select a specific value based on the ttype, for example I have the following rows:
caseid age iss gcs ttype
00170 64 25 17 Transfer Out
00170 64 27 15 Transfer In
00201 24 14 40 Transfer In
If a caseID has ttype 'Transfer Out' I want to use the ISS and GCS values from this row, otherwise use the values from the 'Transfer In' row.
My desired output based on the above example would be:
caseid age iss gcs
00170 64 25 17
00201 24 14 40
My current select statement is:
select caseid, max(age), max(iss), max(gcs)
from Table1
group by caseid
Which I know is incorrect but how do I specify the values for ISS and GCS from a specific row?
Thanks
Edit - I will not always need to select from Row1, table below with expanded data:
caseid age iss gcs los ttype disdate
170 64 25 17 5 Transfer Out 2014-01-02 00:00:00.000
170 64 27 15 1 Transfer In 2014-01-04 00:00:00.000
201 24 14 40 4 Transfer In 2014-01-04 00:00:00.000
In this case, I want the max age and the ISS and GCS figure for row1 as before but I need to sum the LOS and select the disdate for row 2 (ie the latest date), so my output would be:
caseid age iss gcs los disdate
170 64 25 17 6 2014-01-04
201 24 14 40 4 2014-01-04
Is this possible?
You can use a CTE and ROW_NUMBER + Over-clause (edited acc. to your updated question):
WITH CTE AS
(
SELECT caseid, age, iss, gcs, los, ttype, disdate,
SumLos = SUM(los) OVER (PARTITION BY caseid),
LatestDisDate = MAX(disdate) OVER (PARTITION BY caseid),
rn = ROW_NUMBER() OVER (PARTITION BY caseid
ORDER BY CASE WHEN ttype = 'Transfer Out'
THEN 0 ELSE 1 END ASC, disdate ASC)
FROM dbo.Table1
)
SELECT caseid, age, iss, gcs, los = SumLos, disdate = LatestDisDate
FROM CTE
WHERE rn = 1
Demo
I think this is what you need -
;WITH CTE AS
(
SELECT case_id, age,iss,gcs, ROW_NUMBER () over (PARTITION BY ttype order by gcs DESC) Rn
from YOUR_TABLE_NAME
)
SELECT case_id,age,iss,gcs
from CTE where Rn =1
I have a table named PlayerScore that contains the player name and their average scores:
Id Name Average
1 Sakib 80
2 Tamim 70
3 Mushfiq 60
4 Sabbir 50
5 Ashraful 20
6 Aftab 40
7 Rubel 30
8 Kalu 10
I want to find their partnership combination based on a condition that,
palyer whose average score is greater than 40 can not be partner with players whose score is less than 40.
I tried the following query :
select a.Name,a.Average,b.Name,b.Average from ((select * from PlayerScore where Average<=40) as a inner join (select * from PlayerScore where Average<=40) as b on a.Id < b.Id)
union
select a.Name,a.Average,b.Name,b.Average from ((select * from PlayerScore where Average>=40) as a inner join (select * from PlayerScore where Average>=40) as b on a.Id < b.Id)
that results in :
Name Average Name Average
Aftab 40 Kalu 10
Aftab 40 Rubel 30
Ashraful 20 Aftab 40
Ashraful 20 Kalu 10
Ashraful 20 Rubel 30
Mushfiq 60 Aftab 40
Mushfiq 60 Sabbir 50
Rubel 30 Kalu 10
Sabbir 50 Aftab 40
Sakib 80 Aftab 40
Sakib 80 Mushfiq 60
Sakib 80 Sabbir 50
Sakib 80 Tamim 70
Tamim 70 Aftab 40
Tamim 70 Mushfiq 60
Tamim 70 Sabbir 50
Is their any solution without using UNION
select distinct a.Name,a.Average,b.Name,b.Average
from PlayerScore a
join PlayerScore b
on a.Id < b.Id
and ( a.Average<=40 and b.Average<=40
or a.Average>=40 and b.Average>=40
)
it will likely result in the same exceution plan.
Maybe you can do something like this:
SELECT
t.*,
t2.*
FROM
PlayerScore AS t
CROSS JOIN PlayerScore AS t2
WHERE t.Average>=40 AND t2.Average<40
ORDER BY t.Name
You can create 2 groups based on your condition and give them different values and then do a join based on the value. Something like this.
;WITH PlayerScore as
(
SELECT 1 AS Id,'Sakib' AS Name,80 AS Average
UNION ALL SELECT 2,'Tamim',70
UNION ALL SELECT 3,'Mushfiq',60
UNION ALL SELECT 4,'Sabbir',50
UNION ALL SELECT 5,'Ashraful',20
UNION ALL SELECT 6,'Aftab',40
UNION ALL SELECT 7,'Rubel',30
UNION ALL SELECT 8,'Kalu',10
),PlayerCriteria AS
(
SELECT *,CASE WHEN Average >= 40 THEN 1 ELSE 0 END joincondition
FROM PlayerScore
)
SELECT * FROM PlayerCriteria C1
INNER JOIN PlayerCriteria C2 ON C1.joincondition = C2.joincondition
AND C1.Id > C2.Id
Say I have a table like this..
Date Id Duration
13-1-2014 102 0:00:03
13-1-2014 102 0:08:44
14-1-2014 102 0:01:21
14-1-2014 102 0:23:40
15-1-2014 102 0:00:29
15-1-2014 102 0:14:00
13-1-2014 103 0:00:02
15-1-2014 103 0:00:56
15-1-2014 103 1:00:28
16-1-2014 103 0:00:02
13-1-2014 104 0:01:18
13-1-2014 104 0:03:36
14-1-2014 104 0:01:02
14-1-2014 104 0:06:41
15-1-2014 104 0:00:56
15-1-2014 104 0:00:59
I want this to be like,
Date Id Duration No.of.Id
13-1-2014 102 0:08:47 2
14-1-2014 102 0:24:02 2
i.e for one date, i want the total duration for a specific id and the number of duplicate rows of that id for that specific date as a separate column. Is it possible to achieve this in a select query? Any help/direction is greatly appreciated. Thanks in advance!
May be an easier way, but
SELECT [Date], [Id],
CONVERT(VARCHAR(8), DATEADD(ms, SUM(DATEDIFF(ms, 0, CAST([Duration] as time))), 0), 108) as duration,
COUNT(*) as nb
FROM Table1
GROUP BY [Date], [Id]
see SqlFiddle
Does this works?
SELECT date,
id,
SEC_TO_TIME(SUM(TIME_TO_SEC(STR_TO_DATE(Duration, '%h:%i:%s')))) as Duration,
count(*)
FROM table_like_this
WHERE id = %your_id%
GROUP BY date;