First question...
I've been researching this site and found a SQL that should help me...but I'm getting an error that I can't solve. Find below SQL and Error:
SELECT field1,
Sum(IIf(status = "Accepted", 1, 0)) AS [field1_Accepted]
Sum(IIf(status = "Rejected", 1, 0)) AS [field1_Rejected]
Sum(IIf(status = "Cancelled", 1, 0)) AS [field1_Cancelled]
FROM tbl1
GROUP BY field1;
Error: The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect. (Error 3141)
My expectations from this query is this one:
field1/accepted/rejected/cancelled
a/1/2/3
b/2/3/5
c/2/3/4
Letters should be my fld1 names and the other numbers should be a count on how much field has accepted,rejected and cancelled status...
Expressions in the SELECT statement need to be separated by commas. You are missing commas between the column expressions:
SELECT field1,
Sum(IIf(status = "Accepted", 1, 0)) AS [field1_Accepted], -- <<== Here
Sum(IIf(status = "Rejected", 1, 0)) AS [field1_Rejected], -- <<== Here
Sum(IIf(status = "Cancelled", 1, 0)) AS [field1_Cancelled]
FROM tbl1
GROUP BY field1;
Related
I'm getting the following no matter what I do any help would be awesome.
Msg 116, Level 16, State 1, Line 15
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Msg 109, Level 15, State 1, Line 1
There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
My query
[tableA].[PROJECTID],
[tableA].[STUDYID],
[tableA].[SUBJNO],
[tableA].[CASENUMBER],
[tableA].[CASESTATUS],
[tableA].[MODIFIEDBY]
)VALUES((
SELECT b.PROJECTID,
((SELECT TOP 1 a.STUDYID FROM [PRODVIEW] a WHERE a.DYNAME = b.DYNAME and
a.ProjID = b.PROJID)) as STUDYID,
b.SUBJNO,
(b.SUBJNO + '_' + b.SEQUENCE) as CaseNumber,
'READY' as CASESTATUS,
b.UPLOADEDBY
FROM [dbo].[TableB] b WHERE VIEWED = 0
AND b.UPLOADEDDATE >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)))
If you want to use a SELECT as the source of the data for an INSERT, then don't use VALUES, which is for inserting literal data:
INSERT INTO yourTable ([PROJECTID], [STUDYID], [SUBJNO], [CASENUMBER], [CASESTATUS],
[MODIFIEDBY])
SELECT
b.PROJECTID,
(SELECT TOP 1 a.STUDYID FROM [PRODVIEW] a
WHERE a.DYNAME = b.DYNAME and a.ProjID = b.PROJID),
b.SUBJNO,
(b.SUBJNO + '_' + b.SEQUENCE),
'READY',
b.UPLOADEDBY
FROM [dbo].[TableB] b
WHERE
VIEWED = 0 AND
b.UPLOADEDDATE >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0);
There is probably a way to write your query without using a correlated subquery in the select clause, e.g. via a join. Actually, your subquery with TOP makes no sense because there is no ORDER BY clause.
Also note that you don't need to use aliases in the SELECT statement. In fact, they will just be ignored, since the INSERT determines the target columns.
Is there a way to convert the first letter uppercase in Oracle SQl without using the Initcap Function?
I have the problem, that I must work with the DISTINCT keyword in SQL clause and the Initcap function doesn´t work.
Heres is my SQL example:
select distinct p.nr, initcap(p.firstname), initcap(p.lastname), ill.describtion
from patient p left join illness ill
on p.id = ill.id
where p.deleted = 0
order by p.lastname, p.firstname;
I get this error message: ORA-01791: not a SELECTed expression
When SELECT DISTINCT, you can't ORDER BY columns that aren't selected. Use column aliases instead, as:
select distinct p.nr, initcap(p.firstname) fname, initcap(p.lastname) lname, ill.describtion
from patient p left join illness ill
on p.id = ill.id
where p.deleted = 0
order by lname, fname
this would do it, but i think you need to post your query as there may be a better solution
select upper(substr(<column>,1,1)) || substr(<column>,2,9999) from dual
To change string to String, you can use this:
SELECT
regexp_replace ('string', '[a-z]', upper (substr ('string', 1, 1)), 1, 1, 'i')
FROM dual;
This assumes that the first letter is the one you want to convert. It your input text starts with a number, such as 2 strings then it won't change it to 2 Strings.
You can also use the column number instead of the name or alias:
select distinct p.nr, initcap(p.firstname), initcap(p.lastname), ill.describtion
from patient p left join illness ill
on p.id = ill.id
where p.deleted = 0
order by 3, 2;
WITH inData AS
(
SELECT 'word1, wORD2, word3, woRD4, worD5, word6' str FROM dual
),
inRows as
(
SELECT 1 as tId, LEVEL as rId, trim(regexp_substr(str, '([A-Za-z0-9])+', 1, LEVEL)) as str
FROM inData
CONNECT BY instr(str, ',', 1, LEVEL - 1) > 0
)
SELECT tId, LISTAGG( upper(substr(str, 1, 1)) || substr(str, 2) , '') WITHIN GROUP (ORDER BY rId) AS camelCase
FROM inRows
GROUP BY tId;
I'm trying to build a SQL query to cross checks a couple of columns.
My issue is with the calcontvalue column.
The query works fine until I want to include a Where clause containing it AND calcontvalue = job.contvalue.
SELECT
job.accountno,
job.groupno,
job.contvalue,
job.z1total,
job.z2equip,
job.z3freight,
job.z4pack,
job.z5ancil,
job.z6roy,
job.zrpay,
job.z7paid,
job.z8invtype,
IIF(job.z8invtype <> 0, job.z2equip+job.z5ancil,
IIF(job.z8invtype <> 1, job.z2equip+job.z3freight+job.z4pack+job.z5ancil, 0)) AS calcontvalue,
group.groupno,
group.grouptype,
group.title FROM job
LEFT JOIN group
ON job.groupno = group.groupno
WHERE grouptype = 'J' AND calcontvalue = job.contvalue
The I'm presented with this error:
SQL:Column 'CALCONTVALUE' is not found.
Not sure what to try next.
You cannot use alias in WHERE, use full expression:
SELECT
job.accountno,
job.groupno,
job.contvalue,
job.z1total,
job.z2equip,
job.z3freight,
job.z4pack,
job.z5ancil,
job.z6roy,
job.zrpay,
job.z7paid,
job.z8invtype,
IIF(job.z8invtype <> 0, job.z2equip+job.z5ancil,
IIF(job.z8invtype <> 1, job.z2equip+job.z3freight+job.z4pack+job.z5ancil, 0)) AS calcontvalue,
group.groupno,
group.grouptype,
group.title
FROM job
LEFT JOIN group
ON job.groupno = group.groupno
WHERE grouptype = 'J'
AND IIF(job.z8invtype <> 0, job.z2equip+job.z5ancil,
IIF(job.z8invtype <> 1, job.z2equip+job.z3freight+job.z4pack+job.z5ancil, 0)) = job.contvalue
Also naming table group is bad practise because GROUP is keyword GROUP BY.
Im trying to filter out certain numbers in MS Access of tried "Not", "Not Like", and <> and having no luck can anyone help?
You could use <> if you are excluding only one number, like.
SELECT
yourFieldName1,
yourFieldName2
FROM
yourTableName
WHERE
yourNumberField <> 500;
If you want a series of Numbers then you could use,
SELECT
yourFieldName1,
yourFieldName2
FROM
yourTableName
WHERE
(
(yourNumberField <> 1)
Or
(yourNumberField <> 2)
Or
(yourNumberField <> 3)
Or
(yourNumberField <> 4)
Or
(yourNumberField <> 500)
);
I would also use,
SELECT
yourFieldName1,
yourFieldName2
FROM
yourTableName
WHERE
yourNumberField Not In (1, 2, 3, 4, 500);
Consider the following table (snapshot):
I would like to write a query to select rows from the table for which
At least 4 out of 7 column values (VAL, EQ, EFF, ..., SY) are not NULL..
Any idea how to do that?
Nothing fancy here, just count the number of non-null per row:
SELECT *
FROM Table1
WHERE
IIF(VAL IS NULL, 0, 1) +
IIF(EQ IS NULL, 0, 1) +
IIF(EFF IS NULL, 0, 1) +
IIF(SIZE IS NULL, 0, 1) +
IIF(FSCR IS NULL, 0, 1) +
IIF(MSCR IS NULL, 0, 1) +
IIF(SY IS NULL, 0, 1) >= 4
Just noticed you tagged sql-server-2005. IIF is sql server 2012, but you can substitue CASE WHEN VAL IS NULL THEN 1 ELSE 0 END.
How about this? Turning your columns into "rows" and use SQL to count not nulls:
select *
from Table1 as t
where
(
select count(*) from (values
(t.VAL), (t.EQ), (t.EFF), (t.SIZE), (t.FSCR), (t.MSCR), (t.SY)
) as a(val) where a.val is not null
) >= 4
I like this solution because it's splits data from data processing - after you get this derived "table with values", you can do anithing to it, and it's easy to change logic in the future. You can sum, count, do any aggregates you want. If it was something like case when t.VAL then ... end + ..., than you have to change logic many times.
For example, suppose you want to sum all not null elements greater than 2. In this solution you just changing count to sum, add where clause and you done. If it was iif(Val is null, 0, 1) +, first you have to think what should be done to this and then change every item to, for example, case when Val > 2 then Val else 0 end.
sql fiddle demo
Since the values are either numeric or NULL you can use ISNUMERIC() for this:
SELECT *
FROM YourTable
WHERE ISNUMERIC(VAL)+ISNUMERIC(EQ)+ISNUMERIC(EFF)+ISNUMERIC(SIZE)
+ISNUMERIC(FSCR)+ISNUMERIC(MSCR)+ISNUMERIC(SY) >= 4