Related
I want to define variables before a CTE table and after a CTE table because some variables are dependent on the result of the CTE table. For example
SET(K,B) = (5,2);
with my_data(Key,Index,Value) as (
-- data table as cte
select * from values
(1, 3, 10),
(1, 5, 18),
(1, 14, 4),
(2, 2, 11),
(2, 13, 24),
(2, 29, 40)
)
SELECT VALUE + $K
FROM my_data
This examples works perfectly. But this code:
SET(K,B) = (5,2);
with my_data(Key,Index,Value) as (
-- data table as cte
select * from values
(1, 3, 10 ),
(1, 5, 18 ),
(1, 14, 4 ),
(2, 2, 11 ),
(2, 13, 24),
(2, 29, 40)
)
SET AVG_VAL = (SELECT AVG(VALUE) FROM my_data);
SELECT VALUE + $AVG_VAL
FROM my_data
doesn't because snowflake gives me this error
"SQL compilation error: syntax error line 34 at position 0 unexpected 'SET'."
Should I create a temporary table to store the result of this query (SELECT AVG(VALUE) FROM my_data) in it and then include/use this temporary table for future queries instead of a variable?
Your "CTE" is not a standalone "thing" it only exist in the context of a SELECT.
Thus
WITH cte_x AS (...)
SELECT * FROM cte_x
is one SELECT which has a CTE attached to it.
Thus for you variable assignment the CTE has to be "IN" the paren's
with my_data(Key,Index,Value) as (
select * from values
(1, 3, 10 ),
(1, 5, 18 ),
(1, 14, 4 ),
(2, 2, 11 ),
(2, 13, 24),
(2, 29, 40)
)
SELECT AVG(VALUE) FROM my_data;
AVG(VALUE)
17.833333
given that is a discrete chunk of SQL, that can be captured into the variable:
set AVG_VAL = (
with my_data(Key,Index,Value) as (
select * from values
(1, 3, 10 ),
(1, 5, 18 ),
(1, 14, 4 ),
(2, 2, 11 ),
(2, 13, 24),
(2, 29, 40)
)
SELECT AVG(VALUE) FROM my_data
);
status
Statement executed successfully.
now we can use that value:
select $AVG_VAL * 2;
$AVG_VAL * 2
35.666666
But the next query:
SELECT VALUE + $AVG_VAL
FROM my_data
002003 (42S02): SQL compilation error:
Object 'MY_DATA' does not exist or not authorized.
has no CTE called my_data, so that need to be insert:
with my_data(Key,Index,Value) as (
select * from values
(1, 3, 10 ),
(1, 5, 18 ),
(1, 14, 4 ),
(2, 2, 11 ),
(2, 13, 24),
(2, 29, 40)
)
SELECT VALUE + $AVG_VAL
FROM my_data
If you want a table that can be "used twice" you will need an actual table, at which point I would suggest a temporary table so it only have context in this session.
Which the nature of Pankaj's answer (ether via a permanent or temp table)
This can be done as in -
select * from d2;
+-----+-----+
| ID1 | ID2 |
|-----+-----|
| 1 | 2 |
| 100 | 2 |
| 3 | 4 |
| 300 | 4 |
+-----+-----+
Setting variable -
set (var1) = (select sum(id2) from d2);
+----------------------------------+
| status |
|----------------------------------|
| Statement executed successfully. |
+----------------------------------+
Using variable -
select id1+$var1 from d2;
+-----------+
| ID1+$VAR1 |
|-----------|
| 13 |
| 112 |
| 15 |
| 312 |
+-----------+
An alternatvie approach is to simply use windowed AVG function:
with my_data(Key,Index,Value) as (
-- data table as cte
select * from values
(1, 3, 10),
(1, 5, 18),
(1, 14, 4),
(2, 2, 11),
(2, 13, 24),
(2, 29, 40)
)
SELECT VALUE, AVG(VALUE) OVER(),
VALUE + AVG(VALUE) OVER()
FROM my_data;
Output:
OVER() means that the window used to compute average spans over all rows.
I have made a little example data that I modify in three steps. I cant do it in one, maybe there is a clever way with some logic? I use Microsoft SQL Server
This code will generate the four base tables with example data and the step by step queries I want to combine, the result at the end should have 8 entries:
Reference table:
CREATE TABLE ref
(
ID int NOT NULL
NR int NOT NULL
CONSTRAINT KEYS PRIMARY KEY (ID, NR)
);
INSERT INTO ref
VALUES (1234, 223), (1234, 224), (1234, 225),
(1235, 123), (1235, 124), (1236, 540),
(1236, 541), (1237, 233), (1237, 234);
Con1 table:
CREATE TABLE con1
(
NR int NOT NULL
flag int NOT NULL
PRIMARY KEY (NR)
);
INSERT INTO con1
VALUES (123, 0), (124, 1), (125, 0),
(220, 0), (222, 0), (223, 0),
(224, 0), (225, 1), (300, 0),
(540, 1), (541, 1);
Con2 table:
CREATE TABLE con2
(
NR int NOT NULL
ID int NOT NULL
PRIMARY KEY (NR)
);
INSERT INTO con2
VALUES (123, 1235), (124, 1235), (125, 1243),
(220, 1296), (222, 1255), (223, 1234),
(224, 1234), (225, 1234), (300, 1267),
(540, 1236);
Info table:
CREATE TABLE info
(
NR int NOT NULL
SNR int NOT NULL
SSNR int NOT NULL
Level int not NULL
CONSTRAINT KEYS PRIMARY KEY (NR, SNR, SSNR)
);
INSERT INTO info
VALUES (123, 1, 1, 1), (123, 1, 2, 2),
(123, 1, 3, 2), (123, 2, 1, 1),
(123, 2, 2, 2), (123, 2, 3, 2),
(124, 1, 1, 1), (124, 1, 2, 2),
(124, 1, 3, 2), (125, 1, 1, 1),
(125, 1, 2, 2), (125, 1, 3, 2),
(125, 1, 4, 3), (125, 1, 5, 3),
(220, 1, 1, 1), (220, 1, 2, 2),
(223, 1, 1, 1), (223, 1, 2, 2),
(224, 1, 1, 1), (224, 1, 2, 2),
(224, 1, 3, 2), (225, 1, 1, 1),
(225, 1, 2, 2), (300, 1, 1, 1),
(300, 1, 2, 2), (300, 2, 1, 1),
(300, 2, 2, 2), (540, 1, 1, 1),
(541, 1, 1, 1);
Step #1:
SELECT *
FROM con1
INNER JOIN con2 ON con1.NR = con2.NR
WHERE con1.flag = 1
Step #2:
SELECT ref.*
FROM ref
INNER JOIN step1 ON ref.ID = step1.ID
Step #3:
SELECT *
FROM step2
INNER JOIN info ON step2.NR = info.NR
WHERE info.Level = 1
I tried some different ways but always get too much resulting rows
the result should look like this:
ID
NR
Level
SNR
SSNR
1234
223
1
1
1
1234
224
1
1
1
1234
225
1
1
1
1235
123
1
1
1
1235
123
1
2
1
1235
124
1
1
1
1236
540
1
1
1
1236
541
1
1
1
It should be all entries from info with Level=1
excluding:
all NR that do not occur in the intersection of con1 and con2
all NR that con1 lists with flag = 0
but including:
all excluded NR that run with the same ID (according to ref) as any NR not excluded prior
the result has the same columns as info with on NR matching IDs from ref
You can do this easily with Common Table Expressions:
with step1 As (
Select *
From con1
Inner Join con2 On con1.NR = con2.NR
Where con1.flag = 1
), step2 As (
Select ref.*
From ref
Inner Join step1 On ref.ID = step1.ID
)
Select *
From step2
Inner Join info On step2.NR = info.NR
Where info.Level = 1
First, I apologize if the title won't make sense but below is the detailed scenario.
Say I have a document_revision table
id document_id phase_id user_id
1 1 3 1
2 1 2 1
3 1 1 1
4 2 3 2
5 2 2 2
where phase_id is: transcribe = 3; proof = 2; and submit = 1.
I would like to write a query where I can filter the revision records where I will disregard a proof phase if the same user did the transcribe and proof. So the output would be:
id document_id phase_id user_id
1 1 3 1
3 1 1 1
4 2 3 2
I've been struggling for hours figuring out a query for this but no luck so far.
Assuming you only want the phase 3 for any case where a user_id was involved in phase 2 and 3, then one way you could do this is with ROW_NUMBER(), e.g.:
DECLARE #T TABLE (ID INT IDENTITY(1, 1), Document_ID INT, Phase_ID INT, [User_ID] INT);
INSERT #T (Document_ID, Phase_ID, [User_ID]) VALUES
(1, 1, 1), (1, 2, 1), (1, 3, 1), (2, 3, 2), (2, 2, 2), (3, 1, 1), (3, 2, 1), (3, 3, 2);
SELECT ID, Document_ID, Phase_ID, [User_ID]
FROM
(
SELECT *, RN = ROW_NUMBER() OVER (PARTITION BY Document_ID, [User_ID], CASE WHEN Phase_ID IN (2, 3) THEN 2 ELSE Phase_ID END ORDER BY Phase_ID DESC)
FROM #T
) AS T
WHERE RN = 1;
DECLARE #document_revision TABLE (
id INT IDENTITY(1,1),
document_id INT,
phase_id INT,
user_id INT
);
INSERT INTO #document_revision
(document_id, phase_id, user_id)
VALUES
(1, 3, 1),
(1, 2, 1),
(1, 1, 1),
(2, 3, 2),
(2, 2, 2),
-- To test a scenario where there is a proof and a submit with no transcribe phases and same document
(3, 2, 3),
(3, 1, 3),
-- To test a scenario where there is a transcribe and a submit with no proof phases and same document
(4, 3, 4),
(4, 1, 4),
-- To test a scenario where there is a proof and a submit with no transcribe phase (for document_id 5) but different document and same user as above
(5, 2, 4);
SELECT dr.id
, dr.document_id
, dr.phase_id
, dr.user_id
FROM #document_revision AS dr
WHERE NOT EXISTS ( SELECT 1
FROM #document_revision AS temp
-- Same user
WHERE temp.user_id = dr.user_id
-- Same document
AND temp.document_id = dr.document_id
-- To check if there is already a transcribe phase_id with the same user_id and document_id
AND temp.phase_id = 3
-- -- To check if there is already a proof phase_id with the same user_id and document_id
AND dr.phase_id = 2 )
results:
id document_id phase_id user_id
1 1 3 1
3 1 1 1
4 2 3 2
6 3 2 3
7 3 1 3
8 4 3 4
9 4 1 4
10 5 2 4
I would like to have a query that based on the field "Periodicity", would repeat every N rows the value True, grouped by the field "Type".
I think I can explain better with an example:
By having the next table...
id type periodicity
1 1 3
2 1 3
3 1 3
4 1 3
5 1 3
6 1 3
7 1 3
8 1 3
9 1 3
10 2 2
11 2 2
12 2 2
13 2 2
14 2 2
15 2 2
16 2 2
... I would like to have a query that would return something like this...
id type periodicity execute_operation
1 1 3 TRUE
2 1 3 FALSE
3 1 3 FALSE
4 1 3 TRUE
5 1 3 FALSE
6 1 3 FALSE
7 1 3 TRUE
8 1 3 FALSE
9 1 3 FALSE
10 2 2 TRUE
11 2 2 FALSE
12 2 2 TRUE
13 2 2 FALSE
14 2 2 TRUE
15 2 2 FALSE
16 2 2 TRUE
For the rows where the field "type" is 1, the field "execute_operation" would be marked as True every 3 rows (the value defined in the field "periodicity").
For the rows having the "type"is 2, the filed "execute_operation" would be marked as True every 2 rows.
How can I achieve this?
You could combine the windowed function ROW_NUMBER with the modulo operator.
Row number sequences each group (1, 2, 3, ...). Modulo returns the position within the sequence by calculating the remainder. Example:
1 Mod 3 = 1.
2 Mod 3 = 2.
3 Mod 3 = 0.
When the mod of row number over periodicity is 1 then true is returned.
WITH [Sample] AS
(
-- CTE provides sample data.
SELECT
*
FROM
(
VALUES
(1, 1, 3),
(2, 1, 3),
(3, 1, 3),
(4, 1, 3),
(5, 1, 3),
(6, 1, 3),
(7, 1, 3),
(8, 1, 3),
(9, 1, 3),
(10, 2, 2),
(11, 2, 2),
(12, 2, 2),
(13, 2, 2),
(14, 2, 2),
(15, 2, 2),
(16, 2, 2)
) AS x(id, [type], periodicity)
)
SELECT
s.id,
s.[type],
s.periodicity,
CASE ROW_NUMBER() OVER (PARTITION BY s.[type] ORDER BY s.id) % periodicity
WHEN 1 THEN 1
ELSE 0
END AS execute_operation
FROM
[Sample] AS s
ORDER BY
s.id
;
I have done like below to achieve required result Instead of '%' (mod) you can keep periodicity. And i have assumed your id column will have sequentiall numbers so i have used it because of eliminating row_number
SELECT ID,
TYPE,
CASE
WHEN RNO = 1
AND TYPE = 1 THEN 'TRUE'
WHEN RNO = 2
AND TYPE = 2 THEN 'TRUE'
ELSE 'FALSE'
END STATUS
FROM (SELECT ID,
TYPE,
CASE
WHEN TYPE = 1 THEN Replace(ID%3, 0, 3)
ELSE Replace(ID%2, 0, 2)
END RNO
FROM #TABLE1)A
You may try something like below, please replace with the appropriate column names,
update tbl_Sample set Operation = 0 --default to false
;WITH CTE
AS
(
select *
,ROW_NUMBER() over(Partition by Periodicity order by ID) 'R'
from tbl_Sample
)
UPDATE CTE
SET Operation = 1
WHERE R%Periodicity = 1
Select query is not working when use variable in MSSQL2014
My Schema is :-
CREATE TABLE product
(idproduct int, name varchar(50), description varchar(50), tax decimal(18,0))
INSERT INTO product
(idproduct, name, description,tax)
VALUES
(1, 'abc', 'This is abc',10),
(2, 'xyz', 'This is xyz',20),
(3, 'pqr', 'This is pqr',15)
CREATE TABLE product_storage
(idstorage int,idproduct int,added datetime, quantity int, price decimal(18,0))
INSERT INTO product_storage
(idstorage,idproduct, added, quantity,price)
VALUES
(1, 1, 2010-01-01,0,10.0),
(2, 1, 2010-01-02,0,11.0),
(3, 1, 2010-01-03,10,12.0),
(4, 2, 2010-01-04,0,12.0),
(5, 2, 2010-01-05,10,11.0),
(6, 2, 2010-01-06,10,13.0),
(7, 3, 2010-01-07,10,14.0),
(8, 3, 2010-01-07,10,16.0),
(9, 3, 2010-01-09,10,13.0)
and i am executing below command:-
declare #price1 varchar(10)
SELECT p.idproduct, p.name, p.tax,
[#price1]=(SELECT top 1 s.price
FROM product_storage s
WHERE s.idproduct=p.idproduct AND s.quantity > 0
ORDER BY s.added ASC),
(#price1 * (1 + tax/100)) AS [price_with_tax]
FROM product p
;
This is not working in MSSQL, Please Help me out.
for detail check http://sqlfiddle.com/#!6/91ec2/296
And My query is working in MYSQL
Check for detail :- http://sqlfiddle.com/#!9/a71b8/1
Try this query
SELECT
p.idproduct
, p.name
, p.tax
, (t1.price * (1 + tax/100)) AS [price_with_tax]
FROM product p
inner join
(
SELECT ROW_NUMBER() over (PARTITION by s.idproduct order by s.added ASC) as linha, s.idproduct, s.price
FROM product_storage s
WHERE s.quantity > 0
) as t1
on t1.idproduct = p.idproduct and t1.linha = 1
Try it like this:
Explanantion: You cannot use a variable "on the fly", but you can do row-by-row calculation in an APPLY...
SELECT p.idproduct, p.name, p.tax,
Price.price1,
(price1 * (1 + tax/100)) AS [price_with_tax]
FROM product p
CROSS APPLY (SELECT top 1 s.price
FROM product_storage s
WHERE s.idproduct=p.idproduct AND s.quantity > 0
ORDER BY s.added ASC) AS Price(price1)
;
EDIT: Your Fiddle uses a bad literal date format, try this:
INSERT INTO product_storage
(idstorage,idproduct, added, quantity,price)
VALUES
(1, 1, '20100101',0,10.0),
(2, 1, '20100102',0,11.0),
(3, 1, '20100103',10,12.0),
(4, 2, '20100104',0,12.0),
(5, 2, '20100105',10,11.0),
(6, 2, '20100106',10,13.0),
(7, 3, '20100107',10,14.0),
(8, 3, '20100108',10,16.0),
(9, 3, '20100109',10,13.0)
Here is the correct schema for SQL Server and query runs perfect as Shnugo Replied.
VALUES
(1, 1, convert(datetime,'2010-01-01'),0,10.0),
(2, 1, convert(datetime,'2010-01-02'),0,11.0),
(3, 1, convert(datetime,'2010-01-03'),10,12.0),
(4, 2, convert(datetime,'2010-01-04'),0,12.0),
(5, 2, convert(datetime,'2010-01-05'),10,11.0),
(6, 2, convert(datetime,'2010-01-06'),10,13.0),
(7, 3, convert(datetime,'2010-01-07'),10,14.0),
(8, 3, convert(datetime,'2010-01-07'),10,16.0),
(9, 3, convert(datetime,'2010-01-09'),10,13.0)