I'm having the hard time creating a VIEW with the results of the SELECT statement from my CTE. The following is my syntax:
CREATE VIEW fooView AS
;WITH CTE AS (
SELECT a, b, c, ROW_NUMBER() OVER (PARTITION BY a, b, c ORDER BY a) AS RN
FROM foo)
SELECT * FROM CTE WHERE RN > 1
I have also tried this which also doesn't work:
;WITH CTE AS (
SELECT a, b, c, ROW_NUMBER() OVER (PARTTION BY a, b, c ORDER BY a) AS RN
FROM foo)
CREATE VIEW fooView AS
SELECT * FROM CTE WHERE RN > 1
Anybody want to help me out with this?
Related
i have a column in a Snowflake table that returns arrays.
Eg-
['A','U']
['A','P','U']
['A','P']
['P','U']
['M','S']
['S']
i need to remove the subsets and just take the supersets, so only
['A','P','U']
['M','S']
is there an easy way to do this?
not the best solution but if you can't find better try something like this:
SELECT *
FROM (
SELECT a, b, c,
ROW_NUMBER() OVER (PARTITION BY a ORDER BY b) AS row_num
FROM (
SELECT a, b, c,
ROW_NUMBER() OVER (PARTITION BY a ORDER BY c) AS row_num
FROM (
SELECT a, b, c,
ROW_NUMBER() OVER (PARTITION BY c ORDER BY b) AS row_num
FROM (
SELECT a, b, c,
ROW_NUMBER() OVER (PARTITION BY b ORDER BY b) AS row_num
from supertset
) where row_num = 1
)
)
)
QUALIFY row_num = 1
for reference:
https://docs.snowflake.com/en/sql-reference/constructs/qualify.html
For each array we are going to look what array contains it, by looking with a cross join for one with the largest intersection, and then choose the one that's the largest between those:
select distinct x
from (
select b.x
from data a
cross join data b
qualify 1 = row_number() over(
partition by a.id
order by array_size(array_intersection(a.x, b.x)) desc
, array_size(b.x) desc
)
)
This might not work with a different sample dataset, but the rules that the question includes are not enough to determine the right solution in other situations.
Data setup:
with data as (
select row_number() over(order by 1) id, parse_json(value) x
from table(split_to_table($$['A','U']
['A','P','U']
['A','P']
['P','U']
['M','S']
['S']$$, '\n'))
)
I want to calculate the partial percentage for each SiteName here but I would
need to calculate the aggregate count of my rows.
The following query works but is there a way to do this without using the SELECT
within the SELECT statement or declaring a variable for this? I only have read access so
I can't declare variables.
SELECT
ServiceSiteName
, COUNT(*) AS [Alarms Resolved]
-- How can I avoid this.
, (SELECT COUNT(1) FROM [C1Datastore].[dbo].[Fct_AlertCRM_Incident]
WHERE Conditions A, B, C) AS [Total Count]
, COUNT(*) / (SELECT COUNT(1) FROM [C1Datastore].[dbo].[Fct_AlertCRM_Incident]
WHERE Conditions A, B, C) AS [% Count]
FROM TableX
WHERE Conditions A, B, C
GROUP BY
ServiceSiteName
ORDER BY [Alarms Resolved] DESC
The key is to use SUM(COUNT()) OVER (PARTITION BY) and this is much shorter.
SELECT
ServiceSiteName
, COUNT(*) AS [Alarms Resolved]
-- Use this
, SUM(COUNT(1)) OVER (PARTITION BY ServiceSiteName) AS [TOTAL]
, COUNT(*) / SUM(COUNT(1)) OVER (PARTITION BY ServiceSiteName) AS [% Count]
FROM TableX
WHERE Conditions A, B, C
GROUP BY
ServiceSiteName
ORDER BY [Alarms Resolved] DESC
Window function can not be used in where clause. In the below query rn column can be used in where clause only in the outer query like -
select * from (
select *, row_number() over (partition by col1 order by col1) as rn
from tab1) as abc
where rn = 1
can we use rn column with cross join some thing like -
select *, row_number() over (partition by col1 order by col1) as rn
from tab1 as a
cross join(select rn from a)
Just curious to know as i am trying to learn SQL server
You cannot do it in this way. You either have to repeat ROW_NUMBER() inside the subquery:
SELECT *, ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col1) AS rn
FROM tab1 AS a
CROSS JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col1) AS rn FROM tab1) t
or, alternatively, use a common table expression:
;WITH a AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col1) AS rn
FROM tab1
)
SELECT *
FROM a
CROSS JOIN (SELECT rn FROM a) t
Getting syntax error while executing the following query?
select *
from (select row_number() over (order by title) as RowNum from question)
where RowNum>5
Anyone knows how to get it fixed?
Give ALIAS to sub-query like this:
SELECT *
FROM (SELECT row_number() OVER (ORDER BY title) AS RowNum FROM question) AS t1
WHERE RowNum>5
It would help if you selected the COLUMNS from the TABLE in the subquery, no?
select *
from (
select *, row_number() over (order by title) as RowNum
from question) SQ
where RowNum > 5
;With CTE AS
(
select row_number() over (order by title) as RowNum from question
)
select *
From Cte
Where RowNum > 5
OR
Select * From
(select row_number() over (order by title) as RowNum from question)X
Where X.RowNum > 5
I would like to create a select query statement with autonumber.. like..
select * from tbl1
will give me everything from table.
The result I'd like to get is..
1 data
2 data
3 data
So how can I do to get that number..??
like..
select (for autonumber), * from tbl1
the data in my table will repeated (no unique data)
Use ROW_NUMBER:
SELECT ROW_NUMBER() OVER (ORDER BY col1) AS rn, * FROM tbl1
To filter the results based on the row number use this:
SELECT * FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY col1) AS rn, * FROM tbl1
) T1
WHERE rn = 5
You may need to find the identity's offset e.g. last ID of second table:
DECLARE #lastAutoID int
SET #lastAutoID = abs(( Select max(convert(float,[ConsID]))
FROM [RXPIPEDB]...[consumption] ) )
Then use ROW_NUMBER():
#lastAutoID + ROW_NUMBER() OVER (ORDER BY oldICN_str)