SQL Server 2012: MAX on varchar columns not working as expected - sql

I am using aggregates like MAX on varchar columns. When I execute a simple select on a varchar column, I get 4 characters value which is "övrö". In that column I found lots of values that are much bigger in length. My understanding is that it should return a value like "asdfsadfasdfasdfsadfsdafsdafsadfasdfsadfasdf".
Please correct me if I am misunderstanding/misusing MAX as I have to use this concept in a complicated SQL to avoid duplicate records.
If MAX on varchar gets top record based on alphabetical order, how come it still causing the duplicate values if I use it for specific columns (#2 and 3 as shown in the attached image. Red border area is showing duplicates, which should just be a single row.
EDIT - ISSUE RESOLVED
The problem was that I was adding the string columns Aggregates in Group By clause, when removed, duplicates are gone!!!
Thanks for the help.

Min and max get the maximal values. For varchar, it's not the length, but imagine them as sorted alphabetically. Max takes the last element while min takes the first.
If you want to take the maximum length, you need max(length( column ))

Related

Removing rows with duplicated column values based on another column's value

Hey guys, maybe this is a basic SQL qn. Say I have this very simple table, I need to run a simple sql statement to return a result like this:
Basically, the its to dedup Name based on it's row's Value column, whichever is larger should stay.
Thanks!
Framing the problem correctly would help you figure it out.
"Deduplication" suggests altering the table - starting with a state with duplicates, ending with a state without them. Usually done in three steps (getting the rows without duplicates into temp table, removing original table, renaming temp table).
"Removing rows with duplicated column values" also suggests alteration of data and derails train of thought.
What you do want is to get the entire table, and in cases where the columns you care about have multiple values attached get the highest one. One could say... group by columns you care about? And attach them to the highest value, a maximum value?
select id,name,max(value) from table group by id,name

Date Column showing Duplicate Records even after using to_date(to_char(JOB_CLOSED_DATE,'dd-mon-yy')),

I have oracle query which should remove the duplicate records from a date column which consists of time as well. Because of time-stamp there are duplicate records are showing when include other columns along with the date columns. please see attached image from power bi. Is there any way I could be get rid of duplicacy of records.
Select distinct
to_date(to_char(JOB_CLOSED_DATE,'dd-mon-yy'))
From DWH_FACT_DISCRETE_JOB_WIP
First, implicit casting from char to date is very dangerous in oracle - it can result in hard to find bugs.
Second, try to use trunc() function instead of to_char() to get date without time.
Select distinct trunc(JOB_CLOSED_DATE)
From DWH_FACT_DISCRETE_JOB_WIP

SQL query returns different number of rows depending on the columns selected

Thanks for the contributions so far. After more digging I am re-stating the question (and indeed the title of the question) as follows:
I am selecting just 2 columns from a view that contains several columns. The view returns 50,497 rows if I select all columns, but only 50,496 (i.e. 1 fewer) when I select just 2 columns, these being [Patient_ID] (which is a bigint column) and [Condition_Code] (a varchar(6) column).
Version 1:
SELECT * FROM [vw_Query1]
returns 50,497 rows.
But:
SELECT [Patient_ID], [Condition_Code] FROM [vw_Query1]
returns 50,496 rows.
I can post the code for [vw_Query1] if required, but an understanding at a fundamental level how this can happen when no GROUP BY clause has been used is the key question for me.
UPDATE:
It turns out that if I exclude one particular column, I get the lower number of rows of 50,496. This column is unique in having a Case-Sensitive collation. I still dont understand why it is dropping one particular row but at least I am getting closer to an understanding.

String Grouping from a single column in Oracle database having million rows and removing duplicates

We have a huge table and one of the column contains queries like e.g. in row 1
1. (((firstname:Adam OR firstname:Neil ) AND lastname:Lee) ) AND category:"Legal" AND type:Individual
and in row 2 of same column
2. (((firstname:Adam* OR firstname:Neil ) AND lastname:Lee) ) AND category:"Legal" AND type:Organization
Similarly there are few other types of Query strings which are used eventually to query external services.
Issue is based on certain criteria I have to group and remove duplicates from this table.
There are few rules to determine grouping of Strings in different rows.One of them is that if first name and lastname are same then ignore category and type values, therefore above two rows will be grouped to one. There are around million rows. Comparing Strings and doing grouping is not looking elegant solution. What could be best possible solution using sql.

Get row values as column names in t-sql

I have a requirement to display row values as column names in a data grid view. I want to get the store names into columns using sql select statement. (Please refer the attached image). I want user to enter some values under each column. So STORE 1, STORE 2, STORE 3 should displays as columns in datagrid view. Does anyone can help me to get this work?
while googling i found this can be done using PIVOT in SQL. But in this table i don't have any aggregate columns. Any help pls?
the result should be somthing like
You may know that your data only contains a single row for each pivoting column, but SQL Server has to construct a plan that could accommodate multiple rows.
So, use PIVOT and just use an aggregate that, if passed a single value, will return that same value. MIN() and MAX() fit that description (as does SUM if you're working with numeric data)
You may use specific function of dynamic pivot and pass your query with item count column.
You can use below link which provided you function and can easily show you expected output.
http://forums.asp.net/t/1772644.aspx/1
Procedure name:
[dbo].[dynamic_pivot]