names repeated in dropdown.(sql query ) - c#

SELECT ID,Name FROM Master
id Name
1 John
2 John
3 Jack
4 Max
5 Jack
6 Max
7 Max
using above query i get all names repeated in dropdown.
i have to bind data in dropdown foreach Name there have
respective ID How can i do that.

I don't know the logic behind the data, but to get one name only once you have to filter out duplicate names and settle for one of the ids from the duplicate names.
SELECT MIN(id), Name
FROM Master
GROUP BY Name
The query above will output the occurence with the lowest id for each name, given that the id column is numeric.
The reason that your distinct query does not work, is that distinct filters on all columns in the result set, so in this case both name and id are used for uniqueness.
If you for some reason need all the ids for each name you have to filter on the ASP.NET side or look up the ids in the database once you know which person was selected.

Try this query it will return grouped as one
SELECT GROUP_CONCAT(ID), Name FROM your_tbl Group BY Name

Only place the Name column you will get the unique output. I think the problem is that the distinct keyword checks the primary key which is different per each row so it returns all the columns you only have to place the Name column. ( my understanding about distinct )
SELECT distinct Name FROM Master
output
Jack
John
Max
-- Below is what i tested
-- Created a table
Create table mytable(
ID bigint identity primary key,
UserName varchar(50)
)
-- Insert the records
INSERT INTO dbo.mytable(UserName) VALUES('John')
INSERT INTO dbo.mytable(UserName) VALUES('John')
INSERT INTO dbo.mytable(UserName) VALUES('Jack')
INSERT INTO dbo.mytable(UserName) VALUES('Max')
INSERT INTO dbo.mytable(UserName) VALUES('Jack')
INSERT INTO dbo.mytable(UserName) VALUES('John')
-- If i use below query
select distinct * from mytable
ID UserName
1 John
2 John
3 Jack
4 Max
5 Jack
6 John
-- and if i use this
select distinct UserName from mytable
output
Jack
John
Max
-- I think you should not allow the user to enter multiple users with the same name
-- what do you think about this
select UserName +' '+CAST(ID as varchar) as [Users] from mytable
John 1
John 2
Jack 3
Max 4
Jack 5
John 6
-- IN your code you can create a list<string,int> and stored in it. you can easily manage it in code

Related

How to perform the following function using SQL query in c# and SQLSERVER?

how can i copy all records from one table to another without name repetition and summing their values
in another table. THANKS
Source_table
name value
a 5
b 10
a 10
Desired Output:
Dest_table
name value
a 15
b 10
Try below query:
INSERT INTO Dest_table
(name,value)
SELECT name,sum(value) AS value
FROM Source_table
GROUP BY name
Try following query: but for good post it's better if you format your question properly
select name, sum(value) as value from source_table
group by name

how to show multiple distinct columns of one table in sql query

How to show or retrieve multiple columns with distinct value from one table in sql query ?eg: in my database i have account id 13 and id 13 have balance of 8000 and remaining fields are different but id and balance is same in 9 records, so i want id and balance to display only once and for remaining fields it should display 9 records
Try like this;
SELECT DISTINCT c1,c2,c3 FROM table
Or you can use group by
SELECT c1,c2,c3 FROM table group by c1,c2,c3

Add a counter to a sql table

I'd like to add a column to a table whose value counts the number of times it is repeated on another table. Something like this:
Name NameID Quantity | NameID Franchisee Business BusinessID
John 12345 2 | 12345 CA Laundry 45678
Smith 45684 1 | 12345 CA Bakery 45679
| 45684 NY Shampoo 45680
The column Quantity is the one I want to add, I want it to count all the BusinessID that belong to his NameID: John has a NameId of 12345 and that NameID has 2 BusinessIDs assosiated to it.
I don't know how to do this, I want to add this value to an aspx project, maybe it'd be easier to make a function in c# to keep the DB as is and just show the values on the client.
In general you should not store that value. If you do, you'll have to update it every time you change the 2nd table, and then you'll have problems like what do I do if the update of the 2nd table succeeds, but that of the 1st table fails? (answer: use a transaction).
It is way simpler to calculate that value on the fly. You can do this in SQL:
SELECT
t1.Name, t1.NameID, COUNT(*) AS Quantity
FROM
Table1 t1
INNER JOIN Table2 t2 ON t1.NameID = t2.NameID
GROUP BY
t1.Name, t1.NameID
The only reason to store it would be if that value was an expensive calculation (in this case, it is not).

