sum of values in each column in a database table - c#

I have a table like this:
Table name is List.
I have to get sum of each column. I cannot give this query:
SELECT SUM(jayesh,murali,jins) from List;
because the columns are added dynamically. Column names are given as user input. Is there any other code to do this..?

If you want the sum in each column:
SELECT SUM(jayesh), SUM(murali), SUM(jns) from List;

Not saying that any of this is or is not a good idea because no context was provided, but...
Sum of all columns combined for all records (one sum for entire table):
SELECT SUM(jayesh+murali+jns)
FROM List
Sum of all columns for each record (one sum for each row):
(This option requires having an id column to group by so that you can determine how to group the rows)
SELECT
ID,
SUM(jayesh+murali+jns)
FROM List
GROUP BY ID
Sum of each column separately for all records (one sum for each column):
SELECT
SUM(jayesh),
SUM(murali),
SUM(jns)
FROM List
I would also recommend reconsidering your design in adding dynamic columns based on user input. This is generally not a good idea, certainly not for this case. Read more here: http://www.sommarskog.se/dynamic_sql.html

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

LINQ FindAll returning same item twice

I have a table in my database with three columns and two rows:
The columns are: Id, PersonId and ListName
The values in the first row are: 1, 1, 'ListOne'
The values in the second row are: 2, 1, 'ListTwo'
When I try to select these items I'm using LINQ:
var values = dbContext.Table.FindAll().ToList();
The object values has 2 items, just like the table itself. The problem is that it's selecting the first row twice and ignoring the second row, instead of selecting each row just once.
What can I do to retrieve these values from my table correctly?
I've already tried different LINQ methods and they all returned the same thing.
Problem solved! The error was in my model class. I was overriding the Id with the PersonId value.

How to subtract the sum of two columns from different table and insert the difference to the other column

I have three tables STUDENT, STUDENTPAYMENTS, STUDENTCREDIT.
In table STUDENT there is a column TotalPayableAmount, this is the amount needed to be paid by the student.
In table STUDENTPAYMENTS there is a column TotalPaid, this is the amount paid by the students.
And in STUDENTCREDIT there is a column TotalCredit, this is where I put/insert the remaining balance of the student.
Please help me to get this result:
TotalCredit = TotalPayableAmount - TotalPaid
and Group by IDNumber ..
Thanks in advance.
You could create a view many different tables and then use the columns in a view to subtract them from each other. writing a query and subtracting the columns in a single query will be very hard.
Create a view with the columns you need and then use the view to subtract the columns that you require.
How yo create a view
http://www.w3schools.com/sql/sql_view.asp
Subtracting columns in a view
https://drupal.stackexchange.com/questions/181101/subtraction-of-two-column-in-views

SQL - column from all rows returned from a Join into a single column?

I'm writing an SQL query that displays a deliveries details, plus in a seperate column, all the delivery item ID's seperated by commas. Both Deliveries and DeliveryItems are in a seperate table, so I guess I'll be using a Join to retrieve all items that link to that delivery, but how do I get them into a column I'll be displaying, and seperate each item with a comma?
Thanks
EDIT: Here's an idea of my table structure
Deliveries -
ID (PK)
...(other non relevant fields)...
DeliveryItems -
ID (PK)
Delivery (links to ID of delivery)
So for every item of that delivery, I want to display the item ID comma seperated in a column for that delivery.
you can do it by using PIVOT in SQL server 2005.
PIVOT is used for Transpose row to column or column to row, so in your case you can get data in row format by using simple join after that you can use PIVOT for to transpose data and then you can concatenate columns in a single column.
check this link, it has a good example.
There is a GROUP_CONCAT function in MySQL that does just that (take a look at the documentation). Look for something similar in your DBMS.

how to select most frequent items from database?

In the database there are column values which appear in multiple rows. The example rows are the following (in csv format):
AFAB19476C2CEEEE101FFA45FD207BA8B6185B29,539EE0643AFC3A3BE3D20DC6BE7D5376DC536D34,9800,58,29,24,34,2
A801DA9B2F4116A7A1B14A13532B2177C7436C43,91850E4C50536D45C9CEAFE5FB5B3A87154EB754,9800,15,15,15,15,1
4C1E0B5387FB7FE19FC1ED682D0EB08249779180,9B17AE806C79437945F99C054B59A859D5639D11,9800,51,51,51,51,1
5B83A4BE4161497C62471BF133A4E1AD905D25F8,BFF4CED4F54F221A76714B311623398070847B26,9800,71,71,71,71,1
145145E49302ABBEEFF2797CAA8E122FFD3D5BFD,0C287F08E8E11DB4CF10CEB5801EBD61E7664FE4,9800,55,55,55,55,1
99C1F96461BC870574D002034F001BA3F96A9AB5,2EC4F3158764DC07D981008B3054B97809A0B048,Tujina,34,34,34,34,1
**CCB433630C735A8DA1B7828C10820B8CF91F25B9**,2C9C297BEF9CC1C0CF16A0559DE828FA0E226698,9817,339,169,137,202,2
BF2A7F0A9AD762B46A4423F76BF0479B9A72F163,336FB392EA4EF85EFE2563332CDE7D32FCE711B2,9800,34,34,34,34,1
...
**CCB433630C735A8DA1B7828C10820B8CF91F25B9**,C4015FE337F1EEFA1ECE4143D77F9627BEB9D358,9800,464,464,464,464,1
**CCB433630C735A8DA1B7828C10820B8CF91F25B9**,0EC08D78C637EF0A05E858B2BAC85C3EF05DF959,9800,73,73,73,73,1
In this example the value CCB433630C735A8DA1B7828C10820B8CF91F25B9 appears in three rows in combination with different values.
I am looking for a way to count in how many rows the value from the 1st column appears and then order the values by the number of rows that contain that value.
For example only the value from the first row was checked the query would be the following
SELECT COUNT (*) FROM records WHERE column_1 = 'AFAB19476C2CEEEE101FFA45FD207BA8B6185B29'
but instead of just first row the values from all rows have to be checked.
I would be very thankful if anyone of you could suggest an appropriate SQL query statement or a function to sort all the values from the first column by the number of repeatings.
Thank you!
SELECT column_1, COUNT(*)
FROM records
GROUP BY column_1
ORDER BY COUNT(*) DESC
The following will show you the counts of all the values in column1, ordered in ascending order:
select column_1, count(*)
from records
group by column_1
order by 2 asc
Would something like this work?
SELECT column_1, COUNT(column_2)
FROM records
GROUP BY column_1
ORDER BY COUNT(column_2) DESC

Categories

Resources