I have a table in my database with two columns as int for example cal1 and cal2.
I make the sum from each row as in select ( cal1 + cal2) from cat as total, now I want to do the sum from all columns total if is possible.
You can do the addition in the SUM :
SELECT SUM(cal1+cal2) AS total
FROM cat
SELECT (SUM(cal1) + SUM(cal2)) AS TotalSum FROM cat
Thats to sum the values of all rows together. If you want to sum all columns up, you have to specifically write their names into the column list.
You want 2 totals in one go?
SELECT
cal1+cal2 AS PerRowTotal,
SUM(cal1+cal2) OVER () AS AllRowTotal
FROM
cat
Related
I have a table as below
id pausetime resumetime
-----------------------------------------------------
1 2021-09-22 14:21:34.023 2021-09-22 14:25:37.030
2 2021-09-22 14:37:47.810 2021-09-22 14:40:58.817
I want to subtract resumetime from pausetime for all the rows, then add that time so that I can get the total time between resume and pause, number of rows can be different .
How can I do that ?
I tried this
select
dbo.TimeDifference(cast(resumetime as time),
cast(pausetime as time)
but I have to do it for all the rows.
Please help me out.
We can use DATEDIFF here along with SUM:
SELECT SUM(DATEDIFF(ss, pausetime, resumetime)) AS total_diff_in_seconds
I have below requirement.
Input is like as below.
Create table Numbers
(
Num int
)
Insert into Numbers
values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15)
Create table FromTo
(
FromNum int
,ToNum int
)
Select * From FromTo
Output should be as below.
FromNum ToNum
1 5
6 10
11 15
Actual Requirement is as below.
I need to load the data for a column into a table which will have thousands of records with different no's.
Consider like below.
1,2,5,7,9,11,15,34,56,78,98,123,453,765 etc..
I need to load these into other table which is having FROM and TO columns with the intervals of 5000. For example in the first 5000 if i have the no's till 3000, my 1st row should have FromNo as 1 and ToNum as 3000. second row: if the data is not having till 10000 and the next no started as 12312(This is the 2nd Row FromNum) the ToNum value should be +5000 i.e 17312. Here also if we don't have the no's data till 17312 it need to consider the ToNum between the 12312 and 17312
Output should be as below.
FromNum ToNum
1 3205
1095806 1100805
1100808 1105806
1105822 1110820
Can you guys please help me with the solution for the above.
Thanks in advance.
What you may try in this situation is to group data and get the expected results:
DECLARE #interval int = 5
INSERT INTO FromTo (FromNum, ToNum)
SELECT MIN(Num) AS FromNum, MAX(Num) AS ToNum
FROM Numbers
GROUP BY (Num - 1) / #interval
I have a stored procedure which selects columns from multiple tables.
what i want is to be able to get the number of rows with values Y and number or row if value N that are in the returned records.
Example
name availableYN
john y
gary n
mark y
I want to be able to have that result and also a count like
y 2
n 1
any guidance will be much appreciated.
Try using COUNT
SELECT
availableYN,
COUNT(availableYN) AS cnt
FROM
My_Table
GROUP BY
availableYN
I'm new to SQL Server so I apologize if my question seems too easy. I tried finding and answer on my own, but I'm failing miserably. I am trying to create a query which will return total size on the drive of each row in the table.
i thought about using dbcc showconting but it doesn't work for varchar(max) which appears in my table. Also, it doesn't return size for each row, but rather the average, max and min size. My reading so far suggests that it is not possible to get query that could show the size of each individual row in the table so I decided to settle for the total length of all characters in each column in each row. Indirectly it will give me idea about the size of each row.
I have a table with some varchar(500) and varchar(max) columns. I noticed that some of the rows are a lot bigger than others.
What I need is top 1000 longest rows in the table, preferably in an output showing two columns:
Column 1 showing EntryID
Column 2 showing total length of the characters in all columns together for that record (eg total length of the characters in the column 1 + total length of the characters in the column 2 + column3 + column4 etc...) It would be great if this could be aliased RowLength.
What I tried so far is:
SELECT TOP 1000
(LEN(columnname1) + LEN(columnname2) + LEN(columnname3) + LEN(columnname4)) as RowLength,
FROM dbo.tablename
ORDER BY Length Desc
It works, but it doesn't show entry ID corresponding to the total length of all characters in the row. How do I add it?
It also doesn't show the alias for the column showing number of characters in the row.
Could you please suggest how I can change the query to get the expected outcome? I'll be very grateful for any suggestions.
it doesn't show EntryID corresponding to the total length of all
characters in the row. It also doesn't show the alias for the column
showing number of characters in the row.
You have not specified an alias, so what should it show? You also haven't selected EntryID. If you want the longest 1000 rows you have to order by the length:
SELECT TOP 1000
EntryID,
Length = LEN(columnname1) + LEN(columnname2) + LEN(columnname3) + LEN(columnname4)
FROM dbo.tablename
ORDER BY Length DESC
SELECT TOP 1000 EntryID,
(LEN(columnname1) + LEN(columnname2) + LEN(columnname3) + LEN(columnname4)) AS RowLength,
FROM dbo.tablename
ORDER BY EntryID
I want to generate a random number against StudentID, I am using the following SQL
The Result i am getting is :
Please Help.
You're just setting that column to the column index of the partitioned row_number, which is 1. If you want a random number for each row, it would be easier to do this:
UPDATE TAB_EXAM_FORMS
SET RNO_CODE = CAST(RAND(CHECKSUM(NEWID())) * 10 AS INT) + 1
Just change the multiplication to whatever range you want. This range is 1-10.