How to check the Data gap in the query - sql

Scenario: in the below scenario inserted date is missing for 4/21/2021
for the latest record should check whether it has 4/20 if not then count as 1
the data starting from 4/14 to 4/20 we dont find any gap - Please help on this query
Let me know if more info need assume table name is TABA

One typical way you would handle this would be to use a calendar table, containing all dates which your data might cover. In lieu of a calendar table, we can inline one like this:
SELECT d.Date
FROM
(
SELECT '2021-04-14' AS Date UNION ALL
SELECT '2021-04-15' UNION ALL
SELECT '2021-04-16' UNION ALL
SELECT '2021-04-17' UNION ALL
SELECT '2021-04-18' UNION ALL
SELECT '2021-04-19' UNION ALL
SELECT '2021-04-20' UNION ALL
SELECT '2021-04-21' UNION ALL
SELECT '2021-04-22'
) d
LEFT JOIN yourTable t
ON t.InsertedDate = d.Date
WHERE
t.InsertedDate IS NULL;

Related

Oracle How to populate data when there is no query record

I want to execute the following SQL in PL/SQL
select did, n1, n2 ,n3
from t where t.did in (‘A’,’C’,’G’)
the result will be “no recode”
I wish the result can be like this
how should I write the sql
Ideally you should have some other table which maintains all possible did values. In absence of that, we can try using a CTE here:
WITH cte AS (
SELECT 'A' AS did FROM dual UNION ALL
SELECT 'B' FROM dual UNION ALL
SELECT 'C' FROM dual
)
SELECT
t1.did,
t2.n1,
t2.n2,
t3.n3
FROM cte t1
LEFT JOIN t t2
ON t1.did = t2.did;
Of course, this also means that should your table have more than one record matching a giving did value, that your result set would also have more than one record for that did value. If you always expect a single record per did value, then you would have to give us the logic behind that.

Teradata SQL - Union and Where Clause

have a Teradata SQL UNION query and need to know how I can extract data from 2 tables where date in first part of UNION is before the date in the second part of the UNION query. Example:
Select
name1,date1
from table1
UNION
Select
name2,date2
from table2
where date2 < date1
Would like to see only records where date2 is before date1. This is just an example of what I need to retrieve from my data.
Select
Pat_Name, Provider, Clinic_Encounter_Date
from Clinic table
UNION
Pat_Name, Provider, Hosp_Encounter_Date
from Hospital table
where
Hosp_Encounter_Date < Clinic_Encounter_Date;
My SQL query runs fine, but when I add the WHERE clause I keep getting an error message. Teradata just doesn't like me!!!
Hopefully, this is a bit more helpful. :-)
Thank you in advance for your assistance.
The SQLs in before and after the UNION are independent SQLs, so in the second statement it is not recognizing date1. i.e. the following statement is not valid.
Select name2,date2 from table2 where date2 < date1
You will need to change your statement to
Select
name1,date1
from table1
UNION
Select
name2,date2
from table2
where date2 < (select Min(date1) from table1)

Using a SQL query, how can I select every date within a range?

I'm querying a support ticket database, and each ticket has a column for "date opened" and "date closed." Tickets frequently remain open for multiple days, so we need to be able to pull the number of tickets that are OPEN on each day.
For example, for 4/8/14, we need to know how many tickets were opened on 4/8, combined with the total number of unclosed tickets that were opened prior to 4/8 but remained still open at 12:00am on 4/8 (may or may not have been closed during or after 4/8).
This seems straightforward enough for a single date, but now I need to write a query that will pull a complete range of dates.
For example, we need to write a query that returns every date between 1/1/14 and 4/10/14 along with the total number of tickets open on each date (including dates on which 0 tickets were open).
Is this possible using only queries or subqueries, without using any stored procedures or temporary datatables?
We're currently pulling the data into Excel and calculating the date stats there, but Excel is not a scalable solution and we'd like to have SQL perform this work so we can migrate this report to SSRS (SQL Server Reporting Services) down the road.
Your question is not very clear to me, but with SQLServer 2005 or better (SQLServer 2008 or better for the type Date) you can create a calendar. This one the way to do it
WITH [counter](N) AS
(SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1)
, days(N) AS (SELECT row_number() over (ORDER BY (SELECT NULL)) FROM [counter])
, months (N) AS (SELECT N - 1 FROM days WHERE N < 13)
SELECT DISTINCT CAST(DATEADD(DAY, days.n,
DATEADD(MONTH, months.n, '20131231')
) AS date)
FROM months
CROSS JOIN days
ORDER BY 1
if you need more year just add a new cte accordingly
SQLFiddle
You can't do it, without having some sort of date table that contains a row for each date possible. Creating a date table is pretty easy, depending on your RDBMS and your requirements.

Oracle Query fetching table name alongwith column name

select trx_id,refernce number from
(select * from abcd_1_txt union
select * from abcd_2_txt union
select * from abcd_3_txt union
select * from abcd_4_txt)
where trx_id in (123,321,1234)
In the query all the tables are of same format, same column names and same number of columns.
After running this query, surely i will get some data.
My question --- is there any way to know from which of these tables, i am getting the output.
Try to add a column with number of query as below
select qrynum, trx_id,refernce number from
(select 1 as qrynum,* from abcd_1_txt union
select 2,* from abcd_2_txt union
select 3,* from abcd_3_txt union
select 4,* from abcd_4_txt)
where trx_id in (123,321,1234)
as Joe W said in the comment below you can also use name of the table instead of query number, short example:
select tabname, trx_id,refernce number from
(select 'abcd_1_txt' as tabname,* from abcd_1_txt union
...
where trx_id in (123,321,1234)
but both ways don't eliminate duplicates, so you can use union all instead of union. Other way to do that is to run quires separately with the condition
select * from abcd_1_txt where trx_id in (123,321,1234)
select * from abcd_2_txt where trx_id in (123,321,1234)
.
.
.

SQL two tables with different month values. Getting one result set for all the months

If I have two tables like this:
Table1
Month
1
3
Table 2
Month
1
4
How do I get the following result set?
Result Set
Month
1
3
4
Use the UNION operator like this:
SELECT Month FROM Table1
UNION
SELECT Month FROM Table2
The SQL UNION operator combines two or more SELECT statements and produces a single result with unique values.
SELECT [Month]
FROM Table1
UNION
SELECT [Month]
FROM Table2
You will want to use a UNION to combine the results into a single set.
SELECT Month
FROM Table1
UNION
SELECT Month
FROM Table2
By default UNION only grabs distinct values, therefore your exact result. If you wanted duplicate values you could use UNION ALL which would show month 1 in your example twice.
Here is a good tutorial that shows the differences as well.
Use UNION:
SELECT month FROM table1 UNION SELECT month FROM table2
SELECT Month FROM Table 1 UNION Select Month FROM Table 2 should do the trick
Here is my solution
declare # temptable table( months varchar(10) )
inserto into #temptable(months)
select distinct Month from Table1
inserto into #temptable(months)
select distinct Month from Table2
select disticnt * from #temptable
Maybe there is a better way?