I'm trying to select the first occurrence of record on two conditions but have been in vain. Here's my codes:
PROC SQL;
CREATE TABLE table1 AS
SELECT user_id, type, date, money
FROM table2
WHERE date IN (SELECT MIN(date)
FROM twice_transaction
GROUP BY user_id,type);
For example the original table looks like this(table2)
user type date money
user1 type1 1/10/2012 money1
user1 type1 2/20/2012 money2
user1 type2 1/15/2012 money3
user1 type2 2/30/2012 money4
user2 type1 3/28/2012 money5
user2 type2 2/14/2012 money6
user2 type2 4/13/2012 money7
but I want only: (table1)
user1 type1 1/10/2012 money1
user1 type2 1/15/2012 money3
user2 type1 3/28/2012 money5
user2 type2 2/14/2012 money6
How should I modify/code for my end result? thanks!
There are a couple of ways to do this with SQL. The comment by #NoDisplayName shows you a more traditional SAS way of accomplishing this.
CREATE TABLE table1 AS
SELECT a.user_id, a.type, a.date, a.money
FROM table2 as a
INNER JOIN
SELECT (user_id, type, min(date) as date from table2 group by user_id, type) as b
on a.user_id = b.user_id
and a.type = b.type
and a.date = b.date;
What I am doing here is creating an inner select to get the min date by user_id and type. Then I use an inner join to select only the records from the from the first table that line up with the second.
Use the HAVING clause is also an option.
data have;
informat usert type $8. date mmddyy10. money $8.;
format date date9.;
input usert type date money;
cards;
user1 type1 1/10/2012 money1
user1 type1 2/20/2012 money2
user1 type2 1/15/2012 money3
user1 type2 2/28/2012 money4
user2 type1 3/28/2012 money5
user2 type2 2/14/2012 money6
user2 type2 4/13/2012 money7
;
run;
proc sql;
create table want as
select usert, type, date, money
from have
group by usert, type
having date=min(date);
quit;
Related
I am new to postgreSQL
Updated Initial Question:
I have two tables orders & users
user_id
username
a
user1
b
user2
c
user3
order_id
ordered_at
user_id
seller_id
1
2022-08-10
a
s1
2
2022-08-09
b
s1
3
2022-07-06
a
s2
4
2022-08-01
a
s1
5
2022-05-02
c
s1
6
2022-08-11
b
s2
7
2022-08-12
b
s1
My postgres SQL query should give me the result for seller s1:
order_id
last_purchase
user_id
username
1
2022-08-10
a
user1
4
2022-08-10
a
user1
2
2022-08-12
b
user2
7
2022-08-12
b
user2
5
2022-05-02
c
user3
For this I have wrote the SQL query:
SELECT
MAX(orders.ordered_at) AS last_purchase,
orders.order_id AS order_id,
users.user_id AS users_id,
users.username as username
FROM
orders
INNER JOIN
users
ON
orders.user_id = users.user_id
WHERE
orders.seller_id='s1'
GROUP BY users.user_id;
I am not getting the desired output
Any suggestions or help is welcomed. Thank you!
if I am understanding your question you want last purchase to be the max date per user id. if so use a windowing function
SELECT
MAX(orders.ordered_at) over
(partition by users.users_id order by orders.ordered_at desc) as
last_purchase,
orders.order_id AS order_id,
users.users_id AS users_id,
users.username as username
FROM
orders
INNER JOIN
users
ON
orders.user_id = users.id
I have the table user1 :
id |date
1 2018-01-01
2 null
3 2018-01-01
4 null
and another table user2:
id |date_buy
1 2018-01-01
1 2018-01-02
2 2018-01-01
2 2018-01-02
3 2018-01-01
4 2018-01-01
I would like to make a select query that select the id and the date from the table user1 but if the date field is null then it shall take the minimal date_buy for this user and fill the missing with it.
So my first idea was:
- make a simple query on the first table
SELECT id, date from user1
make a simple query on the second table
SELECT id, min(date_buy) as date from user2 group by id
union the two query and make a distinct where date is not null
Which give something like :
SELECT distinct id, date
from (SELECT id, date
from user1
UNION
select id, min(date_buy) as date
from user2 group by id)
where date is not null
But I struggle to shape this and make it work.
In Hive, I think I would do:
select u1.id, coalesce(u1.date, u2.min_date)
from user1 u1 left join
(select id, min(date_buy) as min_date
from user2
group by id
) u2
on u1.id = u2.id;
this should work
select u1.id,COALESCE(u1.date, u2.min_dt) from user1 as u1
join
( select id,MIN(date_buy) as min_dt from user2 group by id
) as u2
on u1.id=u2.id;
I have a table such as this:
PalmId | UserId | CreatedDate
1 | 1 | 2018-03-08 14:18:27.077
1 | 2 | 2018-03-08 14:18:27.077
1 | 3 | 2018-03-08 14:18:27.077
1 | 1 | 2018-03-08 14:18:27.077
I wish to know how many dates were created for Palm 1 and I also wish to know how many users have created those dates for Palm 1. So the outcome for first is 4 and outcome for second is 3
I am wondering if I can do that in a single query as oppose to having to do a subquery and a join on itself as in example below.
SELECT MT.[PalmId], COUNT(*) AS TotalDates, T1.[TotalUsers]
FROM [MyTable] MT
LEFT OUTER JOIN (
SELECT MT2.[PalmId], COUNT(*) AS TotalUsers
FROM [MyTable] MT2
GROUP BY MT2.[UserId]
) T1 ON T1.[PalmId] = MT.[PalmId]
GROUP BY MT.[PalmId], T1.[TotalUsers]
According to first table you could do something like this:
select count(distinct uerid) as N_Users,
count(created_date) as created_date, -- if you use count(*) you consider also rows with 'NULL'
palmid
from your_table
group by palmid
If you want "4" and "3", then I think you want:
SELECT MT.PalmId, COUNT(*) AS NumRows, COUNT(DISTINCT mt.UserId) as NumUsers
FROM MyTable MT
GROUP BY MT.PalmId
I have 2 tables with only 3 common fields.
I wrote the below query to pick up the missing data with Latest date in table 1.
Employee Category Date_Field First_Name Last_Name Status Result
100 Type1 30/08/2010 A B Present Good
200 Type1 1/09/2010 C D Hello Alt
100 Type3 30/09/2010 A B
100 Type1 30/09/2012 A B
Employee Category Date_Field
100 Type1 03/11/2016
100 Type3 30/09/2010
100 Type4 11/10/2010
200 Type3 12/12/1989
My Query is
with x1 as (SELECT Employee,Category,Date_Field,First_Name,Last_Name,Status,Result,' ' as Somefield from table 1
where not exists
(select 1 from table2
where table1.employee=table2.employee and table1.category = table2.Category
and table1.Date_Field = table2.Date_Field)),
x2 as (select Employee,Category,Max(Date_Field) as DateField from x1 group by Employee)
select x1.Employee,x1.Category,x1.Date_Field,x1.First_Name,x1.Last_Name,x1.Status,x1.Result,x1.Somefield from x1,x2
where x1.Employee = x2.Employee and x1.Date_Field=x2.DateField and x1.Category=x2.Category
order by x1.Employee;
The Current Ouptput i get is
Employee Category Date_Field First_Name Last_Name Status Result
100 Type1 30/09/2012 A B
200 Type1 1/09/2010 C D Hello Alt
Can i get this query modified so that If there ia record for the same employee with latest date and category in table 2, my output should not have that record.
So the expected Output is
Employee Category Date_Field First_Name Last_Name Status Result
200 Type1 1/09/2010 C D Hello Alt
Your help is much appreciated
Hope you can ignore the datefield for the NOT EXISTS clause.
SELECT * FROM TABLE1 T1
WHERE NOT EXISTS
(
SELECT 'x' FROM TABLE2 T2
WHERE T1.EMPLOYEE = T2.EMPLOYEE
AND T1.CATEGORY = T2.CATEGORY
AND T2.DATEFIELD >= T1.DATEFIELD
)
Say I have two tables as below where Table A has columns name and type where each name may appear many times and with different type and Table B has unique code, name and sum.
Table A
John Type1
Mark Type2
John Type1
Mark Type3
John Type4
Paul Type5
Table B
1 John 20
2 Mark 33
3 Paul 22
4 Mark 55
5 John 46
Now what I want is something like this:
Table C
1 John 20 Type1
2 Mark 33 Type2
3 Paul 22 Type5
4 Mark 55 Type2
5 John 46 Type1
Normally Table A should contain unique entries with one type for each name and I could do a right join Table B on name to get what I want. But now if I do right join I get duplicate entries on Table C because name has duplicates types in Table A. How do I solve this?
Try this
WITH TableAA
AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY NAME,TYPE) RN
,name
,type
FROM TableA
)
SELECT
B.*
,(
SELECT type from TableAA A WHERE A.name= B.name AND A.RN=
ISNULL(NULLIF((SELECT COUNT(1) FROM TableB C WHERE C.NAME=B.name
and C.no < B.no),0),1)
) AS Type
FROM
TableB B
SQLFiddle Demo
You can try this :
SELECT A.*,B.TYPE
FROM dbo.TABLE_2 A RIGHT JOIN
(
SELECT DISTINCT(NAME),MIN(TYPE)TYPE
FROM TABLE_1
GROUP BY NAME
) B ON A.NAME=B.NAME
ORDER BY CODE ASC
TABLE_1= TABLE A
TABLE_2= TABLE B