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

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

Related

How to create groups in ReportViewer?

I have this result on my query.
This result is from my sales table, each sale has an ID, and I have a table of products sold, each product sold has the sale ID as a foreign key, which represents the ID_NF in the result below:
How do I create a table or matrix in which each product that has the ID_NF equal to the sales ID is within its group.
You can join result which is from sales table with table of products. Condition for join will be ID = ID_NF. Sort it by sales, and then by products, for the sake of the result on report.
SELECT * FROM dbo.Sales s
INNER JOIN dbo.Products p ON s.ID = p.ID_NF
ORDER BY
s.ID,
p.ID_Produto
This select is just representing what you should do, write it so it suits your needs.
In your report (.rdl, or .rdlc file), create a table which will provide data about sales.
Set value of any column, for instance for the first column.
Group rows by sales id. You can do it by these steps:
right click on the row
from the menu select Row Group
select Group Properties
in newly opened window choose tab/card General
go to section Group expressions
click the Add button
in the newly added dropdown list choose id of the sale
One of the columns will be about products. Insert new table in the cell that is intersection of row that represents sale and column that represents products. In the newly added table add needed columns.
In this example new table of three columns is added in column Products.
Add values for the rest of the columns.
For these results
Report should look like this

Display Data From 2 Table in GridView Asp.net

i have Two Table Like This
Request Example UniTerm Example
St_Code 1 Id 1 2
St_Name FirstStudent Term 96-97 95-94
St_Family FirstFamily
and i have a Grid View contain St_Code,Name,Family and Term
now the problem is i want to display all records of the student with the Just Last Record Term field in Term table
i need gridview data like below
1 FirstStudent FirstFamily 96-97
show last record of the Term table and all of the Student table Records
i tried this SQL Code to select them
SELECT Request.St_Code, Request.St_Name, Request.St_Family, UniTerm.Term FROM Student CROSS JOIN UniTerm ORDER BY UniTerm.Term DESC
its ok but shows all of the records in UniTerm table i need just last one!
thanks in advance
Just update your query
SELECT Request.St_Code, Request.St_Name, Request.St_Family, UniTerm.Term FROM Student CROSS JOIN UniTerm ORDER BY UniTerm.Id DESC

SQL Join to get none existing

I have two tables in SQL Server DB. I have a dropdownmenu on my c# page where staff should be able to choose the relevant plan and it should show all users who do not have a current plan setup
Table 1 contains all users
Table 2 only contains people who have a plan. They also can have different plan_id's in table 2 from 1-10.
I need a SQL join which returns all people who do not have a plan at all or the plan id does not match the one selected from the dropdownlist
E.G a user could exist on table 2 with 3 entries with plan_id's 2,3 and 4.
E.G If a user selects plan 7 from the downdownlist, the user would appear in the list as they currently do not have a plan matching the id selected.
Table 2 plans also have an expiry_date and "active" field.
The plan should also not be valid if the expiry date is in the past or active is "0"
Write a not in Query, Where user does not exist in Table 2 with that plan id.
Suppose
Table_1 -> Userid, Username
Table_2 -> UserId, PlanId
SELECT UserId, Username FROM Table_1
WHERE UserId NOT IN ( SELECT UserId FROM Table_2 WHERE PlanId = 1 )
This query will return all the users who are not linked with Plan 1.

Make a new table in DB to extract data from an already existing large table

I am working on a database which has around 2 year data and has around 100 million rows and 30 columns with values of every 10 seconds of different parameters. I want to create a new table which will have average of these data containing only 1 row for each date of data. The database has around 100 000 rows for each date.
Table name is process
and primary key is id
How can I do it because whenever I search for something in this already existing table it takes a long time to find out the required output.
is it possible to create a new table which will take the average of all the data(around 1 lakh rows) of a single date and put them in one row
You want something like:
CREATE TABLE averages AS
SELECT
date_trunc('day', data_capture_timestamp_column) AS day,
avg(col1) AS col1_avg,
avg(col2) AS col2_avg,
...
FROM my_table
GROUP BY 1;
The GROUP BY 1 says to GROUP the data by the first SELECT argument, which in this case is the date. An expression index on my_table( date_trunc('day', data_capture_timestamp_column)) is recommended.

names repeated in dropdown.(sql query )

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

Categories

Resources