How to fetch the first comment date of users in comments table - sql

have a comments table and need to get the first comment date (first inserted record) of each users in the table.
Output will be :
user_id first_comment_date

You can use min() try
SELECT user_id, MIN(comment_date)
FROM commentstable
GROUP BY user_id;

Related

Update table based on LAG value and condition

I'm trying to update the table with a lagging value for specific field when that field is different from '1900-01-01 00:00:00'
select Ticket_ID, Business_Area, Priority, HF_Client_Name, closed_date, Closed_Date_ID, Next_Create_date from schema.table_name order by closed_date desc;
And this is the result I'm trying to get:
I truly appreciate any help I can get. My DB is MYSQL db.
I'm working on a solution with a LAG function, an will paste my code as soon as I have something worth showing.
Thank you all!
Rosa
I've managed to solve this by creating a temp table which contains all the tickets with 0. I joined my original table without the 0 tickets to this second temp table.
The most important piece was to create a temp table with maximal next created date based on that month:
select a.business_area, a.priority, a.hf_client_name, DATE_FORMAT(a.closed_date ,'%Y-%m') month_of_ticket, max(next_created_date) next_created_date
from tmp_next_lead_date a
group by a.business_area, a.priority, a.hf_client_name, DATE_FORMAT(a.closed_date ,'%Y-%m');
After that I just joined the two tables based on month of ticket and the rest of the key.
select a.ticket_id, a.business_area, a.priority, a.hf_client_name, a.closed_date,
/*DATE_FORMAT(a.closed_date ,'%Y-%m') month_of_ticket,*/ a.closed_date_id, b.next_created_date
from tmp_missing_combination a
join tmp_next_created_Date b
on a.business_area=b.business_area
and a.hf_client_name=b.hf_client_name
and **DATE_FORMAT(a.closed_date ,'%Y-%m')=b.month_of_ticket** ;

LIMIT 1 by Column On Query Returning Multiple Results?

I have a user_certifications table, with these props:
int user_id
int cert_id
timestamp last_updated
So, a user can be listed multiple times with a variety of certs. I have a list of user_ids for which I need to get ONLY the most recently updated record. So for one user it would be:
SELECT user_id, last_updated
FROM user_certifications
WHERE user_id == x
ORDER BY last_updated DESC
LIMIT 1
How do I do this efficiently if I need ONLY the last dated entry for each of a number of users. E.g. similar query, but WHERE user_id IN (x,y,z) and returning one entry per user, with the latest date?
P.S. - I apologize for the title, I don't know how to word this.
Use distinct on:
SELECT DISTINCT ON (user_id), uc.*
FROM user_certifications uc
ORDER BY user_id, last_updated DESC

Get unique records from table avoiding all duplicates based on two key columns

I have a table Trial_tb with columns p_id,t_number and rundate.
Sample values:
p_id|t_number|rundate
=====================
111|333 |1/7/2016||
111|333 |1/1/2016||
222|888 |1/8/2016||
222|444 |1/2/2016||
666|888 |1/6/2016||
555|777 |1/5/2016||
pid and tnumber are key columns. I need fetch values such that the result should not have any record in which pid-tnumber combination are duplicated. For example there is duplication for 111|333 and hence not valid. The query should fetch all other than first two records.
I wrote below script but it fetches only the last record. :(
select rundate,p_id,t_number from
(
select rundate,p_id,t_number,
count(p_id) over (partition by p_id) PCnt,
count(t_number) over (partition by t_number) TCnt
from trialtb
)a
where a.PCnt=1 and a.TCnt=1
The having clause is ideal for this job. Having allows you to filter on aggregated records.
-- Finding unique combinations.
SELECT
p_id,
t_number
FROM
trialtb
GROUP BY
p_id,
t_number
HAVING
COUNT(*) = 1
;
This query returns combinations of p_id and t_number that occur only once.
If you want to include rundate you could add MAX(rundate) AS rundate to the select clause. Because you are only looking at unique occurrences the max or min would always be the same.
Do you mean:
select
p_id,t_number
from
trialtb
group by
p_id,t_number
having
count(*) = 1
or do you need the run date too?
select
p_id,t_number,max(rundate)
from
trialtb
group by
p_id,t_number
having
count(*) = 1
Seeing as you are only looking items with one result using max or min should work fine

SQL count, use only last record

can someone help me about counting rows in sql. I have a table, archive, in which I have bank account and status of that account. One account can have and usually have more records, in my count I have to use last record, not records before. Example:
account status
5552222 A
5552222 B
5552222 A
**5552222 B**
4445896 A
4445896 B
**4445896 A**
I have to use this who are bold. Based on this there is one B(blocked) and one A(active) account. I have column datetime, which can tell me what is last record. I just need query to count that
Assuming you want to count based on the most current row for an account:
SELECT tab.status,
COUNT(*)
FROM tab JOIN
(
SELECT account, MAX(datetime) AS maxdate
FROM tab
GROUP BY account
) AS dt
ON tab.account = dt.account
AND tab.datetime = dt.maxtime
GROUP BY tab.Status
SELECT COUNT(*)
FROM yourTable
WHERE Status='B'
or
WHERE AccountName LIKE '%B'
Edit: After OP modified the question to include the table data.
So, the problem is that the same account number can occur multiple times, and you want to count on the basis of last status of the account.
If the account is currently blocked, you would like to count it, irrespective of the number of times it gets blocked earlier.
Assumption: You have a date type column in your table which shows the date when the record's (with new status value) was inserted (or it may be an identity field which keeps track of the order of records created in the table)
The query will be:
SELECT COUNT (*)
FROM
(
SELECT DISTINCT
acNumber,
( SELECT Max(identityField_or_dateField)
FROM tableName t
WHERE t.acNumber = t2.acNumber AND Status='B')
FROM tableName t2
WHERE
( SELECT Max(identityField_or_dateField)
FROM tableName t
WHERE t.acNumber = t2.acNumber AND Status='B') IS NOT NULL
) tblAlias
Glad to help! Please remember to accept the answer if you found it helpful.

Find id of all ads posted within the past three days, Sql

I am new to SQL. I have a table which contains information about ads. Which function should I use, to find id of all ads posted within the past three days?
For Oracle DB, use this,
SELECT id
FROM tablename
WHERE TRUNC(pdate) >= TRUNC(SYSDATE) - 3;
SQLFIDDLE DEMO
Select ID --the id field
From tblAds --the ads table
WHERE Ad_Date >= DATEADD(day,-3,getDate()) --the filter. I am assuming Ad-Date is the name of the ad's date field
order by Ad_Date ASC -- order it