SQLite Query Execution on meeting a Condition - sql
I am trying to come up with an SQLite query which would retrieve all the row values between two given values (A and B) in the query,upon meeting a condition.
if (value B given is greater than the maximum value of B in the table):
- retrieve all values between A and B
Sample Table: inventory
Prod_name | model | location |
tesla | "5.6.1" | CA
toyota | "4.7.1" | WA
kia | "6.8.1" | MD
tesla | "2.6.2" | CA
chev | "7.8.4" | AZ
Input given : model between ("5.0.0" to "8.2.0")
Output : (telsa,5.6.1,CA),(kia,6.8.1,MD) , (chev,7.8.4,AZ)
Input given : model between ("5.0.0" to "6.9.0")
Output: Query should not run as "7.8.4" > "6.9.0"
i.e ( the max value in the table is greater than the upper limit of input query.
Also to note is the model name is TEXT format. I need help to retrieving
I have tried "CASE" statements of sqlite but was not able to retrieve
multiple columns in the subquery.
select
case
when (select 1000000 * replace(model, '.', 'x') +
1000 * replace(substr(model, instr(model, '.') + 1), '.', 'x') +
replace(model, '.', '000') % 1000 as md from inventory ORDER BY md
DESC LIMIT 1) > (select 1000000 * replace('5.0.0', '.', 'x') +
1000 * replace(substr('5.0.0', instr('5.0.0', '.') + 1), '.', 'x') +
replace('5.0.0', '.', '000') % 1000)
THEN (select model from inventory where
1000000 * replace(model, '.', 'x') +
1000 * replace(substr(model, instr(model, '.') + 1), '.', 'x') +
replace(model, '.', '000') % 1000
between
1000000 * replace('5.0.0' '.', 'x') +
1000 * replace(substr(''5.0.0'', instr('5.0.0', '.') + 1), '.',
'x') +
replace('5.0.0', '.', '000') % 1000
and
1000000 * replace('8.5.0', '.', 'x') +
1000 * replace(substr('8.5.0', instr('8.5.0', '.') + 1), '.', 'x') +
replace('8.5.0', '.', '000') % 1000 )
END from inventory
I believe that the following will do what you want :-
/* Query using model in n.n.n format */
SELECT * FROM inventory
WHERE
((1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000)
BETWEEN
(
SELECT 1000000 * substr('5.0.0',1,instr('5.0.0','.') -1)
+ (1000 * replace(substr('5.0.0',instr('5.0.0','.') + 1),'.','x'))
+ replace('5.0.0','.','000') % 1000
)
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
/* MAX COndition */
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
>
(
SELECT MAX(((1000000 * substr(model,1,instr(model,'.')-1))
+ (1000 * replace(substr(model,instr(model,'.') + 1),'.','x'))
+ replace(model,'.','000') % 1000))
FROM inventory
)
ORDER BY
(1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000
;
I am curious to know how this could be used in the current solution.
Or if you have any other approach ?
I would suggest that you are grossly over-complicating matters by using a model that is formatted as n.n.n.
If you were to convert that model to an integer value matters could be greatly simplified.
If you really want to keep the model as n.n.n then perhaps ALTER the table to add a column that stores the model as an integer. e.g. you could, as a one of, use :-
ALTER TABLE inventory ADD COLUMN model_value INTEGER DEFAULT -1;
This adds the column model_value
The ALTER could be followed by a mass UPDATE to then set the values for existing rows e.g. :-
UPDATE inventory SET model_value =
(1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000;
To circumvent needing to change the insert and pre-calculate the model_value, you could add an AFTER INSERT TRIGGER e.g. :-
CREATE TRIGGER IF NOT EXISTS inventory_generate_modelvalue AFTER INSERT ON inventory
BEGIN
UPDATE inventory
SET model_value = (1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000
WHERE model_value < 0 OR model_value IS NULL
;
END;
Note that if you currently use INSERT without specifying the columns, then the insert would have to be adjusted to specify the columns to be used for the insert, OR you could hard code -1 or NULL for the new column.
The query would then be simpler as :-
/* Query using model_value) */
SELECT * FROM inventory
WHERE model_value
BETWEEN
(
SELECT 1000000 * substr('5.0.0',1,instr('5.0.0','.') -1)
+ (1000 * replace(substr('5.0.0',instr('5.0.0','.') + 1),'.','x'))
+ replace('5.0.0','.','000') % 1000
)
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
>
(SELECT MAX(model_value) FROM inventory)
ORDER BY model_value
;
If you wanted convert the model value to n.n.n format you could use base this upon :-
SELECT prod_name,
CAST (model_value / 1000000 AS TEXT)
||'.'
|| CAST((model_value % 1000000) / 1000 AS TEXT)
||'.'
||CAST(model_value % 1000 AS TEXT)
AS model,
location
FROM inventory;
Of course if you had a function within your program or used integer values rather than n.n.n then matters would be even simpler.
Testing
The following code was used for testing the above :-
DROP TABLE IF EXISTS inventory;
DROP TRIGGER IF EXISTS inventory_generate_modelvalue;
CREATE TABLE IF NOT EXISTS inventory (prod_name TEXT ,model TEXT,location TEXT);
INSERT INTO inventory VALUES ('tesla','5.6.1','CA'),('toyota','4.7.1','WA'),('kia','6.8.1','MD'),('tesla','2.6.2','CA'),('chev','7.8.4','AZ') ;
/* Add new column for model as an integer value */
ALTER TABLE inventory ADD COLUMN model_value INTEGER DEFAULT -1;
/* Update existing data for new column */
UPDATE inventory SET model_value =
(1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000;
CREATE TRIGGER IF NOT EXISTS inventory_generate_modelvalue AFTER INSERT ON inventory
BEGIN
UPDATE inventory
SET model_value = (1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000
WHERE model_value < 0 OR model_value IS NULL
;
END;
-- INSERT INTO inventory VALUES('my new model','5.0.1','AA',null),('another','0.999.999','ZZ',-1);
SELECT * FROM inventory;
/* Query using model in n.n.n format */
SELECT * FROM inventory
WHERE
((1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000)
BETWEEN
(
SELECT 1000000 * substr('5.0.0',1,instr('5.0.0','.') -1)
+ (1000 * replace(substr('5.0.0',instr('5.0.0','.') + 1),'.','x'))
+ replace('5.0.0','.','000') % 1000
)
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
/* MAX COndition */
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
>
(
SELECT MAX(((1000000 * substr(model,1,instr(model,'.')-1))
+ (1000 * replace(substr(model,instr(model,'.') + 1),'.','x'))
+ replace(model,'.','000') % 1000))
FROM inventory
)
ORDER BY
(1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000
;
/* Query using model_value) */
SELECT * FROM inventory
WHERE model_value
BETWEEN
(
SELECT 1000000 * substr('5.0.0',1,instr('5.0.0','.') -1)
+ (1000 * replace(substr('5.0.0',instr('5.0.0','.') + 1),'.','x'))
+ replace('5.0.0','.','000') % 1000
)
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
>
(SELECT MAX(model_value) FROM inventory)
ORDER BY model_value
;
SELECT prod_name,
CAST (model_value / 1000000 AS TEXT)
||'.'
|| CAST((model_value % 1000000) / 1000 AS TEXT)
||'.'
||CAST(model_value % 1000 AS TEXT)
AS model,
location
FROM inventory;
Related
mismatched input 'from'. expecting: 'on', 'using' in Athena
Facing this error when using the below code SELECT AM.MemoryDescription from universal_final_circle_sg_Assets LEFT JOIN ( SELECT DISTINCT AP2.AssetID, SUBSTRING( ( SELECT ', ' + TRIM(STR(CountNo)) + ' x' + Capacity AS char FROM ( SELECT AssetID, COUNT(*) AS CountNo, Max(CHARACTER_LENGTH(Capacity / 1024 / 1024 / 1024, 5, 2) + ' GB RAM ' + Type) AS Capacity FROM universal_final_circle_sg_AssetMemoryModules WHERE SeparatedAsset = 0 GROUP BY AssetID, Capacity, Type ) AP1 WHERE AP1.AssetID = AP2.AssetID -- FOR XML PATH ('') ), 3, 1000) [MemoryDescription] FROM ( SELECT AssetID, COUNT(*) AS CountNo, Max(TRIM(Capacity / 1024 / 1024 / 1024),TRIM(5), 2) + ' GB ' + Type)) AS Capacity FROM universal_final_circle_sg_AssetMemoryModules WHERE SeparatedAsset = 0 GROUP BY AssetID, Capacity, Type ) AP2 ) AM ON universal_final_circle_sg_Assets.ID = AM.AssetID
How to use the value from one select query into another in union?
I am trying to insert rows made from random values in a table 'Users' where the the email is dependent on the value of the username: The row 'username' is not seen by the last SELECT query (doesn't exist) How can i fix this? SELECT( SELECT string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as username from (values('abcdefghijklmnopqrstuvwxyz0123456789')) as symbols(characters) join generate_series(1, 15) on 1 = 1 UNION SELECT string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as password from (values('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789*')) as symbols(characters) join generate_series(1, 15) on 1 = 1 UNION SELECT string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as phonenumber from (values('0123456789')) as symbols(characters) join generate_series(1, 9) on 1 = 1 UNION SELECT (username || '#' || ( CASE (RANDOM() * 2)::INT WHEN 0 THEN 'gmail' WHEN 1 THEN 'hotmail' WHEN 2 THEN 'yahoo' END ) || '.com') AS email ) INTO "User" from generate_series(1,10000) This also doesn't work: SELECT( SELECT string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as username from (values('abcdefghijklmnopqrstuvwxyz0123456789')) as symbols(characters) join generate_series(1, 15) on 1 = 1 UNION SELECT string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as password from (values('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')) as symbols(characters) join generate_series(1, 15) on 1 = 1 as select1 UNION SELECT string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as phonenumber from (values('0123456789')) as symbols(characters) join generate_series(1, 9) on 1 = 1 UNION SELECT (select1.username || '#' || ( CASE (RANDOM() * 2)::INT WHEN 0 THEN 'gmail' WHEN 1 THEN 'hotmail' WHEN 2 THEN 'yahoo' END ) || '.com') AS email ) INTO "User" from generate_series(1,10000)
Consider a recursive CTE query and removing subqueries for top level columns: WITH RECURSIVE main AS ( SELECT string_agg(substr(shortchars, (random() * length(shortchars) + 1)::integer, 1),'') AS "username" , string_agg(substr(longchars, (random() * length(longchars) + 1)::integer, 1), '') AS "password" , substr(string_agg(substr(nums, (random() * length(nums) + 1)::integer, 1), ''),1,9) AS "phonenumber" , 1 AS n FROM (values('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!##$%^&*')) as longchars(longchars) CROSS JOIN (values('abcdefghijklmnopqrstuvwxyz0123456789')) as shortchars(shortchars) CROSS JOIN (values('0123456789')) as num(nums) CROSS JOIN generate_series(1, 15) UNION ALL SELECT string_agg(substr(shortchars, (random() * length(shortchars) + 1)::integer, 1),'') AS "username" , string_agg(substr(longchars, (random() * length(longchars) + 1)::integer, 1), '') AS "password" , substr(string_agg(substr(nums, (random() * length(nums) + 1)::integer, 1), ''),1,9) AS "phonenumber" , n + 1 FROM (values('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!##$%^&*')) as longchars(longchars) CROSS JOIN (values('abcdefghijklmnopqrstuvwxyz0123456789')) as shortchars(shortchars) CROSS JOIN (values('0123456789')) as num(nums) CROSS JOIN generate_series(1, 15) CROSS JOIN (SELECT n FROM main LIMIT 1) AS mn WHERE n < 10 GROUP BY mn.n ) SELECT CONCAT(main."username", '#', ( CASE (RANDOM() * 2)::INT WHEN 0 THEN 'gmail' WHEN 1 THEN 'hotmail' WHEN 2 THEN 'yahoo' END ), 'com') AS email , main."username" , main."password" , main."phonenumber" FROM main Online Demo
SQLITE Query- To retrieve all versions between two versions?
The sqlite table consists of attributes : |Versions (TEXT)| | "2.73.8" | | "3.6.4 " | | "3.9.11" | and so on.. I want to retrieve all the versions from the table between two versions given in the query. For instance: Between versions- 2.9.10 & 3.7.10 . I could not find any sqlite function to query this directly. I used Substring (SUBSTR) to split to get individual digits which could then be compared to the one present in the table. I was successful in doing that but I could find a way to query to retrieve all versions between two version set. create table prod(version varchar); insert into prod values('2.7.5'); insert into prod values('2.7.4'); insert into prod values('2.0.0'); insert into prod values('22.73.55'); insert into prod values('22.17.54'); insert into prod values('22.10.06'); insert into prod values('3.7.5'); insert into prod values('3.4.5'); insert into prod values('3.7.6'); Query to retrieve all versions below or equal to : "3.50.6" (using nested "case when" ): SELECT * from prod Where version IN ( SELECT CASE WHEN (CAST(substr(version,0,instr(version,'.')) as integer)=3) THEN CASE WHEN (cast(SUBSTR(SUBSTR(version, INSTR(version, '.')),1,INSTR(SUBSTR(version, INSTR(version, '.') + 1), '.') - 1) as float)< 0.50 ) THEN version ELSE CASE WHEN (cast(SUBSTR(SUBSTR(version, INSTR(version, '.')),1,INSTR(SUBSTR(version, INSTR(version, '.') + 1), '.') - 1) as float)=0.50) THEN CASE WHEN (CAST(replace(version, rtrim(version, replace(version, '.', '')), '')AS INTEGER)<=6) THEN version END END END END FROM prod); Kindly provide me a way to query to retrieve all versions in the table between two sets of versions.
I believe the following will do as you want :- WITH /* SELECTION parameters */ parm(p1,p2) AS (SELECT '3.4.5', '22.10.6' /*<<<<<<<<<<<<< Versions to check against lower first*/), /* Parse 1, 1st value of each and the rest for parse2 */ pv1(parmv1,rest4p2,parm2v1,rest4p22) AS ( SELECT CAST(substr(p1,1,instr(p1,'.')-1) AS INTEGER), substr(p1,instr(p1,'.')+1), CAST(substr(p2,1,instr(p2,'.')-1) AS INTEGER), substr(p2,instr(p2,'.')+1) FROM parm ), /* Parse 2 extra 2 values retrieved for each parameter */ pv2(parmv2,parmv3,parm2v2,parm2v3) AS ( SELECT CAST(substr(rest4p2,1,instr(rest4p2,'.')-1) AS INTEGER), CAST(substr(rest4p2,instr(rest4p2,'.')+1) AS INTEGER), CAST(substr(rest4p22,1,instr(rest4p22,'.')-1) AS INTEGER), CAST(substr(rest4p22,instr(rest4p22,'.')+1) AS INTEGER) FROM pv1), /* calculate the lower and upper values to be checked against for the BETWEEN clause */ finalp(lower,higher) AS ( SELECT (parmv1 * 1000 * 1000) + (SELECT (parmv2 * 1000) + parmv3 FROM pv2), (parm2v1 * 1000 * 1000) + (SELECT (parm2v2 * 1000) + parm2v3 FROM pv2) FROM pv1), /* Parse 1 for the actual data gets the 1st part of the version and the rest of the version */ v1(v1rowid,vpart1,rest4v2) AS (SELECT rowid, CAST(substr(version,1,instr(version,'.')-1) AS INTEGER),substr(version,instr(version,'.')+1) FROM prod), /* Parse 2 for the actual data gets the 2nd and third parts */ v2(v2rowid,vpart2,vpart3) AS (SELECT v1rowid, CAST(substr(rest4v2,1,instr(rest4v2,'.')+1) AS INTEGER),CAST(substr(rest4v2,instr(rest4v2,'.')+1) AS INTEGER) FROM v1) SELECT version FROM v1 JOIN v2 ON v1rowid = v2rowid /* join the 2nd and third parts with the 1st */ JOIN prod ON prod.rowid = v1rowid /* also join the original data for the original version */ JOIN finalp ON 1 = 1 /* joint the upper and lower values */ WHERE (vpart1 * 1000 * 1000) + (vpart2 * 1000) + vpart3 /* do the same calculation used for the upper and lower parameters */ BETWEEN lower AND higher ; The above results in :- and using :- .... (SELECT '3.4.5', '22.10.06' /*<<<<<<<<<<<<< Versions to check against lower first*/) ... as well as :- .... (SELECT '3.4.5', '22.10.6' /*<<<<<<<<<<<<< Versions to check against lower first*/) .... - .6 instead of .06 (i.e. leading 0's are ignored) Results in :- This also checks boundary hits i.e. 3.4.5 and 20.10.06 were selected.
I assume that each one of the 3 parts of the version value is max 3 digits. The simplest way to convert a version value to a number so to make it comparable, is : multiply the 1st part by 1000000, the 2nd part by 1000 and then add them plus the 3d part. In code: 1000000 * replace(version, '.', 'x') + 1000 * replace(substr(version, instr(version, '.') + 1), '.', 'x') + replace(version, '.', '000') % 1000 number If you execute: select version, 1000000 * replace(version, '.', 'x') + 1000 * replace(substr(version, instr(version, '.') + 1), '.', 'x') + replace(version, '.', '000') % 1000 numericversion from prod you get: | version | numericversion | | -------- | -------------- | | 2.7.5 | 2007005 | | 2.7.4 | 2007004 | | 2.0.0 | 2000000 | | 22.73.55 | 22073055 | | 22.17.54 | 22017054 | | 22.10.06 | 22010006 | | 3.7.5 | 3007005 | | 3.4.5 | 3004005 | | 3.7.6 | 3007006 | So to get all versions between versions- 2.9.10 & 3.7.10, do this: with cte1 as ( select 1000000 * replace('2.9.10', '.', 'x') + 1000 * replace(substr('2.9.10', instr('2.9.10', '.') + 1), '.', 'x') + replace('2.9.10', '.', '000') % 1000 numericversion ), cte2 as ( select 1000000 * replace('3.7.10', '.', 'x') + 1000 * replace(substr('3.7.10', instr('3.7.10', '.') + 1), '.', 'x') + replace('3.7.10', '.', '000') % 1000 numericversion ), versions as ( select version, 1000000 * replace(version, '.', 'x') + 1000 * replace(substr(version, instr(version, '.') + 1), '.', 'x') + replace(version, '.', '000') % 1000 numericversion from prod ) select version from versions where numericversion between (select numericversion from cte1) and (select numericversion from cte2) The 1st CTE returns the numeric value of 2.9.10, the 2nd CTE returns the numeric value of 3.7.10 and the 3d the numeric values of all the versions in the table. Finally the query compares the numeric versions. See the demo. Results: | version | | ------- | | 3.7.5 | | 3.4.5 | | 3.7.6 | Or without the CTEs by hardcoding the 2 versions as numbers: select version from prod where 1000000 * replace(version, '.', 'x') + 1000 * replace(substr(version, instr(version, '.') + 1), '.', 'x') + replace(version, '.', '000') % 1000 between 2009010 and 3007010 See the demo. Or: select version from prod where 1000000 * replace(version, '.', 'x') + 1000 * replace(substr(version, instr(version, '.') + 1), '.', 'x') + replace(version, '.', '000') % 1000 between 1000000 * replace('2.9.10', '.', 'x') + 1000 * replace(substr('2.9.10', instr('2.9.10', '.') + 1), '.', 'x') + replace('2.9.10', '.', '000') % 1000 and 1000000 * replace('3.7.10', '.', 'x') + 1000 * replace(substr('3.7.10', instr('3.7.10', '.') + 1), '.', 'x') + replace('3.7.10', '.', '000') % 1000 See the demo.
Convert Recursive CTE to Recursive Subquery
How would I convert the following CTE into a recursive subquery? It's an implementation of Newtons Method. Reasons: 1) I have no permissions to create functions or stored procs in the DB 2) I must do everything in TSQL 3) Not using Oracle TESTDATA Table PMT t V 6918.26 6 410000 3636.51 14 460000 3077.98 22 630000 1645.14 18 340000 8591.67 13 850000 Desired Output PMT t V Newton 6918.26 6 410000 0.066340421 3636.51 14 460000 0.042449138 3077.98 22 630000 0.024132674 1645.14 18 340000 0.004921588 8591.67 13 850000 0.075982984 _ DECLARE #PMT AS FLOAT DECLARE #t AS FLOAT DECLARE #V AS FLOAT --These will be only for 1 example. SET #PMT = 6918.26740930922 SET #t = 6 SET #V = 410000 ;With Newton (n, i,Fi,dFi) AS ( --base SELECT 1, CAST(0.1 AS FLOAT) ,#PMT * (1 - POWER((1 + CAST(0.1 AS FLOAT) / 12), (-#t * 12))) - #V * CAST(0.1 AS FLOAT) / 12 ,#PMT * #t * 12 * POWER((1 + CAST(0.1 AS FLOAT) / 12), (-#t * 12 - 1)) - #V UNION ALL --recursion SELECT n + 1 ,i - Fi/dFi ,#PMT * (1 - POWER((1 + i / 12), (-#t * 12))) - #V * i / 12 ,#PMT * #t * 12 * POWER((1 + i / 12), (-#t * 12 - 1)) - #V FROM Newton WHERE n < 500) --to get the desired value for params above SELECT [x].i FROM ( SELECT n, i, Fi, dFi FROM Newton WHERE n = 500 ) [x] OPTION (MAXRECURSION 500) _ I want Newton to evaluate on Every record of TestData as a stand alone column. Any thoughts?
SQL missing indexes from select Databases
I am trying to use the SQL missing index feature; however.... I am not the DBO or admin. I have access to one database. is there a way to run this for only a single database without elevated permission SELECT migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) AS improvement_measure, 'CREATE INDEX [missing_index_' + CONVERT (varchar, mig.index_group_handle) + '_' + CONVERT (varchar, mid.index_handle) + '_' + LEFT (PARSENAME(mid.statement, 1), 32) + ']' + ' ON ' + mid.statement + ' (' + ISNULL (mid.equality_columns,'') + CASE WHEN mid.equality_columns IS NOT NULL AND mid.inequality_columns IS NOT NULL THEN ',' ELSE '' END + ISNULL (mid.inequality_columns, '') + ')' + ISNULL (' INCLUDE (' + mid.included_columns + ')', '') AS create_index_statement, migs.*, mid.database_id, mid.[object_id] FROM sys.dm_db_missing_index_groups mig INNER JOIN sys.dm_db_missing_index_group_stats migs ON migs.group_handle = mig.index_group_handle INNER JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handle WHERE migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) > 10 ORDER BY migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) DESC
dm_db_missing_index_details dynamic view has Database Id in it. WHERE migs.avg_total_user_cost * ( migs.avg_user_impact / 100.0 ) * ( migs.user_seeks + migs.user_scans ) > 10 AND mid.database_ID = Db_id('Database name') --Here Note : Users must be granted the VIEW SERVER STATE permission or any permission that implies the VIEW SERVER STATE permission to access all the three dynamic view used in your query
Add following in the where clause: AND mid.database_ID = Db_id('Your Database name')