I have 375 dialcodes in table "temp":
917277
917278
918083
...
9172738
I can do the following:
select * from cdr where
dnis like 917277% or
dnis like 917278% or
dnis like 918083% or
...
dnis like 9172738%
Is it possible to make a query including "select in" and "like %" conditions?
select * from cdr where dnis in (select dialcode% from temp)
One method is to use exists:
select c.*
from cdr c
where exists (select 1 from temp t where c.dnis like dialcode || '%' );
Note that this does not require distinct, even when there might be multiple matches.
You can use JOIN and LIKE to achieve similiar result:
SELECT c.* -- DISTINCT may be needed if dialcodes overlap each other
FROM cdr c
JOIN temp t
ON c.dnis LIKE t.dialcode || '%'
Related
I'm trying to use subquery inside "like", sth like this:
select *
from message c
where c.content like (select concat('%', value, '%')
from objects o
where o.orderid = '70008090102484');
and it's returning no results. When I copy paste the output of the select instead of it, it returns one row, so I've tried this:
select *
from message c
where c.content like concat('%', '123', '%'); - no results
select *
from message c
where c.content like '%123%';* - 1 result
I've tried with '%' || '123' || '%' and the same result.
Does anybody have any idea why this is happening?
EDIT: "content" field is of type "bytea" - for varchar it works fine, unfortunately, I'm unable to change this field type
Your first query can be phrased using exists in a structurally-similar way:
SELECT *
FROM message c
WHERE EXISTS (
SELECT 1
FROM objects o
WHERE c.content LIKE CONCAT('%', o.value, '%') AND
o.orderid = '70008090102484'
);
I'm trying to write a nested TOP + DISTINCT query in Teradata SQL. My query looks like this:
SELECT TOP 5
*
FROM
(SELECT DISTINCT k_name.KUNDE_NAME1
FROM DB_DWH_MART_AKM_PLT.VW_F_EVENT f_ev
INNER JOIN DBX_DWH_SBX_AKM_PRD.TB_KUNDE_EKP_NAME_AKTUELL k_name ON f_ev.AUFTRAGGEBER_EKP = k_name.EKP
WHERE f_ev.PROCESS_NO = 1075)
I get an error:
Expected something like a name or a Unicode delimited identifier... between ) and ;".
I don't know what I did wrong.
The DISTINCT query would execute correctly on its own.
Why not use a group by?
SELECT TOP 5 k_name.KUNDE_NAME1
FROM DB_DWH_MART_AKM_PLT.VW_F_EVENT f_ev
INNER JOIN DBX_DWH_SBX_AKM_PRD.TB_KUNDE_EKP_NAME_AKTUELL k_name ON f_ev.AUFTRAGGEBER_EKP = k_name.EKP
WHERE f_ev.PROCESS_NO = 1075
GROUP BY k_name.KUNDE_NAME1 --instead of using distinct
ORDER BY k_name.KUNDE_NAME1; --remove or modify as needed
I have this query that joins all the columns I want:
select * from Customer c
inner join Tier t on c.TierId = t.Id
However I don't want all the columns combined. I would like to have a Tier column and then have the full tier record in this column (as I am mapping to json and it has to be in this format).
How do I create subgroup like that?
I tried:
select *, Tier = (select * from Tier t where t.Id = c.TierId ) from Customer c
inner join Tier t on c.TierId = t.Id
can someone point in right direction please.
Edit:
So I want something like this:
- CustomerID
- CustomerName
- CreatedDate
- Tier <-- all tier columns as a subgroup like when you group by
- TierId
- TierName
Are you looking to select just one column from tier? Be explicit about the columns you want:
select t.tier, c.*
from Customer c inner join
Tier t
on c.TierId = t.Id;
It seems like what you need is sql CONCAT Function.
The query would look something like that:
SELECT CONCAT("[", column1, ", ", column2, "]") AS tier FROM Tier
SQLite doesn't support CONCAT so you have to use the || operator like this:
SELECT ("[" || column1 || ", " || column2 || "]") AS tier FROM Tier
The brackets will mark it as an array in JSON, you can remove them if you want.
You can also add the columnname to the result by just adding them as a string to make it more
NOTE: You won't be able to access the values like an Array because this simply merges the values to a String
I am unable to apply a filter(where condition) on the query
SELECT A.Form_Id,
B.CONTAINER_ID,
A.FORM_DESC,
A.FORM_TITLE,
A.LAYOUT,
A.TOTAL_COLUMNS,
COUNT (*) Over () AS Total_Rows
ROW_NUMBER () OVER ( ORDER BY CONTAINER_ID ASC ) ROWNM
FROM FORM_DEFINITION A
LEFT JOIN
(SELECT CONTAINER_ID,FORM_ID FROM FORM_CONTAINER_DEFINITION
) B
ON A.FORM_ID = B.FORM_ID
AND ( ( UPPER(TRIM(A.FORM_ID)) LIKE '%'
|| UPPER(TRIM('FORM2'))
||'%' ) )
In the above code I applied filter like this
( ( UPPER(TRIM(A.FORM_ID)) LIKE '%'
|| UPPER(TRIM('FORM2'))
||'%' ) )
Except this part the query is giving all the info. This filter should show only 'FORM2' row.
But it is showing all the rows as normaly.
.
.
Could you resolve my issue....
.
.
Thanks in advance. :)
Conditions on the first table in a LEFT JOIN need to go in the WHERE clause. On the second table, in the ON clause. Also, the subquery is not necessary. So:
SELECT . ..
FROM FORM_DEFINITION A LEFT JOIN
FORM_CONTAINER_DEFINITION B
ON A.FORM_ID = B.FORM_ID
WHERE UPPER(TRIM(A.FORM_ID)) LIKE '%' || UPPER(TRIM('FORM2')) || '%';
The logic is actually simpler than the above rule. A LEFT JOIN keeps all rows in the first table, regardless of the condition in the ON clause. Matching rows get the values from the second table. Non-matching rows get NULL values.
This is true even when the condition is on the first table.
Also, I would encourage you to use sensible aliases for tables rather than A and B. I would suggest FD and FCD for these two tables.
I currently want to combine two SQL queries into one. This is a bit similar to SQL: Taking the result of of query and using it another - combine. Suppose there are two queries:
SQL Statement
1.) SELECT *
FROM (SELECT B.example1
FROM EXAMPLE1A A
INNER JOIN EXAMPLE1B B ON A.ID = B.ID
WHERE A.ABC ='ABC'
ORDER BY A.ORDER_BY ) as colstate
2.) SELECT colstate
FROM EXAMPLE_TABLE
WHERE EFG LIKE '%'
AND BGTHAN >= '1'
AND SMTHAN <= '100'
ORDER BY ORDER_BY ASC
I want to use the result in query 1.) as the colstate (column statement) in query 2.). But:
What Have I tried is:
SELECT (SELECT B.example1
FROM EXAMPLE1A A
INNER JOIN EXAMPLE1B B
ON A.ID = B.ID
WHERE A.ABC ='ABC'
ORDER BY A.ORDER_BY )
FROM EXAMPLE_TABLE
WHERE EFG LIKE '%'
AND BGTHAN >= '1'
AND SMTHAN <= '100'
ORDER BY ORDER_BY ASC
And it turns out to be Error: Scalar subquery is only allowed to return a single row, how should I replace the "=" into "IN"? Or is my statement totally wrong?
"Combine two queries into one" - that's not a good specs. Try to find out what exactly you want to get as a FLAT 2-dimensional table, think of nested SELECTs as of nested loops where the inner one can only set a single value for parent's row. Like this:
[Outer loop - parent row]
[Inner loop - children rows]
// all you can do here is change a single parent's field to anything
// like constant/sum/avg/topmost/ugly-subquery-returning-a-single-result
[/Inner loop]
[/Outer loop]
The error says that query you are using as column statement must return at most a single row.
It should probably look something like this:
SELECT (SELECT B.example1
FROM EXAMPLE1A A
INNER JOIN EXAMPLE1B B
ON A.ID = B.ID
WHERE A.ABC ='ABC'
AND A.SOME_COLUMN = E.SOMECOLUMN // retrieve only relevant data for this row
ORDER BY A.ORDER_BY )
FROM EXAMPLE_TABLE E
WHERE EFG LIKE '%'
AND BGTHAN >= '1'
AND SMTHAN <= '100'
ORDER BY ORDER_BY ASC