I am running below query in Teradata editor:
SELECT 'EMP_INFO_MAIN' as TABLE_NAME, COUNT(1) as RECORD_COUNT FROM SCHEMA.TABLE1 UNION ALL
SELECT 'EMP_SAL' as TABLE_NAME, COUNT(1) as RECORD_COUNT FROM SCHEMA.TABLE2 UNION ALL
SELECT 'DEPARTMENT_INFO' as TABLE_NAME, COUNT(1) as RECORD_COUNT FROM SCHEMA.TABLE3;
The query is giving me below result:
TABLE_NAME | RECORD_COUNT
------------|-------------
EMP_INFO | 10
EMP_SAL | 11
DEPARTME | 110
The first column is not showing complete table name.
Can anyone please help here?
This is a common issue.
In Teradata the first SELECT of a UNION determines the resulting data typ and the column name, so either change the order of SELECTs to start with the longest name or add a CAST in the 1st SELECT:
SELECT CAST('EMP_INFO_MAIN' AS VARCHAR(20)) as TABLE_NAME, COUNT(1) as RECORD_COUNT FROM SCHEMA.TABLE1 UNION ALL
SELECT 'EMP_SAL' as TABLE_NAME, COUNT(1) as RECORD_COUNT FROM SCHEMA.TABLE2 UNION ALL
SELECT 'DEPARTMENT_INFO' as TABLE_NAME, COUNT(1) as RECORD_COUNT FROM SCHEMA.TABLE3;
Related
In DB2 is there a way to basically say:
case when sku (select * from table1 where tb1field = 'SMOMD') then 'True' end
Okay so this is my query so far, I've been going at this for at least a month now so any help would be great.
select tb4.customer, tb4.sku, tb4.qty, tb4.retqty, tb4.stipqty, tb4.lastdate, tb4.firstdate, tb4.stipdate
from(
--Table 4
select tb3.Customer as Customer, tb3.sku as SKU, tb3.qty as Qty, tb3.retqty as RetQty, tb3.stipqty as STIPQty,
case when tb3.lastdate is null then '00/0000' else substr(tb3.lastdate,5,2)||'/'||substr(tb3.lastdate,1,4) end as LastDate,
case when tb3.firstdate is null then '00/0000' else substr(tb3.firstdate,5,2)||'/'||substr(tb3.firstdate,1,4) end as FirstDate,
case when tb3.stipdate is null then '00/0000' else substr(tb3.stipdate,5,2)||'/'||substr(tb3.stipdate,1,4) end as STIPDate
from(
--Table 3
select tb2.Customer as Customer, tb2.SKU as SKU, tb2.Qty as Qty, tb2.RetQty as RetQty, tb2.STIPQty as STIPQty,
max(case when tb2.TranID in ('010','100') then tb2.datenum end) as LastDate,
min(case when tb2.TranID in ('010','100') then tb2.datenum end) as FirstDate,
case when tb2.RC = '4M' then tb2.datenum end as STIPDate
from(
--Table 2
select tb1.Customer as Customer, tb1.SKU as SKU,
sum(case when tb1.TranID in ('010','100') then abs(tb1.OrdNet) else '0' end) as Qty,
sum(case when tb1.TranID = '500' and tb1.rc != '4M' then abs(tb1.OrdNet) else '0' end) as RetQty,
count(case when tb1.rc = '4M' then tb1.sku end) as STIPQty,
tb1.datenum as datenum, tb1.TranID as tranid, tb1.RC as rc
from(
--Table 1
select distinct stkund as Customer, sthptg||space(1)||stmodl||space(1)||stvari||space(1)||stfarb||space(1)||stgroe as SKU,
stvorg as TranID, stggru as RC, stprg09 as PG9, stprg08 as PG8, stperi as datenum, ormne1 as OrdNet
from st_usus.s_stati_pv
join caspdtau.cospf440 on stadrn = jadr40
where trim(stvert) in ('111S','122S')
and sthptg != 'V'
and aktv40 = 'A'
and stprg01 in ('01','04')
and stprg02 = '01'
and stvorg in ('500','010','100')
and stperi >= '20160100'
) as tb1
group by tb1.Customer, tb1.SKU, tb1.datenum, tb1.tranid, tb1.rc
) as tb2
group by tb2.customer, tb2.sku, tb2.qty, tb2.retqty, tb2.stipqty, tb2.tranid, tb2.rc, tb2.datenum
) as tb3
group by tb3.customer, tb3.sku, tb3.qty, tb3.retqty, tb3.stipqty, tb3.lastdate, tb3.firstdate, tb3.stipdate
) as tb4
order by tb4.Customer, tb4.sku
I'm not going to try to decipher exactly what you're trying to do...
Some general advice, rather than using Nested Table Expressions (NTE)
select <..> from (select <...>from mytable)
Consider Common Table Expressions (CTE)
with
table1 as (select <...> from st_usus.s_stati_pv join caspdtau.cospf440 on stadrn = jadr40)
, table2 as (select <...> from table1)
, table3 as (select <...> from table2)
, table4 as (select <....> from table3)
select <...> from table4;
Each CTE (ie. tableX) can refer to a prior CTE or a physical table/view as needed. The final select can refer to one or more CTE's along with one or more physical tables or views.
Nice thing about building with CTE's, is that you can check your results after each step..
with
table1 as (select <...> from st_usus.s_stati_pv join caspdtau.cospf440 on stadrn = jadr40)
select * from table1;
I would like to get all values in x that do not exists in table TableA.
I've tried following queries that is returning blank.
select x.num from
(
select '888888' as num from dual union all
select '111111' as num from dual
) x
left outer join TableA a on (a.number = x.num)
where a.number is null
select x.num
FROM
(
select '888888' as num from dual union all
select '111111' as num from dual
) x
where x.num not in
(
select a.number from TableA a where x.num = a.number
)
Table
| TableA.number |
-------------
111111
333333
The expected result is that only '888888' is returned in this case.
I can't see why this shouldn't work, where have i've done wrong?
Try this
with cte as
(
select '888888' as num from dual union all
select '111111' as num from dual
)
select cte.num
from cte
left join TableA a on (a.number = cte.num)
where a.number is null
I want to find the distinct pairs of names in the table which have the same exact items in the items column. For instance:
CREATE TABLE t
(
name VARCHAR(255),
item VARCHAR(255)
);
INSERT INTO t VALUES("Alice", "Orange");
INSERT INTO t VALUES("Alice", "Pear");
INSERT INTO t VALUES("Alice", "Lemon");
INSERT INTO t VALUES("Bob", "Orange");
INSERT INTO t VALUES("Bob", "Pear");
INSERT INTO t VALUES("Bob", "Lemon");
INSERT INTO t VALUES("Charlie", "Pear");
INSERT INTO t VALUES("Charlie", "Lemon");
The answer here would be Alice,Bob because they took the exact same items.
I want to do it with double negation (using NOT EXISTS/NOT IN) only which I think is more well-suited to this question, but I couldn't come up with anything that is remotely close to being functional.
This is somewhat similar to this question but I'm using SQLite so I cannot use GROUP_CONCAT() but I was wondering how it would be done using relational division using NOT EXISTS/NOT IN.
To get the number of common items between all pairs of names you can use the following query:
SELECT t1.name AS name1, t2.name AS name2, COUNT(*) AS cnt
FROM t AS t1
INNER JOIN t AS t2 ON t1.item = t2.item AND t1.name < t2.name
GROUP BY t1.name, t2.name
Output:
name1 name2 cnt
------------------------
Alice Bob 3
Alice Charlie 2
Bob Charlie 2
Now all you want is to filter out (name1, name2) pairs having a count that is not equal to the number of items of name1 and name2. You can do this using a HAVING clause with correlated subqueries:
SELECT t1.name AS name1, t2.name AS name2
FROM t AS t1
INNER JOIN t AS t2 ON t1.item = t2.item AND t1.name < t2.name
GROUP BY t1.name, t2.name
HAVING COUNT(*) = (SELECT COUNT(*) FROM t WHERE name = t1.name) AND
COUNT(*) = (SELECT COUNT(*) FROM t WHERE name = t2.name)
Demo here
With compound queries:
SELECT t1.name, t2.name
FROM t AS t1, t AS t2
GROUP BY t1.name, t2.name
HAVING t1.name < t2.name
AND NOT EXISTS (SELECT item FROM t WHERE name = t1.name
EXCEPT
SELECT item FROM t WHERE name = t2.name)
AND NOT EXISTS (SELECT item FROM t WHERE name = t2.name
EXCEPT
SELECT item FROM t WHERE name = t1.name);
Using NOT IN is possible, bit expresses exactly the same mechanism with more complexity:
SELECT t1.name, t2.name
FROM t AS t1, t AS t2
GROUP BY t1.name, t2.name
HAVING t1.name < t2.name
AND NOT EXISTS (SELECT item
FROM t
WHERE name = t1.name
AND item NOT IN (SELECT item
FROM t
WHERE name = t2.name))
AND NOT EXISTS (SELECT item
FROM t
WHERE name = t2.name
AND item NOT IN (SELECT item
FROM t
WHERE name = t1.name));
This seems to be working with SQLLite
select t1.name
from t t1
join t t2 on t1.name <> t2.name and t1.item = t2.item
join (select name, count(*) as cnt from t group by name) t3 on t3.name = t1.name
join (select name, count(*) as cnt from t group by name) t4 on t4.name = t2.name
group by t1.name, t3.cnt, t4.cnt
having count(*) = max(t3.cnt, t4.cnt)
I might have found a solution to your issue. Mine was tested using MySQL, but it's not using GROUP_CONCAT(). It might work for your SQLite database. My query is used to find people who have bought the same exact items.
Try using this statement:
SELECT DISTINCT e1.name, e2.name from t e1, t e2 WHERE e1.item=e2.item AND e1.name != e2.name GROUP BY e1.item HAVING count(*) >1;
https://gyazo.com/5e5e9d0ddfb33cb47439a674297108ed
I have tables - FAKE_CUST, PRE_CUST, NORMAL_CUST. Based on the constraint present in FAKE_CUST table, the value has to be brought to either PRE_CUST or NORMAL_CUST.
I am using the follwing code:
INSERT ALL
INTO PRE_CUST(CUST_ID, TOTAL_COST_TRANS) (SELECT DISTINCT CUST_ID,C FROM (
SELECT CUST_ID, SUM(COST_TRANS) OVER (PARTITION BY CUST_ID) as C FROM FAKE_CUST) WHERE C>1000)
INTO NORMAL_CUST(CUST_ID, TOTAL_COST_TRANS) (SELECT DISTINCT CUST_ID,C FROM (
SELECT CUST_ID, SUM(COST_TRANS) OVER (PARTITION BY CUST_ID) as C FROM FAKE_CUST) WHERE C<1000)
SELECT 1 FROM DUAL;
The syntax which you have used for multi-table insert is not correct to start with.
The WHEN clause, which evaluates the condition for insert, has to come before insert_intoclause.
Your query should look like this:
INSERT ALL
WHEN c > 1000 THEN
INTO pre_cust (cust_id, total_cost_trans)
WHEN c < 1000 THEN
INTO normal_cust (cust_id, total_cost_trans)
SELECT CUST_ID, SUM(COST_TRANS) OVER (PARTITION BY CUST_ID) as C
FROM FAKE_CUST
**CODE** **TYPE**
--------------------
XXOPT POD
XXOPT FPOD
MSC OPR
KLM OPR
40DV SZTP
90DV SZTP
MMVD POD
KKLP FPOD
SSRG FPOD
I HAVE A DATA TABLE COMING FROM DATA BASE
I WANT TO SHOW MY RESULT LIKE FOLLOWING FORMAT
NOTE: HERE POD,FPOD,OPR,SZTP ARE STATIC TYPES
POD: XXOPT,MMVD
FPOD: XXOPT,KKLP,SSRG
OPR: MSC,KLM
SZTP: 40DV, 90DV
WILL YOU PLESSE HELP ME TO SHOW ABOVE FORMAT AS OUT PUT.
DECLARE #MyTable TABLE(
[CODE] NVARCHAR(50) NOT NULL,
[TYPE] NVARCHAR(50) NOT NULL
);
INSERT #MyTable ([CODE],[TYPE])
SELECT 'XXOPT','POD'
UNION ALL SELECT 'XXOPT','FPOD'
UNION ALL SELECT 'MSC','OPR'
UNION ALL SELECT 'KLM','OPR'
UNION ALL SELECT '40DV','SZTP'
UNION ALL SELECT '90DV','SZTP'
UNION ALL SELECT 'MMVD','POD'
UNION ALL SELECT 'KKLP','FPOD'
UNION ALL SELECT 'SSRG','FPOD';
SELECT x.[TYPE],y.GROUP_CONCAT
FROM (SELECT [TYPE] FROM #MyTable GROUP BY [TYPE]) x
CROSS APPLY(
SELECT STUFF((SELECT ','+y.CODE
FROM #MyTable y
WHERE y.[TYPE]=x.[TYPE]
FOR XML PATH('')),1,1,'') AS GROUP_CONCAT
)y;
Results:
TYPE GROUP_CONCAT
---- ---------------
FPOD XXOPT,KKLP,SSRG
OPR MSC,KLM
POD XXOPT,MMVD
SZTP 40DV,90DV
Note: In this case you need an index on ([TYPE])+INCLUDE([CODE]) or an index on ([TYPE],[CODE]).