I would like to query the max date only of all customers that has a value of 25.
Table1
cust_id Date Value
1 2019-10-01 25
1 2019-10-19 35
1 2018-08-27 29
1 2019-07-09 35
1 2019-10-15 55
2 2019-09-26 45
2 2019-10-19 31
2 2019-07-19 8
2 2019-10-02 28
3 2019-09-02 36
3 2019-08-15 39
4 2019-10-15 37
4 2019-10-16 36
4 2018-11-18 27
wrote this query:
select
t1.value,
max(t1.date) as max_date
from table1 t1
where
(t1.date >= '2019-09-30 and t1.date <= 2019-10-31)
and t1.value > 25
group by t1.value
but I am getting:
cust_id Date Value
1 2019-10-01 25
1 2019-10-19 35
1 2019-10-15 55
2 2019-10-19 31
2 2019-10-02 28
4 2019-10-15 37
4 2019-10-16 36
would like to only see the last data entry that is over 25. Something like this:
cust_id Date Value
1 2019-10-19 35
2 2019-10-19 31
4 2018-11-16 36
Related
I'm hoping some examples will help explain the situation.
SELECT
ID,
--ROW_NUMBER() OVER (PARTITION BY CardNumber ORDER BY ID DESC) AS 'RN',
DENSE_RANK() OVER (ORDER BY CardNumber DESC) AS Rank,
CardNumber,
StampNumber,
AuditDate,
FROM [dbo].[XXXX]
ORDER BY ID DESC, AuditDate DESC, StampNumber DESC
I've read up on DENSE_RANK() and it's the closest to what I'm looking for but not quite there.
Running this block of code gives me
ID
Rank
CardNumber
StampNumber
AuditDate
46
1
3
20
2022-03-07 03:45:50.343
45
1
3
20
2022-03-07 03:45:50.343
44
2
2
40
2022-03-07 03:45:50.343
43
2
2
30
2022-03-07 03:45:50.343
42
2
2
20
2022-03-07 03:45:50.343
41
2
2
10
2022-03-07 03:45:50.343
40
3
1
40
2022-03-07 03:45:50.343
39
3
1
30
2022-03-07 03:45:50.343
38
3
1
20
2022-03-07 03:45:50.343
37
3
1
10
2022-03-07 03:45:50.343
36
1
3
40
2022-03-07 03:45:50.343
35
1
3
30
2022-03-07 03:45:50.343
34
1
3
20
2022-03-07 03:45:50.343
33
1
3
10
2022-03-07 03:45:50.343
The result I'm looking for is
ID
Rank
CardNumber
StampNumber
AuditDate
46
1
3
20
2022-03-07 03:45:50.343
45
1
3
20
2022-03-07 03:45:50.343
44
2
2
40
2022-03-07 03:45:50.343
43
2
2
30
2022-03-07 03:45:50.343
42
2
2
20
2022-03-07 03:45:50.343
41
2
2
10
2022-03-07 03:45:50.343
40
3
1
40
2022-03-07 03:45:50.343
39
3
1
30
2022-03-07 03:45:50.343
38
3
1
20
2022-03-07 03:45:50.343
37
3
1
10
2022-03-07 03:45:50.343
36
4
3
40
2022-03-07 03:45:50.343
35
4
3
30
2022-03-07 03:45:50.343
34
4
3
20
2022-03-07 03:45:50.343
33
4
3
10
2022-03-07 03:45:50.343
I'd like the dense rank to still group the rank by the CardNumber but need the rank column to grow sequentially instead of resetting.
I'm looking to only grab the top 3 ranks.
This is a type of gaps-and-islands problem. You are trying to get a ranking number for each group of identical CardNumber values (with no gaps), when ordered by ID DESC.
You cannot use DENSE_RANK or ROW_NUMBER for this, because they will place all rows with the same CardNumber value together.
There are a number of solutions. Here is one:
WITH PrevValues AS (
SELECT *,
IsNewCardNumber = CASE WHEN CardNumber = LAG(CardNumber) OVER (ORDER BY ID DESC)
THEN NULL ELSE 1 END
FROM XXXX
)
SELECT
ID,
Rank = COUNT(IsNewCardNumber) OVER (ORDER BY ID DESC),
CardNumber,
StampNumber,
AuditDate
FROM PrevValues;
db<>fiddle
SELECT a.*,
SUM(s.amount) over(ORDER BY s.month rows unbounded preceding) AS a ,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows unbounded preceding) AS b,
SUM(s.amount) over(PARTITION BY s.month ) AS c_1,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN unbounded preceding AND unbounded following) AS c,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN 1 preceding AND unbounded following) AS d,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN 1 preceding AND 1 following) AS e,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN unbounded preceding AND 1 following) AS f,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows CURRENT ROW) AS g
FROM all_sales s,
(SELECT *
FROM all_sales) a
WHERE s.rowid = a.rowid;
/
--above query give the result shown below what is difference between c_1 and c column.
YEAR MONTH PRD_TYPE_ID EMP_ID AMOUNT A B C_1 C D E F G
1 2006 1 1 21 1.00 1 1 10 10 10 3 3 1
2 2006 1 1 21 2.00 3 3 10 10 10 6 6 2
3 2005 1 2 21 3.00 6 6 10 10 9 9 10 3
4 2005 1 2 22 4.00 10 10 10 10 7 7 10 4
5 2006 2 1 21 5.00 15 5 11 11 11 11 11 5
6 2005 2 1 21 6.00 21 11 11 11 11 11 11 6
7 2005 3 1 21 21 7 7 7 7 7
8 2006 3 2 21 7.00 28 7 7 7 7 7 7 7
9 2005 4 1 21 8.00 36 8 17 17 17 17 17 8
10 2006 4 2 21 9.00 45 17 17 17 17 17 17 9
11 2006 5 2 21 45 10 10 10 10 10
12 2005 5 1 21 10.00 55 10 10 10 10 10 10 10
13 2006 6 1 21 11.00 66 11 23 23 23 23 23 11
14 2005 6 1 21 12.00 78 23 23 23 23 23 23 12
15 2005 7 2 21 13.00 91 13 27 27 27 27 27 13
16 2006 7 1 21 14.00 105 27 27 27 27 27 27 14
17 2005 8 2 21 15.00 120 15 31 31 31 31 31 15
18 2006 8 1 21 16.00 136 31 31 31 31 31 31 16
19 2005 9 2 21 17.00 153 17 35 35 35 35 35 17
20 2006 9 1 21 18.00 171 35 35 35 35 35 35 18
21 2005 10 2 21 19.00 190 19 39 39 39 39 39 19
22 2006 10 1 21 20.00 210 39 39 39 39 39 39 20
23 2006 11 1 21 21.00 231 21 43 43 43 43 43 21
24 2005 11 1 21 22.00 253 43 43 43 43 43 43 22
25 2006 12 2 21 23.00 276 23 47 47 47 47 47 23
26 2005 12 1 21 24.00 300 47 47 47 47 47 47 24
You have the same result because your statements are preaty the same:
SUM(s.amount) over(PARTITION BY s.month ) AS c_1,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN unbounded preceding AND unbounded following) AS c
The cause of it is condition BETWEEN unbounded preceding AND unbounded following because it covers the entire range of partition.
FROM MSDN:
UNBOUNDED PRECEDING - Specifies that the window starts at the first row of the partition. UNBOUNDED PRECEDING can only be specified as window starting point.
UNBOUNDED FOLLOWING - Specifies that the window ends at the last row of the partition. UNBOUNDED FOLLOWING can only be specified as a window end point. For example RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING defines a window that starts with the current row and ends with the last row of the partition.
When together they are equal to PARTITION BY s.month
I have a table name
Spend:
id value type date total
1 21 ar 3/3/2012 45
2 24 az 3/3/2012 55
3 25 az 3/3/2012 55
4 25 az 3/3/2012 45
5 25 ar 3/3/2012 45
Condition:
APR=(value=25 and type like 'ar') or(total=55)
How can i write case statement in select statement with out using #temp and cte's in single select statement.
The output of select statement must be
id value type date total APR
1 21 ar 3/3/2012 45 0
2 24 az 3/3/2012 55 1
3 25 az 3/3/2012 55 1
4 25 az 3/3/2012 45 0
5 25 ar 3/3/2012 45 1
Try this
SELECT id,value,type,date,total,APR,
CASE WHEN (value=25 and type like 'ar') OR (total=55) Then 1 ELSE 0 END AS [Apr]
FROM Table1
CASE
SELECT IIF((Value = 25 and type like 'ar') or (total=55)', 1, 0) AS APR, *
FROM Spend
I have tables
table1
epid etid id EValue reqdate
----------- ----------- ----------- ------------ ----------
15 1 1 498925307069 2012-01-01
185 1 2 A5973FC43CE3 2012-04-04
186 1 2 44C6A4B776A2 2012-04-05
205 1 2 7A0ED3F1DA13 2012-09-19
206 1 2 77771D65F9C4 2012-09-19
207 1 2 AD74A4AA41BD 2012-09-19
208 1 2 9595ABE5A0C8 2012-09-19
209 1 2 7611D2FB395B 2012-09-19
210 1 2 04A510D6067A 2012-09-19
211 1 2 24D43EC268F8 2012-09-19
table2
PEId Id EPId
----------- ----------- -----------
43 9 15
44 10 15
45 11 15
46 12 15
47 13 15
48 14 15
49 15 15
50 16 15
51 17 15
52 18 15
table3
PLId PEId Id ToPayId
----------- ----------- ----------- -----------
71 43 9 1
72 43 9 2
73 44 10 1
74 44 10 2
75 45 11 1
76 45 11 2
77 46 12 1
78 46 12 2
79 47 13 1
80 47 13 2
I want to get one id whose count is less than 8 in table 3 and order by peid in table 2,
I have written query
SELECT Top 1 ToPayId FROM
(
SELECT Count(pl.ToPayId) C, pl.ToPayId
FROM table3 pl
INNER JOIN table2 pe ON pl.peid = pe.peid
INNER JOIN table1 e ON pe.epid = e.epid
WHERE e.EtId=1 GROUP BY pl.ToPayId
) As T
INNER JOIN table2 p ON T.ToPayId= p.Id
WHERE C < 8 ORDER BY p.PEId ASC
This query executes more than 1000 times in stored procedure depends on the entries in user-defined-table-type using while condition.
But it is very slow as we have millions of entries in each table.
Can anyone suggest better query regarding above?
maybe try with the having clause to get rid of the from select
select table2.id as due
from table3 inner join table2 on table2.PEId=table3.PEId...
group by ...
having count(due) <8
order by ...
-> you have a redundant Id column in table3 : seems pretty useless as the couple PEId and Id appears unique so remove it and reduce the size of table 3 by 25% hence improving performance of db
Will.. since you did not provide enough sample data and I am not sure what exactly your business logic is. So that I can just modify the code in blind.
SELECT ToPayId
FROM (
SELECT TOP 1 Count(pl.ToPayId) C, pl.ToPayId, pe.PEId
FROM table3 as pl
INNER JOIN table2 as pe ON pl.peid = pe.peid AND pl.ToPayId = pe.Id
INNER JOIN table1 e ON pe.epid = e.epid
WHERE e.EtId=1
GROUP BY pl.ToPayId, pe.PEId
HAVING Count(pl.ToPayId) < 8
ORDER BY pe.PEId ASC
) AS T
I have a table that lists visits to a clinic. I'd like to get a "histogram" of sorts showing how frequently patients visit the clinic along with totals. Here's some sample code (tested under MS SQL Server 2005) to show what I'm talking about:
CREATE TABLE #test (
visit_id int IDENTITY(1,1),
patient_id int
);
DECLARE #num_patients int;
SELECT #num_patients = 1000 + ABS(CHECKSUM(NEWID())) % 250;
INSERT INTO #test (patient_id)
SELECT TOP 15 PERCENT ABS(CHECKSUM(NEWID())) % #num_patients
FROM sysobjects a, sysobjects b;
-- SELECT COUNT(*) AS total_visits FROM #test;
-- SELECT COUNT(DISTINCT patient_id) AS distinct_patients FROM #test;
SELECT CASE GROUPING(num_pat_visits) WHEN 1 THEN 'Total'
ELSE CAST(num_pat_visits AS varchar(5)) END AS num_pat_visits,
COUNT(*) AS num_patients, num_pat_visits * COUNT(*) AS tot_pat_visit
FROM
(SELECT patient_id, COUNT(*) AS num_pat_visits FROM #test GROUP BY patient_id) a
GROUP BY num_pat_visits WITH ROLLUP
ORDER BY CAST(num_pat_visits AS int) DESC;
This gets me almost to where I want:
num_pat_visits num_patients tot_pat_visit
-------------- ------------ -------------
60 1 60
54 2 108
52 2 104
51 4 204
50 3 150
49 3 147
48 7 336
47 7 329
46 15 690
45 15 675
44 29 1276
43 36 1548
42 45 1890
41 45 1845
40 59 2360
39 71 2769
38 51 1938
37 72 2664
36 77 2772
35 74 2590
34 72 2448
33 82 2706
32 90 2880
31 74 2294
30 69 2070
29 47 1363
28 30 840
27 27 729
26 26 676
25 21 525
24 13 312
23 4 92
22 5 110
21 4 84
20 2 40
18 2 36
Total 1186 NULL
However, I can't seem to get SQL Server to display the total number of visits where it says NULL on the total row.
Any ideas?
I think you can just do:
sum(num_pat_visits) as tot_pat_visit
SELECT CASE GROUPING(num_pat_visits) WHEN 1 THEN 'Total'
ELSE CAST(num_pat_visits AS varchar(5)) END AS num_pat_visits,
COUNT(*) AS num_patients,
--num_pat_visits * COUNT(*) AS tot_pat_visit
sum(num_pat_visits) as tot_pat_visit
FROM
(SELECT patient_id, COUNT(*) AS num_pat_visits FROM #test GROUP BY patient_id) a
GROUP BY num_pat_visits WITH ROLLUP
ORDER BY CAST(num_pat_visits AS int) DESC;