SQL Query to find missing numbers between 2 values - teradata

Need your expertise in writing an SQL for the below scenario
I have a single row in a table "range_num" as follows.
start_num end_num
10 14
Is there any way we can write a query to find all the gaps between 10 and 14 i ..e 11,12,13 in the below format
col1 col2 col3 col4 col5
10 11 12 13 14
I am using Teradata databases , so any compliant SQL query will be of great help!!

Here is solution for your problem...
select
max(case when rownum=1 then gap end) as col1,max(case when rownum=2 then gap end) as col2,
max(case when rownum=3 then gap end) as col3,max(case when rownum=4 then gap end) as col4,
max(case when rownum=5 then gap end) as col5,max(case when rownum=6 then gap end) as col6,
max(case when rownum=7 then gap end) as col7,max(case when rownum=8 then gap end) as col8,
max(case when rownum=9 then gap end) as col9,max(case when rownum=10 then gap end) as col10,
max(case when rownum=11 then gap end) as col11,max(case when rownum=12 then gap end) as col12,
max(case when rownum=13 then gap end) as col13,max(case when rownum=14 then gap end) as col14,
max(case when rownum=15 then gap end) as col15,max(case when rownum=16 then gap end) as col16,
max(case when rownum=17 then gap end) as col17,max(case when rownum=18 then gap end) as col18,
max(case when rownum=19 then gap end) as col19,max(case when rownum=20 then gap end) as col20,
max(case when rownum=21 then gap end) as col21,max(case when rownum=22 then gap end) as col22,
max(case when rownum=23 then gap end) as col23
from
(
select a.id_start,
b.day_of_calendar as gap,
csum(1,1) as rownum
from db_sok.testt a,
sys_calendar.calendar b
where b.day_of_calendar between a.id_start and a.id_end
)X
do a little tweaking in case you need something else...:)

Related

I want to create sub query in stored proc in oracle the following query giving error too many values

I want to add this query in stored procedure but it gives an error: ORA-00913: too many values
Select SUM(CONNMASS.CONN_BILLAMOUNT) as Revenuebilled,
count(CONNMASS.CONN_BILLEDUNITS) AS volumebilled,
count(MASSBILL.BM_lo_id) as normalbilled,
( SELECT COUNT(CASE WHEN SRM_DISCON_STATUS_ID = 3 THEN 1 END) AS ACTIVE_CONNECTIONS,
COUNT(CASE WHEN SRM_DISCON_STATUS_ID = 2 THEN 1 END) AS DISC_CONNECTIONS
from Connection_master)
from CONNECTION_MASTER CONNMASS
left join BILLING_MASTER MASSBILL
on MASSBILL.CONN_SERVICE_NO = CONNMASS.CONN_SERVICE_NO;
The problem is that you are selecting multiple values in the sub-query. You can only select a single value in a sub-query in the SELECT clause.
However, you do not need the sub-query and appear to want:
Select SUM(CONNMASS.CONN_BILLAMOUNT) as Revenuebilled,
count(CONNMASS.CONN_BILLEDUNITS) AS volumebilled,
count(MASSBILL.BM_lo_id) as normalbilled,
COUNT(CASE SRM_DISCON_STATUS_ID WHEN 3 THEN 1 END) AS ACTIVE_CONNECTIONS,
COUNT(CASE SRM_DISCON_STATUS_ID WHEN 2 THEN 1 END) AS DISC_CONNECTIONS
from CONNECTION_MASTER CONNMASS
left join BILLING_MASTER MASSBILL
on MASSBILL.CONN_SERVICE_NO = CONNMASS.CONN_SERVICE_NO;
If you did want the sub-queries then:
Select SUM(CONNMASS.CONN_BILLAMOUNT) as Revenuebilled,
count(CONNMASS.CONN_BILLEDUNITS) AS volumebilled,
count(MASSBILL.BM_lo_id) as normalbilled,
( SELECT COUNT(CASE SRM_DISCON_STATUS_ID WHEN 3 THEN 1 END)
from Connection_master) AS ACTIVE_CONNECTIONS,
( SELECT COUNT(CASE SRM_DISCON_STATUS_ID WHEN 2 THEN 1 END)
from Connection_master) AS DISC_CONNECTIONS
from CONNECTION_MASTER CONNMASS
left join BILLING_MASTER MASSBILL
on MASSBILL.CONN_SERVICE_NO = CONNMASS.CONN_SERVICE_NO;

update single record with results from multiple case query

