This question already has answers here:
Unpivot, Split Column, Pivot Bigquery/Postgresql
(2 answers)
Closed 10 days ago.
i have a table SQL similar like this picture:
i want to change the table into something like this, based on their group type.
I'm still have an issue how to change the table, to adding type "attribution / immediate" can somebody suggest me how to do that? Thank you!
Simple variant, to "pivot/unpivot" by auxiliary cte or subquery, applicable if a fixed set of additional columns is specified
with types as( select 'attribution' typeX union all select 'immidate'
)
select device,typeX
,case when b.typeX='attribution' then session_attribution
when b.typeX='immidate' then session_immidate
else null
end session
,case when b.typeX='attribution' then order_attribution
when b.typeX='immidate' then order_immidate
else null
end orderX
from tableA a join types b on 1=1
order by typeX,device
JOIN on condition like 1=1 my be replaced by CROSS APPLY.
Using names similar to reserved words - practice leading to syntactic and semantic errors. In example changer type->typeX, order->orderX.
Related
I need help to understand what I did wrong ... I'm a beginner so excuse me the simple question!
I have two tables in which I want to do a JOIN where, in one of the columns I had to use REPLACE to remove the text 'RIxRE' that does not interest me.
In table 1, this is the original text of the column id_notification: RIxRE-1787216-BSB and this is the text that returns when using REPLACE: 1787216-BSB
In column 2, this is the text that exists: 1787216-BSB
However, I get the following error:
# 1054 - Unknown column 'a.id_not' in 'on clause'
SELECT *, REPLACE(a.id_notificacao,'RIxRE','') AS id_not
FROM robo_qualinet_cadastro_remedy a
JOIN (SELECT * FROM painel_monitoracao) b ON a.id_not = b.id_notificacao
You cannot use a column alias again in the FROM clause or the WHERE clause after the SELECT (and possibly not other clauses as well, depending on the database).
So, repeat the expression:
SELECT *, REPLACE(a.id_notificacao, 'RIxRE', '') AS id_not
FROM robo_qualinet_cadastro_remedy rqcr JOIN
painel_monitoracao pm
ON REPLACE(rqcr.id_notificacao, 'RIxRE', '') = pm.id_notificacao;
Notes:
Use table aliases the mean something, such as abbreviations for the able names.
The subquery is not necessary in the FROM clause.
I suspect that you have a problem with your data model if you need a REPLACE() for the JOIN condition, but that is a different issue from this question.
I am currently working with a MS SQL database on Windows 2012 Server
I need to query only 1 column from a table that I only have access to read, not make any kind of changes.
Problem is that the name of the column is "Value"
My code is this:
SELECT 'Value' FROM table
If I add
`ORDER BY 'Value'`
The issue is that the query is returning an empty list of results.
Things I've tried already
I tried replacing ' with `"' but this didn't work either.
I also tried writing SELECT * instead of SELECT VALUE
Using the table name in the SELECT or ORDER clauses again didn't help
You are claiming that this query:
SELECT 'Value'
FROM table
ORDER BY 'Value'
Is returning no rows. That's not quite correct. It is returning an error because SQL Server does not allow constant expressions as keys for ORDER BY (or GROUP BY for that matter).
Do not use single quotes. In this case:
SELECT 'Value' as val
FROM table
ORDER BY val;
Or, if value is a column in the table:
SELECT t.Value
FROM table t
ORDER BY t.Value;
Value is not a reserved word in SQL Server, but if it were, you could escape it:
SELECT t.[Value]
FROM table t
ORDER BY t.[Value];
it looks like your table has null values. and because of the order by all null values come first.
try to add filter like this
select Value FROM table
where Value is not null and Value <> ''
order by Value
I would like to know if is it possible to use the clause "with as" with a variable and/or in a block begin/end.
My code is
WITH EDGE_TMP
AS
(select edge.node_beg_id,edge.node_end_id,prg_massif.longueur,prg_massif.lgvideoupartage,prg_massif.lgsanscable from prg_massif
INNER JOIN edge on prg_massif.asset_id=edge.asset_id
where prg_massif.lgvideoupartage LIKE '1' OR prg_massif.lgsanscable LIKE '1')
,
journey (TO_TOWN, STEPS,DISTANCE,WAY)
AS
(SELECT DISTINCT node_beg_id, 0, 0, CAST(&&node_begin AS VARCHAR2(2000))
FROM EDGE_TMP
WHERE node_beg_id = &&node_begin
UNION ALL
SELECT node_end_id, journey.STEPS + 1
, journey.DISTANCE + EDGE_TMP.longueur,
CONCAT(CONCAT(journey.WAY,';'), EDGE_TMP.node_end_id
)
It create a string as output separated by a ; but i need to get it back as variable or table do you know how? I used a concat to retrieve data in a big string. Can i use a table to insert data
,
A need to use the result to proceed more treatment.
Thank you,
mat
No, WITH is a part of an SQL statement only. But if you describe why you need it in pl/sql, we'll can advice you something.
Edit: if you have SQL statement which produces result you need, you can assign it's value to pl/sql variable. There are several methods to do this, simpliest is to use SELECT INTO statement (add INTO variable clause into your select).
You can use WITH clause as a part of SELECT INTO statement (at least in not-too-very-old Oracle versions).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do you return all possibilities, whether there is content or it is NULL?
If I want to return everything that isn't NULL:
SELECT * FROM table WHERE column LIKE '%'
And if I want to return all NULLs:
SELECT * FROM table WHERE column IS NULL
How do I combine them both? I need to be able to because I am parameterizing it. The front end of my application will have multiple options ALL (any content or NULL) or a specific value.
How can I achieve this?
EDIT:
Let me clarify better. I have a dropdown List that will show things like this
-Select All-
Team A
Team B
...
So if -Select All- is selected then I need the query to return all NULLs and those with any Team
If Team A is selected I need to show only Team A and no NULLs and so on...
I cant change the query just a single variable (parameter)
WHERE column LIKE '%' OR column IS NULL
Assuming NULL as the parameter value means "All"
WHERE Team = #Team OR #Team IS NULL
Unless you are on 2008+ and use OPTION (RECOMPILE) this can give sub optimal plans though.
See Dynamic Search Conditions in T-SQL
It's fairly straightforward. To only get NULLS:
SELECT * FROM table
WHERE column IS NULL
To only get NOT NULLS:
SELECT * FROM table
WHERE column IS NOT NULL
-- could be this if your example is not representative
-- WHERE column IS NULL OR column LIKE '%whatever%'
And for everything (no filter), just do:
SELECT * FROM table
Further clarification:
In your example, if the code is already written and you can only pass in the WHERE clause then you could do this:
WHERE <insert here>
column IS NULL -- just nulls
column = 'teamX' OR column IS NULL -- nulls or 'teamX'
column IS NOT NULL -- any value, but no nulls
1=1 -- for the case where you don't really want a WHERE clause. All records
It doesn't sound like this is the best way of structuring your code, but if you are already restricted by something that can't be changed, I guess you have to make do.
If I understood your question then this is what you are looking for
SELECT *
FROM table
WHERE column LIKE '%' or column Is null
Does select * from table get you what you want?
Perhaps you mean "parameter can be passed or parameter can be NULL" ?
If so then something like this should do the trick
SELECT * FROM table WHERE param IS NULL OR column LIKE '%' + param '%'
Similarly if parameter passed keyword 'ALL' that means "select everything" it would be
SELECT * FROM table WHERE param = 'All' OR column LIKE '%' + param '%'
One case is not to use any where clauses.
Or else you can use or condition
where (column is null or column like '%something%')
Try something like this:
SELECT * FROM table WHERE column LIKE '%'
Union
SELECT * FROM table WHERE column IS NULL
It will combine both query if they have the same column name and number, since the only difference is where clause, it should be working
This question already has answers here:
Is there a combination of "LIKE" and "IN" in SQL?
(28 answers)
Closed 9 years ago.
Is it possible to combine LIKE and IN in a SQL Server-Query?
So, that this query
SELECT * FROM table WHERE column LIKE IN ('Text%', 'Link%', 'Hello%', '%World%')
Finds any of these possible matches:
Text, Textasd, Text hello, Link2, Linkomg, HelloWorld, ThatWorldBusiness
etc...
Effectively, the IN statement creates a series of OR statements... so
SELECT * FROM table WHERE column IN (1, 2, 3)
Is effectively
SELECT * FROM table WHERE column = 1 OR column = 2 OR column = 3
And sadly, that is the route you'll have to take with your LIKE statements
SELECT * FROM table
WHERE column LIKE 'Text%' OR column LIKE 'Hello%' OR column LIKE 'That%'
I know this is old but I got a kind of working solution
SELECT Tbla.* FROM Tbla
INNER JOIN Tblb ON
Tblb.col1 Like '%'+Tbla.Col2+'%'
You can expand it further with your where clause etc.
I only answered this because this is what I was looking for and I had to figure out a way of doing it.
One other option would be to use something like this
SELECT *
FROM table t INNER JOIN
(
SELECT 'Text%' Col
UNION SELECT 'Link%'
UNION SELECT 'Hello%'
UNION SELECT '%World%'
) List ON t.COLUMN LIKE List.Col
No, you will have to use OR to combine your LIKE statements:
SELECT
*
FROM
table
WHERE
column LIKE 'Text%' OR
column LIKE 'Link%' OR
column LIKE 'Hello%' OR
column LIKE '%World%'
Have you looked at Full-Text Search?
No, MSSQL doesn't allow such queries. You should use col LIKE '...' OR col LIKE '...' etc.