I want to select the max customer ID from the same account_number.
select max (customer_ID),account_number
from Account
group by account_number
having account_number ='30010258'
The result is
I expect result is
Use a where clause instead of having.
Note: if account_number is of type int then remove the quotes around 30010258.If the datatype for customer_ID is string then you will have to cast it to int.
select max (customer_ID),account_number
from Account
where account_number ='30010258'
group by account_number
select max (int(customer_ID)),account_number
from Account
group by account_number
having account_number ='30010258'
Related
Assuming there is a table with 100 columns, how can I select all columns with a sum without having to type out all the columns?
For example something like this:
select *, sum(price) as sales
from table
group by *
order by date
try this
select table.* , t.sales from table
inner join (select id, sum(price) as sales from table group by id ) t
on table.id=t.id
order by date
But in general it is not recommended to use an stare in a select statement,
for example dont use * in table-valued function
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.
Select id, name , max(modify_time)
from customer
group by id, name
but I get all records.
Order by modify_time desc and use row_number to number the row for id,name combination.Then select each combination with row_number = 1
select id,modify_time,name
from (
select id,modify_time,name,row_number() over(partition by id order by modify_time desc) as r_no
from customer
) a
where a.r_no=1
Ids are unique, which means grouping them by the id, will result in the same table.
My suggestion would be, to order the table by "modify_time" descending and limit the result to 1 (Maybe something like the following):
Select id, name modify_time from customer ORDER BY modify_time DESC limit 1
The reason you are getting the whole table as a result is because you are grouping by id AND name. That means every unique combination of id and name is returned. And since all names per id are different, the whole table is returned.
If you want the last modification per id (or name) you should only group by id (or name respectively).
I need to select a UserID from the table whose sum of Data greater than 24.
I can able to select group and sum the records using
SELECT SUM(DATA),UserID FROM TableName GROUP BY UserID
But how can I select only the records for which SUM(DATA)>24
I have tried
SELECT SUM(DATA),UserID FROM #tempTimesheetValue where SUM(DATA)>24 GROUP BY UserID
But its not working.
Thanks in advance for suggestion..,
you can do this by below query:
select UserID, DATA from (
SELECT SUM(DATA) as DATA, UserID FROM #tempTimesheetValue GROUP BY UserID
) A where DATA > 24
The question might as well have the correct answer, which is;
SELECT SUM(DATA), UserID
FROM #tempTimesheetValue
GROUP BY UserID
HAVING SUM(DATA) > 24;
A subquery could be used, but it is unnecessary complication.
What query will count the number of rows, but distinct by three parameters?
Example:
Id Name Address
==============================
1 MyName MyAddress
2 MySecondName Address2
Something like:
select count(distinct id,name,address) from mytable
To get a count of the number of unique combinations of id, name and address:
SELECT Count(*)
FROM (
SELECT DISTINCT
id
, name
, address
FROM your_table
) As distinctified
Get all distinct id, name and address columns and count the resulting rows.
SELECT COUNT(*) FROM mytable GROUP BY id, name, address
Another (probably not production-ready or recommended) method I just came up with is to concat the values to a string and count this string distinctively:
SELECT count(DISTINCT concat(id, name, address)) FROM mytable;
You can also do something like:
SELECT COUNT(DISTINCT id + name + address) FROM mytable
Having to return the count of a unique Bill of Materials (BOM) where each BOM have multiple positions, I dd something like this:
select t_item, t_pono, count(distinct ltrim(rtrim(t_item)) + cast(t_pono as varchar(3))) as [BOM Pono Count]
from BOMMaster
where t_pono = 1
group by t_item, t_pono
Given t_pono is a smallint datatype and t_item is a varchar(16) datatype