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
Related
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** ;
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;
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.
Hi all I am using SQL server.
I have one table that has a whole list of details on cars and events that have happened with those cars.
What I need is to be able to pick out the last entry for each vehicle based on their (Reg_No) registration number.
I have the following to work with
Table name = UnitHistory
Columns = indx (This is just the primary key, with increment)
Transdate(This is my date time column) and have Reg_No (Unique to each vehicle) .
There are about 45 vehicles with registration numbers if that helps?
I have looked at different examples but they all seem to have another table to work with.
Please help me. Thanks in advance for the help
WITH cte
AS
(
SELECT *,
ROW_NUMBER() OVER
(
PARTITION BY Reg_No
ORDER BY Transdate DESC
) AS RowNumber
FROM unithistory
)
SELECT *
FROM cte
WHERE RowNumber = 1
If you only need the index and the transdatem and they are both incremental (I am assuming that a later date corresponds to a higher index number) then the simplest query would be:
SELECT Reg_No, MAX(indx), MAX(Transdate)
FROM UnitHistory
GROUP BY Reg_No
If you want all data for a known Reg_No, you can use Dd2's answer
If you want a list of all Reg_No's with thier data, you will need a subquery
I am lost in MySQL documentation. I have a table with votes - it has these columns
id
song_id
user_id
created
I cannot find the query which will process the information and output the 10 most voted songs in a given time period. What is it?
SELECT id, song_id, COUNT(1) AS total
FROM votes
WHERE created BETWEEN [user_defined_start_date] AND [user_defined_end_date]
GROUP BY song_id
ORDER BY total DESC
LIMIT 10;
How exactly are you storing the votes?
Replace the words in [] below with the variables you want.
select song_id
from [table]
where created > to_date('[the date you want]', '[the format you want]') and created < to_date('[the date you want]', '[the format you want]')
order by [votes]
limit 10;