SQL Server :: How To Use Distinct On A Single Column From A List Of Columns
Jan 21, 2011
I would like to know if there is a way that I can use DISTINCT on one column's values but need to show other columns where the value of the DISTINCT column is equal to another table's column
How can i select all columns of table but to distinct by one column? i am tryin to figure it out without success, i know how to make distinct (select distinct column from table, but i need all the values from the table and to distinct by Delcompany.
I would like to select a distinct query based on three fields and display the rest of the fields. For example,
I have firstname, lastname, address, city, state and zip, but I only want to use the distinct on these fields first and last name. However when i use the distinct function I get a distinct on all the selected rows. I just want to distinct firstname and lastname and display the other fields. for example,
Mary Smith New York Mary Smithy New York Mary Smith Maine
I would like the result to be:
Mary Smith New York Mary Smithy New York
I don't care about Mary Smith Maine, because i am only using the distinct for "Mary Smith" firstname and last name and showing the city.
I have a table 'tbl' and a column 'col'. The elements are like 1,1,2,2,2,3,4,4. Now I want to show two columns one is the one will contain the distinct values from 'col' like 1,2,3,4.... and second will contain the count for each of them like for above case it will be 2,3,1,2.
what I wanna achieve is to have a statement that selects distinct uniqueid's from the chapterID field and with every duplicate I would like to have the record with the highest version number
Select tbl_name.name,tbl_midname.midname,tbl_last.lastname As name From tbl_name inner join tbl_midname on tbl_midname.id=tbl_name.id inner join tbl_last on tbl_last.id=tbl_name.id where tbl_name.id='1'
i use this to select one checkbox to isselected column how i convert this to multi checkboxlist to single column i use many ways more than 3 days without success
private void BindCheckBoxList() { DataTable dt = new DataTable(); SqlConnection connection = new SqlConnection(GetConnectionString()); try { connection.Open(); string sqlStatement = "SELECT * FROM boby"; SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection); SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlDa.Fill(dt); if (dt.Rows.Count > 0) { CheckBoxList1.RepeatColumns = 4; // set the number of columns in the CheckBoxList CheckBoxList1.DataSource = dt; CheckBoxList1.DataTextField = "Name"; // the items to be displayed in the list items CheckBoxList1.DataValueField = "Name"; // the id of the items displayed CheckBoxList1.DataBind(); //Setting the Selected Items in the ChecBoxList based from the value in the database //to do this, lets iterate to each items in the list for (int i = 0; i < dt.Rows.Count; i++) { if (!string.IsNullOrEmpty(dt.Rows[i]["IsSelected"].ToString())) { CheckBoxList1.Items[i].Selected = Convert.ToBoolean(dt.Rows[i]["IsSelected"]); } } } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { connection.Close(); } } private void Update(string name, bool isSelected) { SqlConnection connection = new SqlConnection(GetConnectionString()); SqlCommand cmd; string sqlStatement = string.Empty; try { connection.Open(); sqlStatement = "UPDATE handymen SET IsSelected = @IsSelected WHERE Name = @BizName"; cmd = new SqlCommand(sqlStatement, connection); cmd.Parameters.AddWithValue("@Name", name); cmd.Parameters.AddWithValue("@IsSelected", isSelected); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert/Update error"; msg += ex.Message; throw new Exception(msg); } finally { connection.Close(); } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindCheckBoxList(); } } protected void Button1_Click(object sender, EventArgs e) { string str = string.Empty; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { str = CheckBoxList1.Items[i].Text; Update(str, CheckBoxList1.Items[i].Selected); } } //ReBind the List to retain the selected items on postbacks BindCheckBoxList(); }
I have a question in gridview, I want to combine my two or more records into a single cell with two rowsfor Example i have a ID, Name, Year Start, and Year End for Column name in database and i want call it into my gridview that the Year Start and Year End will combine to Year like:
I am using below query to count a particular column data based on single condition.
[code].....
But I have to calculate for 3 more conditions. Is it possible to count based on 3 conditions(i.e. 2001 - 3000,3001 - 4000, 4001 - 5000,>5000) and I want result set for all the conditions?
I have a master document which is composed of other document types (some are required and some are not). While the master document's status is "Draft" I need to ignore all not nullable constraints (because the writer may decide to work on one of the children documents that is not required and save as draft).
The closest solution I found for this problem is check constraints but I have not yet to see an example of this in check contraint and I'm not even sure if this is possible. Has anyone implemented a similar solution with check constraints (or any other methods)?
I have an object, that has many properties but the only two to worry about are:
myobject.ID which is an int myobject.Names which is a HashSet
Then I have a List of those objects that looks something similar to this:
List<myobject>
I use Linq to get a some of the data into a repeater, but I'm not sure how to get the list of Names and how often they show up.
Want to use Linq to avoid having to loop through the data.
As my tags should show, this is an ASP.NET solution using C#.
Some clarification: Lets say I have just three items in my list: Item 1 has John, Fred, Jack in its names. Item 2 has John, Fred, Joe in its names. Item 3 has John in its names.
I am trying to return the following: John - 3 Fred - 2 Jack - 1 Joe - 1
Also, as a note I am familiar with having to write my own comparer for my object, I'm just missing the 'how to' for the overall solution in my thoughts.
Here is the query I have now which returns over 2 million records:
[Code]....
This works, but the result is littered with many rows that contain the same SSN. So how do I return only one row for each ssn? I cant use distinct because all of the other columns are already always different, this would return the same as above.
I was thinking about the fastest way to retrieve a "set" of objects related to a specific user. For instance, if I was to create a website similar to Flickr where people upload lots and lots of photos(jpegs) what would be the fastest way to be able to retrieve those photos from the SQL Server database. Scenario 1: You store metadata about each file they upload to the website as a single row in a database table. Then when you want to retrieve a specific users uploaded files you simply scan the database file for every row matching the UserID and store them in memory.
This, of course, gets all the information about all the photos a single user has uploaded. My concern with this approach is when the user activity goes from a few thousand to being in excess of a million.
This approach seems fine for a small website but does this method work for a million plus user website like Flicker or MySpace? When you have a million users and millions more photos does this approach still perform at acceptable speeds?
create a collection in memory of "ALL" the PictureIds' for a single user and store them in-memory, in a collection and then save this collection as a single VARBINARY(MAX) field in the SQL Server Database. So instead of having to find multiple records in the Photos database you would simply need to find one, which would include all the PhotoIds' that belong to the user. In essence, translating into the phrase, "If you find one, you have found them all". And improving server performance by leaps and bounds. I only have about a year experience in working with SQL Server and ASP.NET so Im not sure if this solution is practical, if its already been tried, if it can be done.