Found related type solutions, but nothing to match my need;
I want to update an existing table record (fields pu_cnt, pu_ot, pu_ltc etc.) with the result from the query (pu_cnt, pu_ot, pu_ltc etc.) etc.
Here's my query:
SELECT
COUNT(CASE WHEN vuWO_CHC_ALL.status IN('C','D','H','F') THEN 1 END) AS
pu_cnt,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actpu_delay_code,1) LIKE '' THEN 1 END) AS
pu_ot,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actpu_delay_code,1) LIKE 'C' THEN 1 END)
AS pu_ltc,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actpu_delay_code,1) LIKE 'U' THEN 1 END)
AS pu_ltu,
COUNT(CASE WHEN vuWO_CHC_ALL.status IN('D','H','F') THEN 1 END) AS del_cnt,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actdel_delay_code,1) LIKE '' THEN 1 END) AS
del_ot,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actdel_delay_code,1) LIKE 'C' THEN 1 END)
AS del_ltc,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actdel_delay_code,1) LIKE 'U' THEN 1 END)
AS del_ltu
FROM vuWO_CHC_ALL WHERE
RTRIM(vuWO_CHC_ALL.SHIPPER) LIKE #pVendor AND
RTRIM(vuWO_CHC_ALL.ORIG) LIKE #pOrigin AND
RTRIM(vuWO_CHC_ALL.Dest) LIKE #pDestin
..
Figured it out using #variables.

How to use the MAX and Case function over three tables?

I have joined three tables using Max and Case functions.
When I used two tables it working fine but when I added the third table I got this error.
Ambiguous column name 'Month'.
SELECT Category ,
KPI ,
TDTargetValue ,
MAX(CASE WHEN Month = 'Jan' THEN Input
END) Jan ,
MAX(CASE WHEN Month = 'Feb' THEN Input
END) Feb ,
MAX(CASE WHEN Month = 'Mar' THEN Input
END) Mar ,
MAX(CASE WHEN Month = 'Apr' THEN Input
END) Apr ,
MAX(CASE WHEN Month = 'May' THEN Input
END) May ,
MAX(CASE WHEN Month = 'Jun' THEN Input
END) Jun ,
MAX(CASE WHEN Month = 'Jul' THEN Input
END) Jul ,
MAX(CASE WHEN Month = 'Aug' THEN Input
END) Aug ,
MAX(CASE WHEN Month = 'Sep' THEN Input
END) Sep ,
MAX(CASE WHEN Month = 'Oct' THEN Input
END) Oct ,
MAX(CASE WHEN Month = 'Nov' THEN Input
END) Nov ,
MAX(CASE WHEN Month = 'Dec' THEN Input
END) Dec
FROM [NEWSEMAKPI].[dbo].[NewCriteria] NC
INNER JOIN ( SELECT *
FROM [NEWSEMAKPI].[dbo].[UpdateData]
WHERE PeriodId = '1'
) UD ON UD.Cid = NC.Id
LEFT JOIN ( SELECT *
FROM [NEWSEMAKPI].[dbo].[TargetData]
) TD ON UD.Cid = TD.CId
WHERE NC.Grade = 'A'
AND IsActived = '0'
GROUP BY Category ,
KPI ,
TDTargetValue
ORDER BY 1
Any help would be appreciated.
Add alias also for the column Month...
Assuming NewCriteria table has the column Month
SELECT Category ,
KPI ,
TDTargetValue ,
MAX(CASE WHEN NC.Month = 'Jan' THEN Input
END) Jan ,
MAX(CASE WHEN NC.Month = 'Feb' THEN Input
END) Feb ,
MAX(CASE WHEN NC.Month = 'Mar' THEN Input
END) Mar ,
MAX(CASE WHEN NC.Month = 'Apr' THEN Input
END) Apr ,
MAX(CASE WHEN NC.Month = 'May' THEN Input
END) May ,
MAX(CASE WHEN NC.Month = 'Jun' THEN Input
END) Jun ,
MAX(CASE WHEN NC.Month = 'Jul' THEN Input
END) Jul ,
MAX(CASE WHEN NC.Month = 'Aug' THEN Input
END) Aug ,
MAX(CASE WHEN NC.Month = 'Sep' THEN Input
END) Sep ,
MAX(CASE WHEN NC.Month = 'Oct' THEN Input
END) Oct ,
MAX(CASE WHEN NC.Month = 'Nov' THEN Input
END) Nov ,
MAX(CASE WHEN NC.Month = 'Dec' THEN Input
END) Dec
FROM [NEWSEMAKPI].[dbo].[NewCriteria] NC
INNER JOIN ( SELECT *
FROM [NEWSEMAKPI].[dbo].[UpdateData]
WHERE PeriodId = '1'
) UD ON UD.Cid = NC.Id
LEFT JOIN ( SELECT *
FROM [NEWSEMAKPI].[dbo].[TargetData]
) TD ON UD.Cid = TD.CId
WHERE NC.Grade = 'A'
AND IsActived = '0'
GROUP BY Category ,
KPI ,
TDTargetValue
ORDER BY 1

ODBC rows to columns

