How find duplicates of the same id but different date with SQL (Oracle) - sql

I've got a datatable like that :
id
line
datedt
123
1
01/01/2021
123
2
01/01/2021
123
3
01/01/2021
777
1
13/04/2020
777
2
13/04/2020
123
1
12/04/2021
123
2
12/04/2021
888
1
01/07/2020
888
2
01/07/2020
452
1
05/01/2020
888
1
02/05/2021
888
2
02/05/2021
I'd like to obtain a result like that, ie : the nb of same id with differents dates.
Example we can find 123 with 2 diffents dates, 888 too, but only date for 777 and 452
id
nb
123
2
777
1
888
2
452
1
How could I get that ?
Hope it's clear :)
Thanks you

select id , count(distinct datedt) as nb
from table
group by id

Related

SQL Select statement and show only changed

I have the simple select script and it generates following audit table.
SELECT *
FROM Mytable
WHERE File = '123456A'
Output:
ID
File
StatusA
StatusB
User
UpdateDate
1
123456A
A
0
Tom
2021-01-01
12
123456A
B
0
Jack
2021-01-05
19
123456A
A
1
Alicia
2021-02-09
56
123456A
B
1
Jason
2021-03-09
87
123456A
A
1
Jason
2021-03-10
107
123456A
B
0
Ellie
2021-03-26
203
123456A
A
0
lucy
2021-04-08
239
123456A
B
1
Ellie
2021-04-16
I am trying to retrieve the rows when only column StatusB is changed. So it will generates the table like this.
SELECT *
FROM Mytable
WHERE File = '123456A'
-AND StatusB is changed
ID
File
StatusA
StatusB
User
UpdateDate
1
123456A
A
0
Tom
2021-01-01
19
123456A
A
1
Alicia
2021-02-09
107
123456A
B
0
Ellie
2021-03-26
239
123456A
B
1
Ellie
2021-04-16
In this case, I can see Alicia and Ellie changed the column StatusB. I am still thinking how to accomplish this goal.
Thanks,
-Ming
You can use lag():
select t.*
from (select t.*,
lag(statusB) over (order by updatedate) as prev_statusB
from Mytable t
where File = '123456A'
) t
where prev_statusB is null or prev_statusB <> statusB;

MS Access: Update the values in a table to match the ID fields of another table

I have two tables in my MS Access database:
1) tblLines
LineID
Line
LineName
2) tblTripTimes
TripTimesID
Line
Time
The tblTripTimes times table was imported into MS Access from a transit software program, and tblLines I created in MS Access. The Line column in each table share identical values; however, I'd prefer the tblTripTimes.Line values be replaced with the primary key values in tblLines.LineID.
For example:
Before
tblLines tblTripTimes
--------------- ------------------------
LineID | Line TripId | Line | Time
1 1 234 3 13:00
2 2 235 1 09:00
3 2A 236 2 17:17
4 2B 237 2B 07:30
5 3 238 2A 21:36
After
tblLines tblTripTimes
--------------- ------------------------
LineID | Line TripId | Line | Time
1 1 234 5 13:00
2 2 235 1 09:00
3 2A 236 2 17:17
4 2B 237 4 07:30
5 3 238 3 21:36
I've tried creating an update query that would match tblTripTimes.Line with tblLines.Line, then replace the values in tblTripTimes.Line with the values in the tblLines.LineID column. Being a rookie I'm at a complete loss.
Can someone please help?!
You can use a simple update query:
UPDATE tblLines INNER JOIN tblTripTimes ON tblLines.Line = tblTripTimes.Line
SET tblTripTimes.Line = tblLines.LineID

SQL Syntax to group this data in SQL Server 2012

I have a table (called StayDate) which looks like this:
ResaID Date RoomCategory RateAmount
1 2014-09-01 A 125
1 2014-09-02 A 125
1 2014-09-03 B 140
2 2014-09-04 A 125
2 2014-09-05 A 125
2 2014-09-06 A 125
2 2014-09-07 C 160
2 2014-09-08 C 160
The output from the SQL syntax I'm after need to look like this:
ResaID Count RoomCategory RateAmount
1 2 A 125
1 1 B 140
2 3 A 125
2 2 C 160
Can anyone help with the SQL syntax needed to summarize the data as above?
A way to do this without a GROUP BY:
SELECT DISTINCT ResaID, COUNT(*) OVER (PARTITION BY ResaID, RoomCategory, RateAmount) Count, RoomCategory, RateAmount
FROM StayDate

Need to join three tables. Not sure which join fits the req

table 1
AcctNO CustomerId Role Name
1 123 A ABC
2 121 B BCA
3 321 C CBA
table 2
AcctNo CustomerId Role Address
1 123 A 1/12
2 121 B 11/3
4 231 C 12-1
3 321 C 111
5 221 C 121
table 3
AcctNo CustomerId Role CompanyName
4 231 C hello
5 221 C bello
3 321 C cello
output should be as follows
AcctNo CustomerId Role Name Address COmpanyName
1 123 A ABC 1/12 NULL
2 121 B BCA 11/3 NULL
3 321 C CBA 111 cello
4 231 C NULL 12-1 hello
5 221 C NULL 121 bello
Use a left join between the tables on a unique column like the CustomerID.
A left join is used instead of a inner join so you get any missing values as a null instead of missing a record.

MSAccess: Ranking rows based upon column criteria

I have a dataset that looks like this:
Account Cost Centre TransNo
aaa 111 43443
aaa 111 32112
aaa 111 43211
aaa 112 32232
aaa 113 56544
bbb 222 43222
bbb 222 98332
ccc 111 88778
I need a column added that is a counter of the number of rows that relate to that Account/Cost Centre combination:
Account Cost Centre TransNo rCounter
aaa 111 43443 1
aaa 111 32112 2
aaa 111 43211 3
aaa 112 32232 1
aaa 112 56544 2
bbb 222 43222 1
bbb 222 98332 2
ccc 111 88778 1
Is this possible to do in MSAccess using SQL? and how would I go about it (ie what would be the SQL script I would need to write)?
Thanks in advance.
Something like:
SELECT a.Account, a.[Cost Centre], a.TransNo, (SELECT Count(*)
FROM table4 b
WHERE b.Account=a.Account
AND b.[Cost Centre]=a.[Cost Centre]
AND b.TransNo<=a.TransNo) AS AccountNo
FROM Table4 AS a
ORDER BY a.Account, a.[Cost Centre], a.TransNo;