I have a table where as below, I want to get records by id which have value b if value b is not there then value b. I have a date volumn associated in the table by which I can use order by date.
I tried to post an image of screen from mobile it failed attach.
Also could not post sql which i tried as it is officially not allowed here.
My attempt to explain the question is below
Persid. Value. Date
1 a
1 a
1 a
2 b
2 a
2 a
Expected result
1 a. Latest date
2 b any date
I have tried using group by on id and value columns to get count but I am not able to understand how to use that count to filter out records.
Any help is greatly appreciated :)
You can use row_number():
select id, value, date
from (select t.*,
row_number() over (partition by persid
order by value desc, date desc
) as seqnum
from t
) t
where seqnum = 1;
This gets the latest date for any value. Note that the value desc is so "b" appears before "a". If your values are not ordered alphabetically, then you can use a case or other mechanism to set the proper sort order.
Related
I would like to get a new ID, no matter the format (in the example below 11,12,13...)
Based on the following condition:
Every time the days column value is greater then 1 and not null then current row and all following ones will get the same ID until a new value will meet the condition.
Within the same email
Below you can see the expected 1 (in the format of XX)
I thought about using two conditions with the following order between them
Every time the days column value is greater then 1 then all following rows will get the same ID until a new value will meet the condition.
2.AND When lag (previous) is equal to 0/1/null.
Assuming you have an EmailDate column over which you're ordering (a DATETIME field, really), try something like this:
WITH
TableNameWithEmailDateIDs AS (
SELECT
*,
ROW_NUMBER() OVER (
ORDER BY
Email DESC,
EmailDate
) AS EmailDateID
FROM
TableName
),
IDs AS (
SELECT
*,
LEAD(EmailDateID, 1) OVER (
ORDER BY
Email,
EmailDate
) AS LeadEmailDateID
FROM
(
SELECT
*,
-- REMOVE +10 if you don't want 11 to be starting ID
ROW_NUMBER() OVER (
ORDER BY
Email DESC,
EmailDate
)+10 AS ID
FROM
TableNameWithEmailDateIDs
WHERE
Days > 1
OR Days IS NULL
) X
)
SELECT
COALESCE(TableName.EmailDate, IDs.EmailDate) AS EmailDate,
IDs.Email,
COALESCE(TableName.Days, IDs.Days) AS Days,
IDs.ID
FROM
IDs
LEFT JOIN TableNameWithEmailDateIDs TableName
ON IDs.Email = TableName.Email
AND TableName.EmailDateID BETWEEN
IDs.EmailDateID
AND IDs.LeadEmailDateID-1
ORDER BY
ID DESC,
TableName.EmailDate DESC
;
First, create a CTE that generates IDs for each distinct Email/Date combo (helpful for LEFT JOIN condition later). Then, create a CTE that generates IDs for rows that meet your condition (i.e. the important rows). Finally, LEFT JOIN your main table onto that CTE to fill in the "gaps", so to speak.
I suggest running each of the components of this query independently to fully understand what's going on.
Hope it helps!
I have table as below:
I want write a sql query to get output as below:
the query should select all the records from the table but, when multiple records have same Id column value then it should take only one record having latest Date.
E.g., Here Rudolf id 1211 is present three times in input---in output only one Rudolf record having date 06-12-2010 is selected. same thing with James.
I tried to write a query but it was not succssful. So, please help me to form a query string in sql.
Thanks in advance
You can partition your data over Date Desc and get the first row of each partition
SELECT A.Id, A.Name, A.Place, A.Date FROM (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Date DESC) AS rn
FROM [Table]
) A WHERE A.rn = 1
you can use WITH TIES
select top 1 PERCENT WITH TIES * from t
order by (row_number() over(partition by id order by date desc))
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=280b7412b5c0c04c208f2914b44c7ce3
As i can see from your example, duplicate rows differ only in Date. If it's a case, then simple GROUP BY with MAX aggregate function will do the job for you.
SELECT Id, Name, Place, MAX(Date)
FROM [TABLE_NAME]
GROUP BY Id, Name, Place
Here is working example: http://sqlfiddle.com/#!18/7025e/2
The confusing question is best asked through an example. Say we have the following result set:
What I want to do is count how many times one number appears from both columns.
So the returning data set might look like:
ID Counted
0 4
1 2
9 1
13 1
My original thought was to do some sort of addition between the counts on both IDs, but I'm not exactly sure how to GROUP them in SQL in a way that is working.
You can do this with a subquery, GROUP BY, and a UNION ALL, like this:
SELECT ID, COUNT(*)
FROM(
SELECT ID1 AS ID FROM MyTable
UNION ALL
SELECT ID2 AS ID FROM MyTable
) source
GROUP BY ID
ORDER BY ID ASC
Suppose I have a table filled with the data below, what SQL function or query I should use in db2 to retrieve all rows having the FIRST field FLD_A with value A, the FIRST field FLD_A with value B..and so on?
ID FLD_A FLD_B
1 A 10
2 A 20
3 A 30
4 B 10
5 A 20
6 C 30
I am expecting a table like below; I am aware of grouping done by function GROUP BY but how can I limit the query to return the very first of each group?
Essentially I would like to have the information about the very first row where a new value for FLD_A is appearing for the first time?
ID FLD_A FLD_B
1 A 10
4 B 10
6 C 30
Try this it works in sql
SELECT * FROM Table1
WHERE ID IN (SELECT MIN(ID) FROM Table1 GROUP BY FLD_A)
A good way to approach this problem is with window functions and row_number() in particular:
select t.*
from (select t.*,
row_number() over (partition by fld_a order by id) as seqnum
from table1
) t
where seqnum = 1;
(This is assuming that "first" means "minimum id".)
If you use t.*, this will add one extra column to the output. You can just list the columns you want to avoid this.
Ladies and Gents,
I need to write a query that grabs data from a view, but I'm not sure how to go about this. The issue is there is really no key and there are two fields I'm concerned with that will control what rows I need to retrieve.
The view looks something like this:
Category columna columnb uploaddate
-----------------------------------------------------
a value value 1/30/2013 04:04:04:000
a value value 1/29/2013 04:04:04:000
b value value 1/28/2013 01:23:04:000
b value value 1/30/2013 04:04:04:000
b value value 1/30/2013 04:04:04:000
c value value 1/30/2013 01:01:01:000
c value value 1/30/2013 01:01:01:000
What I need to retrieve is all rows for each unique category and the newest uploaddate. So in the example above I would get 1 row for category a which would have the newest uploaddate. Category b would have 2 rows which have the 1/30/2013 date. Category c would have two rows also.
I also need to just compare the date of upload, not the time. As the loading can take a couple seconds. I was trying to use max date but it would only grab the time to the second.
Any guidance/thoughts would be great.
Thanks!
EDIT:
Here is what I threw together so far and I think it's close but it's not working yet and I doubt this is the most efficient way to do this.
select
*
from
VIEW c
INNER JOIN
(
SELECT
Category,
MAX(CONVERT(DateTime, Convert(VarChar, UploadDate, 101))) as maxuploaddate
FROM
View
GROUP BY
Category,
UploadDate
) temp ON temp.Category = c.Category AND CONVERT(VarChar, UploadDate, 101) = temp.maxuploaddate
The problem lies in the nested selected statement as it's still grabbing all combinations of Category and Upload date. Is there a way to do a distinct on the Category and UploadDate, just getting the newest combination?
Thanks Again
Your query is close, you have a mistake in the group by. I'd also get rid of the date conversions; date comparisons work fine.
select
*
from
VIEW c
INNER JOIN
(
SELECT
Category,
MAX(UploadDate) as maxuploaddate
FROM
View
GROUP BY
Category
) temp ON temp.Category = c.Category AND UploadDate = temp.maxuploaddate
If you want to do this to the nearest date, you need to convert to a date first. In SQL Server syntax:
select *
from (select category, columna, columnb, uploaddate,
rank() over ( partition by category order by cast(uploaddate as date) desc) as seqnum
from view
) v
where seqnum = 1
In Oracle syntax:
select *
from (select category, columna, columnb, uploaddate,
rank() over ( partition by category order by to_char(uploaddate, 'YYYY-MM-DD') desc) as seqnum
from view
) v
where seqnum = 1
Because you want ties, these use rank() instead of row_number().
In Oracle you can use Rank() to achieve this. Rank() creates a duplicate number if the same criteria are met.
Edit: And you can use Trunc() to "trim" the time from the uploaddate.
select *
from (select category, columna, columnb, uploaddate,
rank() over ( partition by category order by trunc(uploaddate) desc) rank
from view)
where rank = 1
Also Dense_Rank() exists, which won't create duplicate numbers. So this is not applicable here. See this question for more info on the differences.