Avoid duplicate columns select on nested select - sql

In CrateDB is there a way to avoid to re-select the same column in nested SELECT statement, to show the value in the results?
e.i. in the following query, is there any way to avoid re-selecting A and B through the nested SELECT? Ideally would be nice to select just once in the first
SELECT
A,
B,
AB,
A * B * AB AS ABAB,
A / AB::DECIMAL AS AAB,
FROM (
SELECT
A,
B,
(A + B) AS AB,
FROM (
SELECT
(SELECT count(*) FROM schema.table_01 WHERE process_state IN ('State_1')) AS A,
(SELECT count(*) FROM schema.table_01 WHERE process_state IN ('State_2')) AS B,
) alias_for_subquery_01
) alias_for_subquery_02
Thanks

Related

Update column as Duplicate

I have a table with three columns, A, B, and status.
first, I filter the table to get only duplicate value
using this query
SELECT A
FROM Table_1
GROUP BY A
HAVING COUNT(A) >1
the output :
In the second step, I need to check if column B has a duplicate value or not, if have duplicate I need to update the status as D.
I try this query
UPDATE Table_1
SET status = 'D'
WHERE exists
(SELECT B
FROM Table_1
GROUP BY B
HAVING COUNT(B) >1)
but it is updated all the rows.
The following does what you need using row_number to identify any group with a duplicate and an updateable CTE to check for any row that's part of a group with a duplicate:
with d as (
select *, row_number() over(partition by a,b order by a,b) dn
from t
)
update d set d.status='D'
where exists (select * from d d2 where d2.a=d.a and d2.b=d.b and d2.dn>1)
You can do this with an updatable CTE without any further joins by using a windowed COUNT
WITH d AS (
SELECT *,
cnt = COUNT(*) OVER (PARTITION BY a, b)
FROM t
)
UPDATE d
SET status = 'D'
WHERE cnt > 1;

Which select is faster in Sqlite?

a is a table index, b is a normal column.
select a,b from ( select a,b from table where a in (*listA*) ) where b in (*listB*)
or
select a,b from table where (a=listA[0] and b=listB[0]) or (a=listA[1] and b=listB[1])...
I am using pseudocode to represent a list declaration.
The first query is wrong because it never looks at the combinations of a and b.
To use a temp table, you have to join with it:
SELECT a, b
FROM MyTable
JOIN (SELECT 1 AS a, 2 AS b UNION ALL
SELECT 3, 4 UNION ALL
SELECT 5, 6 ...)
USING (a, b)
Which version is better optimized depends on too many factors; the only way to find out is to measure with representative data.

How to select duplicate records without a primary key in SQL Server

If I run this query:
SELECT
a,
b,
c,
...
FROM [DMS].[dbo].[CreditDebitAdjustment]
I get 24197 records.
If I run this query:
SELECT DISTINCT
a,
b,
c,
...
FROM [DMS].[dbo].[CreditDebitAdjustment]
I get 24176 records.
How do I go about selecting only the rows that are identical?
SELECT
a,
b,
c
FROM [DMS].[dbo].[CreditDebitAdjustment]
group by a,b,c
having count(*) > 1
If you want to delete those duplicates, use
;WITH CTE AS
(
SELECT
a, b, c,
RowNum = ROW_NUMBER() OVER(PARTITION BY a,b,c ORDER BY ...(define how to order those rows)..)
FROM
[DMS].[dbo].[CreditDebitAdjustment]
)
DELETE FROM CTE
WHERE RowNum > 1
This "partitions" (groups) all your data by the tuple (a,b,c) and gives each row a number - starting at 1 for each new tuple.
So any cases where you have a RowNum that's larger than 1 - that's a duplicate, and I delete it away.
But really: any serious data table ought to have a proper primary key!

Select Sum and multiple columns in 1 select statement

Is there a way to select the sum of a column and other columns at the same time in SQL?
Example:
SELECT sum(a) as car,b,c FROM toys
How about:
select sum(a) over(), b, c from toy;
or, if it's required:
select sum(a) over(partition by b), b, c from toy;
try add GROUP BY
SELECT sum(a) as car,b,c FROM toys
GROUP BY b, c
Since you do not give much context, I assume you mean one of the following :
SELECT (SELECT SUM(a) FROM Toys) as 'car', b, c FROM Toys;
or
SELECT SUM(a) as Car, b, c FROM Toys GROUP BY b, c;
SELECT b,
c,
(SELECT sum(a) FROM toys) as 'car'
FROM toys

SELECT SUM as field

Suppose i have this table
table (a,b,c,d). Datatypes are not important.
I want to do this
select a as a1,b as b1,c as c1,
(select sum(d) from table where a=a1 and b=b1) as total
from table
group by a,b,c
...but I can't find a way (sqldeveloper keeps complaining with "from clause not found".)
Is there a way? Is it possible?
SELECT a as a1,b as b1,c as c1,
(
SELECT SUM(d)
FROM mytable mi
WHERE mi.a = mo.a
AND mi.b= mo.b
) as total
FROM mytable mo
GROUP BY
a, b, c
It's much more simple and efficient to rewrite it as this:
SELECT a AS a1, B AS b1, c AS c1, SUM(SUM(d)) OVER (PARTITION BY a, b) AS total
FROM mytable
GROUP BY
a, b, c
Note the SUM(SUM(d)) here.
The innermost SUM is the aggregate function. It calculates the SUM(d) a-b-c-wise.
The outermost SUM is the analytic function. It sums the precalculated SUM(d)'s a-b-wise, and returns the value along with each row.
Du you mean something like this?
select a as a1,
b as b1,
c as c1,
sum(sum(d)) OVER (PARTITION BY a, b) AS total
from table
group by a,b,c
you can do it with aliases:
SELECT a AS a1, b AS b1, c AS c1,
(SELECT SUM(d)
FROM test_t t_in
WHERE t_in.a = t.a
AND t_in.b = t.b) AS total
FROM test_t t
GROUP BY a, b, c