i wanna change some rows to columns.
This is a example how i have my database now
And i wanna change it by
But i'm using a ODBC connection, if you could help me with a useful example or code guide i'll appreciate it.
Thanks
You can use a MAX/CASE "manual pivot" that should work on most (all?) SQL databases, something like;
SELECT "datetime",
MAX(CASE WHEN bsc='bsccc2' THEN ineffective_attempts END) bsccc2,
MAX(CASE WHEN bsc='bsccc3' THEN ineffective_attempts END) bsccc3,
MAX(CASE WHEN bsc='bsccc4' THEN ineffective_attempts END) bsccc4,
MAX(CASE WHEN bsc='bscmb2' THEN ineffective_attempts END) bscmb2,
MAX(CASE WHEN bsc='bscmbo' THEN ineffective_attempts END) bscmbo,
MAX(CASE WHEN bsc='bscva2' THEN ineffective_attempts END) bscva2
FROM mytable
GROUP BY "datetime"
ORDER BY "datetime"
Depending on the underlying database, you may want to change the quoting of the datetime identifier (or possibly rename the field to avoid needing to quote it).

How to convert column values into rows in Sqlite?

I have a table value in sqlite like..
i'm getting value in sqlite query like this..
select * from tablename where id='101' and id='102' and id='1' and id='18'
101 Local Local Local Local Local Local
102 9 12 9 12 9
1 3:55 4:20 4:40 5:00 5:20 5:40
18 4:50 5:15 5:35 5:55 6:15 6:35
Above value I need to convert in sqlite query like below..
Local 9 3:55 4:50
Local 12 4:20 5:15
Local 9 4:40 5:35
Local 12 5:00 5:55
Local 9 5:20 6:15
Local 9 5:40 6:35
Oh, so you're stuck with a terrible DB design which you aren't allowed to change... that sucks. It ought to be divided into 4 tables (at least). Anyway, here is one way to do it in this particular case.
I assume the first query is equivalent to this:
SELECT id, F1, F2, F3, F4, F5, F6
FROM tablename
WHERE id IN (101,102,1,18)
So, you can do transposing/pivoting/whatever it's actually called like this:
SELECT
MAX(CASE WHEN id=101 THEN F1 END) R1,
MAX(CASE WHEN id=102 THEN F1 END) R2,
MAX(CASE WHEN id=1 THEN F1 END) R3,
MAX(CASE WHEN id=18 THEN F1 END) R4
FROM tablename
UNION
SELECT
MAX(CASE WHEN id=101 THEN F2 END) R1,
MAX(CASE WHEN id=102 THEN F2 END) R2,
MAX(CASE WHEN id=1 THEN F2 END) R3,
MAX(CASE WHEN id=18 THEN F2 END) R4
FROM tablename
UNION
SELECT
MAX(CASE WHEN id=101 THEN F3 END) R1,
MAX(CASE WHEN id=102 THEN F3 END) R2,
MAX(CASE WHEN id=1 THEN F3 END) R3,
MAX(CASE WHEN id=18 THEN F3 END) R4
FROM tablename
UNION
SELECT
MAX(CASE WHEN id=101 THEN F4 END) R1,
MAX(CASE WHEN id=102 THEN F4 END) R2,
MAX(CASE WHEN id=1 THEN F4 END) R3,
MAX(CASE WHEN id=18 THEN F4 END) R4
FROM tablename
UNION
SELECT
MAX(CASE WHEN id=101 THEN F5 END) R1,
MAX(CASE WHEN id=102 THEN F5 END) R2,
MAX(CASE WHEN id=1 THEN F5 END) R3,
MAX(CASE WHEN id=18 THEN F5 END) R4
FROM tablename
UNION
SELECT
MAX(CASE WHEN id=101 THEN F6 END) R1,
MAX(CASE WHEN id=102 THEN F6 END) R2,
MAX(CASE WHEN id=1 THEN F6 END) R3,
MAX(CASE WHEN id=18 THEN F6 END) R4
FROM tablename
Unfortunately, it's sorted in a strange way.
And I really advise you to persuade someone in charge to re-design the DB. Some designers think that having tables with such structure is "flexible" and "improves performance", but it's not so: doing actual work with such tables requires highly non-trivial queries, which most of DBMSs cannot optimize because they were never expected to deal with such atrocities.
Such tables also break the very idea of the relational model. Each table must have one-statement "template", for example, CREATE TABLE S (id INTEGER, name TEXT, address TEXT) can have template "A supplier with id ID is called NAME and is located at ADDRESS". Now, each row in this table yields you a true statement about the world when you put its contents into the template: if S would contain a row (1, "Bob & Co.", "Lime St., 125"), then it would tell you that "A supplier with id 1 is called Bob & Co. and is located at Lime St., 125".
All relational operations: selection, projection, joins, filtering, etc. don't just combine tables in a particular way, no! They also combine "templates", so you can say what exactly information a query gives you. And vice versa, if you can express what information you want through combining those "templates", it would almost automatically give you a corresponding query.
And the table you have to work with can be associated with no sane "template". That's why you have to write nonsensical queries in order to get something useful. Note that I can basically guess the "template" of the resulting set,
R1 R2 R3 R4
============================
Local 9 3:55 4:50
...
It's something like "An event of type R2 was happening at place R1, starting at time R3 and ending at time R4". Am I correct?

Resources