Get the Rows separately based on the ID in sql

I have a table as follows,
TypeID Name Date
-------------------------------
1 Carrot 1-1-2013
1 Beetroot 1-1-2013
1 Beans 1-1-2013
2 cabbage 1-1-2013
2 potato 1-1-2013
2 tomato 1-1-2013
2 onion 1-1-2013
If need 2 rows then it should return 2 rows from TypeId 1 and 2 rows from TypeId 2.If need the only 4 rows, means I have to get 4 rows from TypeId 1 and 4 rows from TypeId 2
but TypeId 1 has only 3 rows so we need to get only 3 rows for typeId 1
How to do that? Shall I add RowNumber?
For SQL Server;
EDIT: Your question changed slightly;
If you want want a maximum of x items per category, you can use ROW_NUMBER();
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY TypeID ORDER BY Name) rn FROM Table1
)
SELECT TypeID, Name, [Date] FROM cte
WHERE rn <=3 -- here is where your x goes
ORDER BY TypeID;
An SQLfiddle to test with.
You can write your query to order by the TypeID.
Then, if you're using SQL, you could use SELECT TOP N or LIMIT N (depending on the DB), or with TSQL and SQL Server, use TOP(N) to take the top N rows.
If you're using a LINQ based ORM from your C# code, then you can use Take(N), which automatically creates the appropriate query based on the provider details to limit the number of results.
I think you should use a query to select your 3 rows from type 1.....and then the however many rows from type 2 and then add the results together.
;With CTE(TypeID,Name,Date,RowNo)
AS
(
select *, ROW_NUMBER() OVER(PARTITION BY ID ORDER BY ID) from TableVEG
)
Select Top(#noofRows*2) * from CTE where RowNo<=#noofRows order by rowno
The above query worked.. Thank u all... :-)

Datatable Recors

I have a datatable and it has 4 columns. My problem is some columns has same data, same date. I have to delete same data, same date. How can I delete dublicate data?
My Datatable:
In this table I have to delete 1 or 3 (Id) In code side with for or foreach loop. Because in the same date there is a same Isban.
Id Name Isban Date
1 A 123 09.09.2010
2 B 123 10.09.2010
3 C 123 09.09.2010
4 A 234 11.09.2010
5 B 342 12.09.2010
Thanks You
john
A standard way to do this is to run a select distinct query to insert the distinct records into a new table, delete the existing table, and then rename the new table to the previous table.
Edit: You can that you have to do this client-side.
One way is addressed here: Distinct in DataTable
Alternatively, loop through the table and store in a hash table each record; use the pair Isban/Date as the key and the record as the value. When you encounter a duplicate record it will already be in the hash table so you pass over it. Then, you can create a new data table from the records in the hash table.
If you DO have to do it in a loop, I would do it something like the following... Pre-query based on the minimum ID based on the given duplicate entitie elements, then delete for NOT being the minimum key
Select
FldDup1,
FldDup2,
min( IDKey ) as KeepThisID,
count(*) as TotalPerDupFields
from
YourTable
group by
FldDup1,
FldDup2
having
TotalPerDupFields > 1
In this case, you'll end up with a sample result of...
FldDup1 FldDup2 KeepThisID TotalPerDupFields
123 09.09.2010 1 2
as I was ignoring the 2nd column of "A", "B" and "C" as it didn't appear to be the indicator in your explanation of duplicates.
Then, I would isse a delete... via parameterized SQL-Delete query
Delete from YourTable
Where FldDup1 = ResultQuery.FldDup1
and FldDup2 = ResultQuery.FldDup2
and NOT IDKey = ResultQuery.KeepThisID

Categories

Resources