How to create a result from sql server with a summed column - sql

Hello what I want is to write a query which will fetch me 3 column:-
nvarchar column1
integer Values column2
single cell of the summed column2
is it possible , I am getting the following error:-
Msg 8120, Level 16, State 1, Line 1
Column 'tablename.columnname' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
What is the correct procedure to get data in the format I wish to get.
Edit
Jaques' answer works but I dont get what I want. What I want is:
column 1 PID | column 2 SID | column 3 | column 4 | Column 5(Total of 4)
-------------------------------------------------------------------------
1 | 1 | ABC | 125.00 | 985.00
2 | 2 | XYZ | 420.00 |
3 | 3 | DEF | 230.00 |
4 | 4 | GHI | 210.00 |

i suspect you are using some aggregate function on some columns and not listing your remaining columns in group by clause. your query should look like this.
select sum(column2), column1 from table1
group by column1

You can do it in the following way, because you need to add all non aggregated values in the group by, which makes it difficult
Select column1, column2, SUM(column2) OVER (PARTITION BY column1) as Total from [Table]
This should work.
You can do it with a subselect from your edited answer, but why do you want it like that?
Select Column1, Column2, Column3, Column4, (Select SUM(Column4) from Table) as Column 5 from Table

You must include the same columns in the select and group by clauses.
If you want to sum a column with all the values, you must include in the select clause a column with different value for each row, like this:
SELECT columnId, sum(column4) as total
FROM MyTable
GROUP BY columnId
or simply don't include on the select any extra column, like this:
select sum(column4) from MyTable

Related

How to have a dummy value in a SQL select statement column?

So far I have a column that doesn't need values under the field, so I did: SELECT NULL AS Column1.
However, how do I query a column to actually have dummy values under it, say 'X' in all the rows for that column?
ex:
ID | Column2
1 | x
2 | x
3 | x
4 | x
The same way, just provide a value:
select id, 'X' as column2
from t;

SQL code to get next variable in table with different value

I need to find a way in SQL Server 2014 Management Studios to find the next unique value in a column that shares the value of a different column.
So for example below I would want my results to be
Column 1 - A
Column 2 - 1
Column 3 - 4
As that is the first time that A has unique values in column 2 and 3
Column1 | Column2 | Column3
---------+---------+---------
| A | X | 1 |
| A | X | 2 |
| B | Y | 3 |
| A | Z | 4 |
Query:
SELECT
Column1,
LEAD(Column3) OVER (PARTITION BY Column2 ORDER BY Column3) AS FindValue
FROM
Table
If I understand it correctly I would try something like this:
-- first we find minimum values for column1, column2 variations
WITH min_values AS (
SELECT
column1,
column2,
min(column3) AS min_value
FROM
table
GROUP BY 1,2
)
-- then we find bottom 2 values for column1
,bottom_2 AS (
SELECT
column1,
min_value,
row_number() OVER (PARTITION BY column1 ORDER BY min_value ASC) AS rn
FROM
min_values
)
-- THEN we JOIN results INTO single record
SELECT
b1.column1, b2.min_value, b1.min_value
FROM
bottom_2 b1
JOIN
bottom_2 b2 ON b1.column1 = b2.column1 AND b2.rn < b1.rn
WHERE b1.rn <= 2
I just checked comments above and would like to add some notes.
If you want to find next value ordered by column2 then you have to change order by from min_value to column2 in row_number() line. Otherwise, if you are looking for next inserted value then you need a timestamp or some kind of id.

Move All null columns (Columns with zero non-null values) to the extreme right of the table

I have a table with 100's of columns. For few of the columns, the value is always null. I want to move all such columns (columns with zero non-null values) to the extreme right so that when my users see the table, they will get to see the usable information first.
Eg : test_table
**column1 | column2 | column3 | column4**
a | null | null | 1
b | null | null | 2
c | null | null | 3
After insertion, I want to make the test_table as like the below table
**column1 | column4 | column2 | column3**
a | 1 | null | null
b | 2 | null | null
c | 3 | null | null
The order the columns show up is defined when you query the table.
If you want these columns to show at the right end of the result set, you need to re-order them within the SELECT portion of your query.
So to stick to your example, instead of having:
Select column1, column2, column3, column4 FROM test_table
You would have:
Select column1, column4, column2, column3 FROM test_table
I do not believe there's a way to do this dynamically, but since you know these columns will always have a null value you should feel pretty confident with this.
use isnull and case statment
select distinct isnull(id,0) from Employee
select distinct case when id is null then 0 else id end as id from Employee

SQL query to give distinct values from one column, and a count of distinct values from a second column

Say I have a table like this:
column1 | column2
---------------------
1 | a
1 | b
1 | c
2 | a
2 | b
I need an SQL query to show the distinct values from column 1, and a count of the related distinct values from column 2. The output would look like:
column1 | count
-------------------
1 | 3
2 | 2
You could do something like this:
SELECT column1, count(column2)
FROM table
GROUP BY column1
You should do a COUNT(DISTINCT ...) with a GROUP BY:
Select Column1,
Count(Distinct Column2) As Count
From Table
Group By Column1

SQL select values of each selected columns on separate rows

I have a table with hundreds of rows and tens of W columns:
Column1 | Column2_W | Column3_W | ColumnX_W
123 | A | B | x
223 | A | NULL | NULL
How can i select it so that the output would be:
Column1 | W
123 | A
123 | B
123 | x
223 | A
EDIT: I am well aware that i am working with a terrible DB "design". Unfortunately i can't change it. This question is actually part of a larger problem i was handed today. I will try the given ideas tomorrow
SELECT Column1, Column2_W
FROM table
UNION ALL
SELECT Column1, Column3_W
FROM table
UNION ALL
SELECT Column1, Column4_W
FROM table
....
ORDER BY Column1
Better option: redesign your database! This looks like a spreadsheet, not a relational database.
Take a look at this article: UNPIVOT: Normalizing data on the fly
Unforunately, you're going to be stuck hand typing the column names with any of the solutions you use. Here's a sample using UNPIVOT in SQL Server...
SELECT Column1, W
FROM YourTable
UNPIVOT (W for Column1 in (Column2_W, Column3_W /* and so on */)) AS W
If you don't have any UNPIVOT options...
SELECT
Column1,
CASE ColumnID WHEN 2 THEN Column2_W
WHEN 3 THEN Column3_W
...
WHEN X THEN ColumnX_W
END AS Column2
FROM
yourTable
CROSS JOIN
( SELECT 2 AS ColumnID
UNION ALL SELECT 3
...
UNION ALL SELECT X
)
AS UnPivot