I want to select distinct value in the query but it shows multiple values. I have 2 identical names in my name column. I think distinct is for unique value but I don't know what is happening.
Here is my query
string lakhas1 = "SELECT DISTINCT
NAME,EXPENSE,AMOUNT1,AMOUNT2,AMOUNT3,AMOUNT01,AMOUNT02,AMOUNT03 FROM
INCOME ORDER BY NAME";
DataTable dt1 = DataAccess.GetDataTable(lakhas1);
In order to have your result set be distinct by name, you can use the group by keyword to group the records. When you do this, any other columns you want to select either have to be part of the group by (i.e. part of the key that makes the record distinct) or they have to be used in an aggregate function.
In this case, I assumed you want to sum the values from the records together. However, you could use min() or max() just the same.
select
name,
sum(expense) as expense,
sum(amount1) as amount1,
sum(amount2) as amount2,
sum(amount3) as amount3,
sum(amount01) as amount01,
sum(amount02) as amount02,
sum(amount03) as amount03
from dbo.income
group by name
order by name
Related
first of all I am sorry if this question is too obvious, since I am quite new in SQL.
So, I have a list of IDs (variable, depending how many products the user chooses). And I want to check if all of them are in a table. If one of them is not, the result of the query should be null. If all of them are there, the result should be all the rows where those IDs are.
How can I do this?
Best regards,
Flavio
Do a LEFT JOIN from the list to the table on the ID field. You'll get a null if there is no record
You can even put a WHERE clause like 'WHERE List.ID IS NULL' to only see those that aren't in the table
Edit: Original Poster did not say they were using C# when I wrote this answer
UNTESTED:
Not sure if this is the most efficient but it seems like it should work.
1st it generates a count of items in the table for your list. Next it cross joins the 1 result record to a query containing the entire list ensuring the count matches the count in your provided list and limiting the results to your list.
SELECT *
FROM Table
CROSS JOIN (
SELECT count(*) cnt
FROM table
WHERE ID in (yourlist)) b
WHERE b.cnt = yourCount
and ID IN (YourList)
Running two in statements seems like it would be terribly slow overall; but my first step when writing SQL is usually to get something that works and then seek to improve performance if needed.
Get the list of Ids into a table, (you can pass them as a table variable parameter to a Stored proc), then in the stored proc, write
assuming the list of ids from C# is in table variable #idList
Select * from myTable
Where id in (Select id from #idList)
and not exists
(Select * from #idList
where id Not in
(Select id from myTable))
I have a query like this:
Select table1.*, table2.column1 from table1 join table2 on table1.column1=table2.column1
It works, but it puts the column in the end of the datagridview, but i have to put table2.column1, after a specified column of table2, and i have to use table1.* and i cant use listing of the table1's columns is it possible?
And why exactly can't you use a list of all the fields?
NO , it's not possible to place a column in the middle of columns specified with * , not with pure SQL and not with dynamic.
Just specify them, don't be lazy, it's better practice:
SELECT table1.col1,
table1.col2,
table2.col1,
table1.col3
..........
because i am using union queries, and the table names are changing and one table contains more colums than the other
if table1 differs, that above all should be a strong argument for specifing all needed fields separatly. In case of a new field in table1, your query would be broken, cause the number of fields will differ from the ones used in the next union.
I had a problem in retrieving the Distinct values from my MySQL DB from a table from a particular column. I have a table in which i have repeated values, so while retrieving the distinct values am not getting distinct as am trying to retrieve the ID and Marks field.
Here is the table
So here am trying to get the distinct values from Total Marks into a dropdownlist box using Asp.net and i need to get student id also for further process.
I am trying with a query as below:
Select Distinct TotalMarks,Student_id from studenttable;
Is this correct query?
I need to get the output in dropdownlist as distinct values of TotalMarks and ID.
write this way:
Select Distinct TotalMarks,Student_id from studenttable group by Stuident_id;
The question is, if student 1 and student 2 have total marks 80. And you want to fetch only one of them (distinct total marks), which one is it?
Anyway the query would look like this:
SELECT TotalMarks, MIN(Student_id) AS Student_id FROM studenttable GROUP BY TotalMarks;
This query would fetch 1st student with such Total marks from each group. If you want the last one just change the aggregate function to MAX
Try this query
SELECT MAX(Student_id),TotalMarks
FROM studenttable
GROUP BY TotalMarks
I have a database table like this:
I want to get the the row number of the second row. I use the following code:
SELECT ROW_NUMBER() OVER(ORDER BY Name) From Deposit WHERE Name='Murali'
But, its not working. Whats wrong with the code?
Thanks in advance.
The ROW_NUMBER function returns the row number in the resulting dataset.
In your query you restricted the results to only those whose name is Murali. Since you have only one such record, it is normal that it will return 1.
In SQL there's no such notion as row number. Table rows do not have an order. The notion of order only makes sense when you make a SQL query. Without SQL query you simply cannot talk about order and row numbers.
It appears that you need to introduce some order number for each user. The correct way to implement this is to add an Order column to your Deposit table. Now in order to retrieve it you would use the following query:
SELECT [Order] From Deposit WHERE Name = 'Murali'
All that's left is make the Order column to autoincrement and you are good to go. Everytime a new record is inserted the value will be automatically incremented. So there you go, now you have an order which represents the order in which the records have been inserted into the table. You now have context.
Perhaps something like this (if I understood you correctly):
SELECT Q.RN FROM (
SELECT ROW_NUMBER() OVER(ORDER BY Name) AS RN, * From Deposit
) AS Q
WHERE Q.Name = 'Murali'
Try this
WITH TempTable AS
(
SELECT Name,ROW_NUMBER() OVER (ORDER BY Name) AS 'RowNumber'
FROM Deposit
)
SELECT RowNumber,Name
FROM TempTable
WHERE Name='Murali'
This is my table:
I am basically using the following table to pass a select statement and viewing it as a pie graph.The pie graph excludes fields that are null or 0. So First i used the following query to select the total number by its respective group number and it worked fine. This is the working query.
query = "Select groupNumber as \"Group Number\", totalNumber as \"Total Number\" From " + table;
However i found it hard to read since both columns are numbers and therefore figured it will be better to pass a query where i can select the total numbers by its respective groupName. This is a problem though because if u look at the table you will notice that groupNames can be repeated.
Therefore i would like to do a select satement where i can query the total number by the groupnumber and groupname.
Is this possible if so how? Also i can't modify the data as i receive it in such format from a established connection.