I have a SQL Table like this one:
and I want the output to be like this:
Basically:
replace duplicates with blanks but
if col6 value is different from the previous row for the same
col1
value, all the data fields should be included.
col10 values are blank.
col12 is removed.
I am struggling to create a query.
I found this: CTE answer
and tried to run the following:
;WITH CTE
AS
(
SELECT DBA.s12.*,
ROW_NUMBER() OVER(PARTITION BY DBA.s12.col6 ORDER BY(SELECT 1)) rownum
FROM DBA.s12
)
SELECT
DBA.s12.col1,
DBA.s12.col2,
DBA.s12.col3,
DBA.s12.col4,
DBA.s12.col5,
DBA.s12.col7,
DBA.s12.col8,
DBA.s12.col9,
DBA.s12.col10,
DBA.s12.col11,
DBA.s12.col12,
CASE rownum
WHEN 1 THEN DBA.s12.col6
ELSE ''
END AS col6
FROM CTE
ORDER BY DBA.s12.col1;
but I get an error "Could not execute statement. Syntax error or access violation"
Can anyone shed some light on where my query has a syntax error/access violation or have a better method to extract the data?
I am not entirely sure if this will work, but maybe try to replace
END AS col6
with
END AS DBA.s12.col6
Related
I am working with some SQL queries on DB2. Is it possible to select all the columns in a table and also specify certain conditions using the "as" keyword within that select statement? For example, is this query possible:
select
*,
col1 + col2 as sum1,
col3 - col4 as dif1
from
table;
Whenever I attempt this, I am getting the SQL0104 error and it is saying "Token , was not valid. Valid tokens: FROM INTO".
Thank you for your help.
EDIT:
This query is running inside an SQLRPGLE program on an AS400.
Put your table alias in front of the *. So:
select
t.*,
col1 + col2 as sum1,
col3 - col4 as dif1
from
table t;
I am working on Ingres9.2 version. When I execute a query like
select col1 from table1 group by col1 having not((5=min(col1)) and (0 = 1))
it raising an error as:
bad select or subselect target list has been found.
But the error not occurring if I change the query with any one of following conditions:
0=0 or 1=1 instead of 0=1.
Removing not.
Using or instead of and.
Removing min.
I am not able to find the reason for this behaviour. And the same error not occurring in other database which is also in Ingres. If anyone knows the reason, please explain it.
Ingres is an old database. Maybe it has a problem with using an aggregation function on a group by key. This is speculation, but this should work:
select col1
from table1
where not (5 = col1) and (0 = 1))
group by col1 ;
Of course, the condition would be more likely written as col1 <> 5, but the two forms are equivalent.
I'm not too sure how to describe my SQL Insert statement so I will describe the expected result.
I'm building a data extract list and have a table that I've put all my data into. It's called _MATTER_LIST
What I am trying to Achieve is to have the Client_Number + Col1 combination repeat after every unique COL1+COL2+COL3 combination but not duplicate when there is already a CLIENT_NUMBER+COL1. So the end result would be:
thanks in advance for any tips.
Simple ORDER BY should work for you if i understand. Try this :
select Client_Number, Col1, Col2, Col3 from _MATTER_LIST
order by Client_Number, Col1
I've managed to fix my own issue. I added a unique key for the col1 + col2 + col3 , then make col2 repeat over each combination for example.
The result is: select * from _MATTER_LIST order by COL4, COL5
I have a query
SELECT col1, Val(col2)
FROM table1;
where col2 is a text data type.
I want to use DISTINCT here,
SELECT DISTINCT col1, Val(col2)
FROM table1;
but when I add it, I have an error
"Data type mismatch in criteria expression".
I have the same error when I try to sort the column2 (for the first query). Why?
The Val function doesn't process NULL values. Change your second column to Val(nz(col2,""))
I'm answering my question.
Because some of the rows have NULL value in col2 and SQL cannot compare two NULLs to find distinct rows, one should add WHERE col2 IS NOT NULL.
SELECT DISTINCT col1, Val(col2)
FROM table1
WHERE col2 IS NOT NULL;
I have 10,001 rows in my table, and all of the rows except one start with a number. I need to find this one row that doesn't start with a number, or even that doesn't contain a number.
So this is what I have:
Select col1 from table1 where col1 not like '?%'
Is this even close? I need to find the row that doesn't have a number...
Thanks!!
UPDATE: I am using a sqlite database
Use:
SELECT col1
FROM table1
WHERE SUBSTR(col1, 1, 1) NOT BETWEEN 0 AND 9
Reference:
core functions (incl SUBSTR)
LIKE
On Sql Server,
Select * From table
Where col1 Like '[^0-9]%'
EDIT: Don't know if there is an equivilent on SQLLIte,
but this will work...
Select * From table
Where col1 Not Like '0%'
And col1 Not Like '1%'
...
And col1 Not Like '9%'
There is a post in code project that allow you to use Regex with Ms SQL.
Hope this help.
The easiest way might be to just note that you're not using numbers; you're using strings that happen to have numbers in them. In this case, you can do
select * from table1 where col1 not between '0' and '9:';
(where the colon is the ASCII character after '9'; this way '9999999' won't be found).
It should be a lot less expensive than some of the other suggestions (e.g., checking the value of the first character).
#Dueber got me thinking, couldn't you just do this?
SELECT * FROM table1 WHERE col1 > '9'
Grab the first character, see if it's numeric.
SELECT *
FROM table1
WHERE ISNUMERIC(SUBSTRING(col1,1,1)) = 0
SUBSTRING
ISNUMERIC