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
Related
How to combine these 2 sets of code?
Tables: geregistreerd g, instrument i and indeling id.
I want to display g.voornaammuzikant, g.achternaammuzikant, i.naaminstrument, id.familie.
Table g is linked with table i with instrumentid
Table i is linked with table id with indelingid
--oefening 1:
SELECT TRIM(' ' FROM g.voornaammuzikant), TRIM(' ' FROM g.achternaammuzikant),
(SELECT i.naaminstrument
from instrument i
where i.instrumentid = g.instrumentid) as "naam instrument"
from geregistreerd g
order by g.voornaammuzikant;
--oefening 2:
SELECT i.naaminstrument,
(SELECT id.familie
from indeling id
where i.indelingid = id.indelingid) as "familie",
(SELECT id.onderfamilie
from indeling id
where i.indelingid = id.indelingid) as "onderfamilie"
from instrument i
order by i.naaminstrument;
COMBINED
SELECT TRIM(' ' FROM g.voornaammuzikant),
TRIM(' ' FROM g.achternaammuzikant),
(SELECT i.naaminstrument
(SELECT id.familie
from indeling id
where i.indelingid = id.indelingid) as "familie"
from instrument i
where i.instrumentid = g.instrumentid) as "naam instrument"
from geregistreerd g
order by g.voornaammuzikant;
Why would you want to do things in a complicated way, if they can be simplified? What's wrong with a simple join? Something like this:
select g.voornaammuzikant,
g.achternaammuzikant,
i.naaminstrument,
id.familie
from geregistreerd g join instrument i on i.instrumentid = g.instrumentid
join indeling id on id.indelingid = i.indelingid
order by g.voornaammuzikant;
By the way, those TRIMs you posted don't look good. SELECT TRIM(' ' FROM g.voornaammuzikant) is certainly wrong; I don't know what you meant to say with that. Perhaps removing superfluous spaces from those values? If they exist, is - by any chance - that column's datatype CHAR? If so, consider switching to VARCHAR2 because the former pads values with spaces up to the whole length of that column.
I am doing a join between two tables A and B on A.Item = B.Item. I am not getting the records as expected. After doing some investigations, I saw that all the items in table B contains nulls at the end of the item.
I would like to be able to do something like:
SELECT * FROM A INNER JOIN B ON TRIMNULL(A.ITEM) = TRIMNULL(B.ITEM);
Is there any such method in AS400 to trim the null values?
Take a look at the TRIM function in the manual. You can specify a character to trim.
If assuming you mean a hex x'00' when you say NULL. Then this should work:
SELECT *
FROM A INNER JOIN B
ON TRIM(TRAILING x'00' FROM A.ITEM)
= TRIM(TRAILING x'00' FROM B.ITEM);
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 || '%'
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
Can anyone tell me the exact syntax for NOT IN condition in SQL on two columns.
This is my query written in VBA.
strNewSql = "SELECT distinct(tblRevRelLog_Detail.PartNumber), tblRevRelLog_Detail.ChangeLevel, tblRevRelLog_Detail.ID FROM tblRevRelLog_Detail LEFT JOIN tblEventLog ON tblRevRelLog_Detail.PartNumber = tblEventLog.PartNumber"
strNewSql = strNewSql & " WHERE (tblEventLog.PartNumber) Not In(SELECT tblEventLog.PartNumber FROM tblEventLog WHERE tblEventLog.EventTypeSelected = 'pn REMOVED From Wrapper') AND tblEventLog.TrackingNumber = """ & tempTrackingNumber & """ AND tblEventLog.TrackingNumber = tblRevRelLog_Detail.RevRelTrackingNumber;"
I want to change this sub query like, it should apply on the combination of two columns as follows:
strNewSql = "SELECT tblRevRelLog_Detail.PartNumber, tblRevRelLog_Detail.ChangeLevel, tblRevRelLog_Detail.ID FROM tblRevRelLog_Detail LEFT JOIN tblEventLog ON tblRevRelLog_Detail.PartNumber = tblEventLog.PartNumber"
strNewSql = strNewSql & " WHERE (((tblEventLog.PartNumber, tblEventLog.PartNumberChgLvl) Not In(SELECT tblEventLog.PartNumber,tblEventLog.PartNumberChgLvl FROM tblEventLog WHERE tblEventLog.EventTypeSelected = 'pn REMOVED From Wrapper') AND tblEventLog.TrackingNumber = """ & tempTrackingNumber & """ AND tblEventLog.TrackingNumber = tblRevRelLog_Detail.RevRelTrackingNumber);"
But this is not working.....
You can't use IN with more than one column but you can usually achieve the same effect using EXISTS:
SELECT *
FROM tbl1
WHERE NOT EXISTS
(
SELECT *
FROM tbl2
WHERE tbl2.col1 = tbl1.col1
AND tbl2.col2 = tbl1.col2
)
General syntax:
where col not in (items)
Items can be
a list of items -- (4,5,3,5,2) or ('243','3','cdds') or any other datatype.
Or a select statement (select hatefulthings from table)
Addition 6 years later
All major platforms support tuples with NOT IN, for example
SELECT *
FROM empoyee
WHERE (empID, #date) NOT IN
(SELECT empID, vacationDay
FROM empVacation
)
In this example we select everything from the employee table where the tuple of employee id and date are not in a table containing vacation days.
Your question is a bit unclear. Is this what you need?
SELECT *
FROM
MY_TABLE MT
WHERE 'Smith' NOT IN (MT.FIRST_NAME)
AND 'Smith' NOT IN (MT.LAST_NAME)
This will show you all records where the search phrase ("Smith") is in neither the first_name nor the last_name column.
Perhaps you meant
SELECT *
FROM MY_TABLE
WHERE (FIRST_NAME, LAST_NAME) NOT IN (SELECT FIRST_NAME, LAST_NAME
FROM SOME_OTHER_TABLE)
This is allowed under Oracle - not sure about SQL Server.
Share and enjoy.
Select * from some_table
where (((Some_Value) Not IN (Select Column1 & Column2 from Some_Other_Table)));
I saw where you indicated this was for Access
It is not clear what you are asking, but I gather that you would like to have a NOT IN condition based on two columns instead of just one.
As an example, lets say you have a column called F_Name and another called L_Name (both variable in size), and that you want to exclude specific combination of those names from another table that has them already combined as NAME. In that case, you could do this:
select F_name
, L_name
, col1
, coln
from mytable1
where F_name -- First name (variable length)
|| ' ' -- appended to a blank space
|| L_name -- appended to the last name (v)
not in -- is not one of these names
( select name
from mytable2
where ...
)
The main issue with this query is that you must get the formatting just right so that they can match up exactly.
If you are dealing with a combination of fields with different types, like numeric and timestamps, then use any of the conversion commands (DECIMAL, INTEGER, CHAR, SUBSTR ...) at your disposal to convert to text equal and then match it up accordingly.