Get intermediate time periods in SQL Server [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to generate a table in SQL of intermediate joined states. E.g. I have the following table
status_1 status_2 start_date_V1 end_date_v1 start_date_2 end_date_v2
--------------------------------------------------------------------------------
A B 01Jan2018 31Jul2018 31Dec2017 31Jan2018
A C 01Jan2018 31Jul2018 01Feb2018 30Dec2018
In this table there are start and end dates of the different states "status_1" and "status_2". I wan to have the information about the changes of the two joined states. The desired table would be:
status_1 status_2 start_date end_date
-----------------------------------------------
A B 01Jan2018 31Jan2018
A C 01Feb2018 31Jul2018
The following image might help to understand the problem:
Can anyone help?

Seems like you need the intersecting time period(?), that'd be solved with a simple 'CASE-WHEN-ELSE'-statement for each date in the query result.
SELECT
[status1],
[status2],
[start_date] = CASE WHEN [start_date_V1] < [start_date_2] THEN [start_date_V1] ELSE [start_date_2] END,
[end_date] = CASE WHEN [end_date_v1] < [end_date_v2] THEN [end_date_v1] ELSE [end_date_v2] END
FROM Table
If you've got many date columns (known amount), it'd be cleaner to type it as below. However, beware that sub queries like this can slow down your queries tremendously, if you don't know what you're doing.
SELECT
Status1,
Status2,
-- New Name Name of custom group of values Column1 Column2 Name of custom group of values
-- | | | | |
[start_date] = (SELECT MAX(StartDate) FROM (VALUES (start_date_1), (start_date_2)) AS value(StartDate)),
[end_date] = (SELECT MIN(EndDate) FROM (VALUES (end_date_1), (end_date_2)) AS value(EndDate))
FROM Table

Related

Count by ID if value is lower than and set the value 1 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a problem.
My table is tableXYZ
I have a row a where i set the year.
I have a row b where i need to set value 1 if the year from row a is lower than 2019 and 0 if is > 2019.
After all i need to make a count from row b only the 1 values as total.
How can i do that, because i tried a lot of examples but doesn't work.
This might help:
SELECT COUNT(1) AS [b_count]
FROM (
-- Use case to evaluate column a and set value for column b
SELECT a,
CASE WHEN a < 2019 then '1' else '0' end as [b]
FROM tableXYZ
) AS X -- Alias of subquery
WHERE X.b = '1' -- Select only rows where b = '1'

How to get column values as a table column in sql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
This is my table defined in the database:
pi_value | Status
------------+-------------------
500.000 | Bank Submitted
500.0000 | Bank Submitted
1000.0000 | Maturity Received
4000.0000 | Bank Submitted
50.0000 | Maturity Received
I want the output to look like this:
Maturity Received | Bank Submitted |
------------------+----------------+
1050.0000 | 5000.0000 |
------------------+----------------+
use conditional aggregation
select sum(case when status='Bank Submitted' then pi_value else 0 end),
sum(case when status='Maturity Received' then pi_value else 0 end) from table
As you say in a comment that the status is not before-known, just select the mere data and have your app or website do the layout:
select status, sum(pi_value) as total
from mytable
group by status
order by status;
Whatever programming language you are using, it will be easy to simply loop through the result set and fill some grid with it.
Alternative:
If you want to get the pivoted data right away, you'll have to use dynamic SQL instead. This means you select the data needed to build the query first, then you build the query from it and run it. Here is how to get the statuses:
select distinct status from mytable order by status;
Then loop through these and construct a query like shown in Zaynul Abadin Tuhin's answer. Then run that query.

SQL filter using 2 dates + SAS [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a dataset that looks like below
ctry | start | end
I have a second dataset b
that has
ctry | start | end
as well as other columns.
I would like to filter the second dataset based on ctry, start and end dates
Am presuming you are looking for some kind of inner join (keep all records in the second dataset, b, if the three columns match those in the first dataset). Try the following approach (one of many):
proc sql;
create table filtered as
select b.*
from first_ds as a /* you never said what your first dataset was called */
inner join b as b
on a.ctry=b.ctry
/* edits following OP comment */
and a.start < b.end
and a.end > b.start;

SQL query to update [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have this table employee with columns
name | salary
-------------
A | 5000
B | 2000
c | 1000
another table
works with columns
name| work
---------
A | w1
A | w2
A | w3
B | w4
B | w5
I want to increase salary of employee by 100 per work and update only if number of works are greater than 1.
Can anyone help me out with this. I need a sql update query for this( no stored proc or trigger or cursor).
Please try using merge statement:
MERGE
INTO employee
USING (
select distinct "name", count(*) over (partition by "name") cnt from works
)x
ON (employee."name" = x."name")
WHEN MATCHED THEN
UPDATE
SET salary = salary+(100*case when cnt=1 then 0 else cnt end);
here is proper query
update employee as a set salary = salary + 100 * NVL((
SELECT count(*)
FROM works as b
where b.name=a.name
group by name
having count(*)>1), 0)

Counting frequency of customer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this table on sql sever
cstomer |No_Nota
CUS000 | 98342
CUS000 | 98343
CUS000 | 98343
CUS001 | 98355
CUS001 | 98355
I would like to count the frequency of each customer. For similar number of no_nota the value is 1.
I'd like a result like this:
cstomer |Frequent
CUS000 | 2
CUS001 | 1
You want the distinct count of the column no_nota, so that's what you should select...
select customer, count(distinct no_nota) as frequent
from my_table
group by customer
You want to count the result of your select query:
SELECT COUNT(expression)
FROM tables
WHERE predicates;
Example:
SELECT COUNT(No_Nota) FROM your_table WHERE No_Nota > 0;
Sounds like you just want a group by statement to get a count of all individual customers. Something like:
SELECT cstomer, SUM(1) as Frequent FROM table GROUP BY cstomer, No_Nota