I have sql script for grouping values
select count(*), equity_name, date, field
from balance
group by equity_name, date, field
having count(*)>1
How can i get ids for each group? In future i need to insert dublicates values to another table.
My current result:
Result what i need:
If you want to assign a sequential number to the groups, you can use:
select count(*), equity_name, date, field,
row_number() over (order by equity_name, date, field) as id
from balance
group by equity_name, date, field
having count(*) > 1;
If you want a stable "name" for the group that doesn't depend on the data in the table, you can just concatenate the fields together:
select count(*), equity_name, date, field,
concat_ws(':', equity_name, date, field) as id
from balance
group by equity_name, date, field
having count(*) > 1;
However, if you really care about persisting the group over time, I would suggest that you save the results into a table with an auto-generated identity column.
Related
I need to set a query that gets as result a table with single records for a specific column values from the main table, each record must have the newest date and if the latest date is the same in multiple records, i need only one record with the greatest id.
Up to now I'm using
Select id, p_id, max(date), column1, etc
From table
Group by p_id
But I need the other part to have single record for p_id each with latest date and greatest id among the others.
Just use distinct on:
select distinct on (p_id) t.*
from mytable t
order by p_id, date desc, id desc
For each p_id, this gives you the row with the greatest date. Column id is used as a tie-breaker.
So I have the table below, I want to alter the table to display the second table, where I count up the number of IDs, then reset the count as I come to a different ID value.
Current Table
Desired Table
Just use row_number():
select t.*, row_number() over(partition by id order by case_number) cnt
from mytable t
Situation:
I have three columns:
id
date
tx_id
The primary id column is tx_id and is unique in the table. Each tx_id is tied to an id and it has a record date. I would like to test whether or not the tx_id is incremental.
Objective:
I need to extract the first tx_id by id but I want to prevent using ROW_NUMBER
i.e
select id, date, tx_id, row_number() over(partition by id order by date asc) as First_transaction_id from table
and simply use
select id, date, MIN(tx_id) as First_transaction_id from table
So how can i make sure since i have more than 50 millions of ids that by using MINtx_id will yield the earliest transaction for each id?
How can i add a flag column to segment those that don't satisfy the condition?
how can i make sure since i have more than 50 millions of ids that by using MINtx_id will yield the earliest transaction for each id?
Simply do the comparison:
You can get the exceptions with logic like this:
select t.*
from (select t.*,
min(tx_id) over (partition by id) as min_tx_id,
rank() over (partition by id order by date) as seqnum
from t
) t
where tx_id = min_tx_id and seqnum > 1;
Note: this uses rank(). It seems possible that there could be two transactions for an id on the same date.
use corelated sunquery
select t.* from table_name t
where t.date= ( select min(date) from table_name
t1 where t1.id=t.id)
Using MS Access SQL
Is it possible to;
list and count all duplicates in one field based on another field?
list all non duplicates in one field based on another field?
Example database below
Based on your results, you just want a simple group by:
select name, year, count(*)
from [table]
group by name, year;
One statement cannot return two different headers. I mean, you could run two queries:
select name, year, count(*) as NumDuplicates
from [table]
group by name, year
having count(*) > 1;
select name, year, count(*) as NumNonDuplicates
from [table]
group by name, year
having count(*) = 1;
I have a table made up of dates and sales totals for the particular date. I would like to be able to query the table and select the following: max sales, the date associated with the max sale figure, sum of all sales, and the minimum date in the table. One additional complication is that there are duplicate max values. I don't care which max value is chosen but I just want one at random. This is for Oracle.
Here is what I tried. It was using a sub query.
Select sales, date, min(date), sum(sales) from table
Where sales = (select distinct(max(sales)) from table)
select
max(sales),
max(date_) keep (dense_rank first order by sales desc),
sum(sales),
min(date_)
from
table_
See also This SQL Fiddle