dynamic query for several date - tsql

I wish to make a dynamic query for a set of dates, for example: 06/01/2015, 2015-07-01,2015-05-31, 30.04.2015 ... etc
I can do all these consultation date in one query:
select * from mytable
Where DimtiempoId = 'date'.
I do not want is to make a query for each date. example:
select * from mytable
Where DimtiempoId = "01/06/2015".
select * from mytable
Where DimtiempoId = "01/07/2015".
select * from mytable
Where DimtiempoId = "31/05/2015".

I'm not exactly sure what you're trying to do. Where is your set of dates coming from?
How about:
SELECT *
FROM mytable
WHERE DimtiempoId IN ('2015-01-06', '2015-01-07', '2015-05-31')
If your set of dates is coming from another table, you could do this:
SELECT *
FROM mytable
WHERE DimtiempoId IN (select mydate
from setOfDates)

thanks,
my set of dates are parameters of a sp.
To be more specific I save every result set in a table.
Example:
insert table1
select * from othertbl
where DimtiempoId= "01/06/2015"
insert table2
select * from othertbl
where DimtiempoId= "01/07/2015"
but I want to optimize my sp in a single query.

Related

How to SELECT only the "nth" record from a query result?

I have a T-SQL query that return X records ordered.
I want to get only on record , for instance, only the 5th record from that result: how ?
Thanks
For that you have to update your query.
I.e in oracle we have rownum that assign rownumber to every row.
You can do like this,
Select * from(
Select a.*, rownum as n from your table) where n = 3;
Something like this.
Try this:
WITH NumberedTable AS
(
SELECT
RowNo = ROW_NUMBER() OVER(ORDER BY <'Order Column here'>)
, *
FROM <'Table Name here'>
)
SELECT *
FROM NumberedTable
WHERE RowNo = <'Record No. here'>

Update column with more than one value

I have a table tableA which looks something like this:
issue_id start_date end_date
issue1 2019-11-07 2020-04-30
issue2 2019-11-07 2020-01-28
I have to update the end_date based on the results of the query.
UPDATE tableA SET end_date =
(
SELECT max_end_date from update_end_date
)
WHERE issue_id = (SELECT issue_id FROM update_end_date);
It works when when query returns one result. However it fails when more than one results are returned which make sense. I cannot pre determine the results of the query so it might return more than one result. Is there any way if I can update the column with multiple values.
You could use correlated subquery:
UPDATE tableA
SET end_date = (SELECT max_end_date
from update_end_date
WHERE update_end_date.issue_id = tableA.issue_id)
WHERE issue_id IN (SELECT issue_id FROM update_end_date);
Another possibility to #Lukas solution is using proprietary PostgreSQL's syntax UPDATE FROM
UPDATE tablea
SET end_date = max_end_date
FROM update_end_date
WHERE tablea.issue_id = update_end_date.issue_id

How can I get one particular value out of this table?

So I have this table: table
How do I obtain the cod_Armazem where it doesn't have any of the stocks at 0?
I am using SELECT cod_Armazem FROM table WHERE stock>0;
This should work
SELECT DISTINCT
t1.cod_Armazem
FROM table t1
WHERE NOT EXISTS(
SELECT *
FROM table
WHERE stock = 0
AND cod_Armazem = t1.cod_Armazem);

Create a variable from the output of a select statement and use that variable in another select statement POSTGRE SQL

Say I have the following query:
SELECT quantity
FROM TABLE A
WHERE account = xx
AND date = xxx-xx-xx
this query returns one value. Say I now want to use that value in a second query, as a variable, like:
SELECT SUM(quantity) + variable as 'CURRENT POS'
FROM TABLE B
WHERE account = xx
AND date = xxx-xx-xx
How would I go about doing this?
SELECT
(SELECT SUM(quantity) FROM tb2 WHERE account = xx AND date = xxx-xx-xx) +
(SELECT SUM(quantity) FROM tb1 WHERE account = xx AND date = xxx-xx-xx)
Just note there are a million ways to solve it :-)

Find most recent date

I have o table name table_1 with 4 columns id, text, fromDate, toDate. The table represents the working experience.I want to create a function which will return the row with columns id, text where the employee worked more recently. This means I need column toDate to be closest to today.
Here is a demonstration of my code:
Select (abs("toDate"-now())) as date_diff
from table_1
Select id,text
from table_1
where (abs("toDate"-now()))=select min(date_diff)
Is this correct or is there something better I can do?
I wil try something like this:
Select id,text
from table_1
where "toDate" = ( select max ("toDate") from table_1 )
It will provide you the latest "toDate" value.
Try this:
select * from table_1
order by to_date desc
limit 1