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;
Related
I have a table that looks like this. And I want to get the distinct count horizontally across the three columns ignoring nulls.
ID
Column1
Column 2
Column 3
1
A
B
C
2
A
A
B
3
A
A
The desired output I'm looking for is:
ID
Column1
Column 2
Column 3
unique_count
1
A
B
C
3
2
A
A
B
2
3
A
A
1
One possible option would be
WITH sample AS (
SELECT 'A' Column1, 'B' Column2, 'C' Column3 UNION ALL
SELECT 'A', 'A', 'B' UNION ALL
SELECT 'A', 'A', NULL UNION ALL
SELECT '', 'A', NULL
)
SELECT Column1, Column2, Column3, COUNT(DISTINCT NULLIF(TRIM(c), '')) unique_count
FROM (SELECT *, ROW_NUMBER() OVER () rn FROM sample) t LATERAL VIEW EXPLODE(ARRAY(Column1, Column2, Column3)) tf AS c
GROUP BY Column1, Column2, Column3, rn;
output
+---------+---------+---------+--------------+
| column1 | column2 | column3 | unique_count |
+---------+---------+---------+--------------+
| | A | NULL | 1 |
| A | A | NULL | 1 |
| A | A | B | 2 |
| A | B | C | 3 |
+---------+---------+---------+--------------+
case when C1 not in (C2, C3) then 1 else 0 end +
case when C2 not in (C3) then 1 else 0 end + 1
This will not work if you intend to count nulls. The pattern would extend to more columns by successively comparing each one to all columns to its right. The order doesn't strictly matter. There's just no point in repeating the same test over and over.
If the values were alphabetically ordered then you could test only adjacent pairs to look for differences. While that applies to your limited sample it would not be the most general case.
Using a column pivot with a distinct count aggregate is likely to be a lot less efficient, less portable, and a lot less adaptable to a broad range of queries.
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.
I'm trying to create an sqlite query but I'm having some problems.
Let's say that table has three columns id, foreign-id and value.
I need to select all rows with distinct foreign_id with a given value, however that value may not exist for all different foreign_ids.
In which case a row where value is set to some fallback value must be selected (such row always exists) for that foreign_id.
I apologize for my english since I'm not native english speaker.
Here is an example:
Table:
id | foreign_id | value
------------------------
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 2 | 1
5 | 2 | 3
If desired value is 2 and fallback value is 1 then the query should return
id | foreign_id | value|
------------------------
2 | 1 | 2
4 | 2 | 1
It return row with id 1 because it has desired value 2 for foreign_id 1.
And it return row with id 4 because for foreign_id 2 a row with value of 2 does not exits, so it selects a row with fallback value of 1.
Hope that clears up my question a bit.
You might be able to do it with a Union... something like:
SELECT DISTINCT (foreign_id), value
FROM TABLE
WHERE value = 2
UNION
SELECT DISTINCT (foreign_id), '1' as value
FROM TABLE
WHERE foreign_id NOT IN (
SELECT DISTINCT (foreign_id), value
FROM TABLE
WHERE value = 2
)
where everything that has a value 2 set value as 2 and everything else sets value as 1
(I haven't tested this query, you might have to do some tweaking)
This is the solution that I produced based on Seth's answer.
SELECT DISTINCT (foreign_id), value, id FROM testTable
WHERE value = 2
UNION SELECT DISTINCT (foreign_id), value, id FROM testTable
WHERE value = 1
AND foreign_id NOT IN
(
SELECT foreign_id
FROM testTable
WHERE value = 2
)
I have a table T, say
1 | a
2 | a
I want to duplicate its rows while changing the value of the second column to b, so as to have
1 | a
2 | a
1 | b
2 | b
I came to
INSERT INTO T(col1, col2)
SELECT col1, 'b'
FROM T
but I get an error
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Remove those extra parentheses in the SELECT :
INSERT INTO T(col1, col2)
SELECT col1, 'b' AS col2 FROM T;
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