I have a problem with my SQL statement:
I use SELECT statement and then CASE when I use order by.
SELECT .....
ORDER BY
CASE WHEN #sort = 'ND' THEN name END DESC,
CASE WHEN #sort = 'NA' THEN name END,
CASE WHEN #sort = 'AD' THEN (isAuthorized) END DESC,
CASE WHEN #sor = 'AA' THEN (isAuthorized) END
isAuthorized is a BIT value, so I would like to use secondary order by there.
I tried something like:
SELECT .....
ORDER BY
CASE WHEN #sort = 'ND' THEN name END DESC,
CASE WHEN #sort = 'NA' THEN name END,
CASE WHEN #sort = 'AD' THEN (isAuthorized, name ) END DESC,
CASE WHEN #sort = 'AA' THEN (isAuthorized, name ) END
But it doesn't work.
I use SQL server 2008.
Any help would be greatly appreciated.
If you want order the result set first by name and then by isAuthorized when #sort = ND use this query:
SELECT .....
ORDER BY
CASE WHEN #sort = 'ND' THEN name END DESC,
CASE WHEN #sort = 'ND' THEN isAuthorized END
You can combine CASE clauses. I hope you got the idea.
Related
I've an Informatica function which I want to convert into query to be getting used in Spring Batch code.
I've a table EMPLOYEE table having 15 fields (all I want in select) and Informatica has function Router which creates group based on STATUS_CD = 'A' and default (means all other records should go here - where status is other than A).
How can we do in Postgres?
I've all the employees and I want to check based using combination of EMPLOYEE_CD, EMPLOYEE_ID is unique and I want to simply return the count of it.
Query1
SELECT EMPLOYEE_CD AS EMPLOYEE_CD,
EMPLOYEE_ID AS EMPLOYEE_ID,
COUNT (*) AS CNT
FROM EMPLOYEE
GROUP BY EMPLOYEE_CD, EMPLOYEE_ID
HAVING COUNT (*) > 1;
Query 2
SELECT EMPLOYEE_ID, EMPLOYEE_NAME, EMPLOYEE_EMAIL, EMPLOYEE_PHONE, EMPLOYEE_ADDRESS, (Create Count Field here)
FROM EMPLOYEE
Query 3 - I need to group (which is my original question) or Create Columns ACTIVE, NON_ACTIVE columns as a part of query results where EMPLOYEE_STAT_CD = 'A', ACTIVE column value should say YES and EMPLOYEE_STAT_CD other than A, NON_ACTIVE should say Yes.
How can merge Query1 and Query 2 and Query 3 into single query ?
if I understood the question, your code is something like:
SELECT EMPLOYEE_ID, EMPLOYEE_NAME, EMPLOYEE_EMAIL, EMPLOYEE_PHONE, EMPLOYEE_ADDRESS,
COUNT(*)OVER(PARTITION BY EMPLOYEE_CD, EMPLOYEE_ID) AS counter_from_sql1,
CASE WHEN EMPLOYEE_STAT_CD = 'A' THEN 'YES' ELSE NULL END AS ACTIVE,
CASE WHEN EMPLOYEE_STAT_CD <> 'A' THEN 'YES' ELSE NULL END AS NON_ACTIVE
FROM EMPLOYEE;
or
SELECT * FROM (
SELECT EMPLOYEE_ID, EMPLOYEE_NAME, EMPLOYEE_EMAIL, EMPLOYEE_PHONE, EMPLOYEE_ADDRESS,
COUNT(*)OVER(PARTITION BY EMPLOYEE_CD, EMPLOYEE_ID) AS counter_from_sql1,
CASE WHEN EMPLOYEE_STAT_CD = 'A' THEN 'YES' ELSE NULL END AS ACTIVE,
CASE WHEN EMPLOYEE_STAT_CD <> 'A' THEN 'YES' ELSE NULL END AS NON_ACTIVE
FROM EMPLOYEE
) z
WHERE counter_from_sql1 > 1;
I have a DB2 Server and I want to select data depending on the condition.
If I have the Value Value in the ColumnX, ColumnY or ColumnZ it should search for it between the TB2.Column1 and TB3.Column1
My statement is a bunch of left outer joins but it looks like that:
SELECT
CASE
WHEN (TB1.ColumnX || TB1.ColumnY || TB1.ColumnZ) = 'Value'
THEN Statement1
ELSE ' '
END AS MyColumn
FROM
TB1 LEFT OUTER JOIN TB2 ON TB1.JOINCOL = TB2.JOINCOL
LEFT OUTER JOIN TB3 ON TB2.JOINCOL2 = TB3.JOINCOL
WHERE TB1.Column1 between TB2.Column1 and TB3.Column1
But it doesn't work, is there a mistake in the syntax?
It is not possible to check for multiple equalities using just a single expression. You may use the following syntax trick:
CASE WHEN 'Value' IN (TB1.ColumnX, TB1.ColumnY, TB1.ColumnZ)
THEN Statement1 ELSE ' ' END AS MyColumn
The alternative to this would be to repeat the full equality check for each column:
SELECT
CASE
WHEN TB1.ColumnX = 'Value' OR
TB1.ColumnY = 'Value' OR
TB1.ColumnZ = 'Value'
THEN Statement1
ELSE ' '
END AS MyColumn
FROM
TB1 LEFT OUTER JOIN TB2
ON TB1.JOINCOL = TB2.JOINCOL
LEFT OUTER JOIN TB3
ON TB2.JOINCOL2 = TB3.JOINCOL
WHERE
TB1.Column1 BETWEEN TB2.Column1 AND TB3.Column1;
If you need to refer to the CASE expression in the WHERE clause, then you have two choices. First, you may subquery your current query, and assign an alias to the CASE expression:
SELECT *
FROM
(
SELECT CASE WHEN ... AS exp
FROM TB1 ...
) t
WHERE exp = ...
Or, you may avoid the subquery and just repeat the entire CASE expression in the WHERE clause.
I have a SQL Statement where I have to check on conditions on rows since it has duplicates.
Condition1 is to watch if the value from a column in tb1 is between two values in a two columns of tb2 but only on rows which has a 'Y' inside.
I have something like this:
SELECT DISTINCT
tb1.columnA, tb1.columnB, tb2.columnA, tb2.columnB,
CASE
WHEN tb1.col1 = 'Y'
THEN CONCAT(tb1.col2,tb2.col2)
ELSE 'no changes'
END as conditionColumn
FROM
tb1 left outer join tb2 on tb1.columnX tb2.columnX
WHERE
CASE
WHEN tb1.col1 = 'Y'
THEN Condition1
ELSE Condition2
END
From what I saw before, a case statement is not allowed in the where clause?
How can I handle this then?
EDIT
When I have a 'Y' in tb1.col1
the where clause should output me an ID specified in tb1.columnA and only the rows which values from tb1.ColumnB is between tb2.columnA and tb2.columnB. If it doesn't have a Y inside then it should simply just give me the ID
so I used this, but it gives me an syntax error:
SELECT DISTINCT
tb1.columnA, tb1.columnB, tb2.columnA, tb2.columnB,
CASE
WHEN tb1.col1 = 'Y'
THEN CONCAT(tb1.col2,tb2.col2)
ELSE 'no changes'
END as conditionColumn
FROM
tb1 left outer join tb2 on tb1.columnX tb2.columnX
WHERE
CASE
WHEN tb1.col1 = 'Y'
THEN tb1.columnA = 'MyID'
AND tb1.columnB BETWEEN tb2.columnA and tb2.columnB
ELSE tb1.columnA = 'MyID'
END
If you want to do this
WHERE
CASE
WHEN tb1.col1 = 'Y'
THEN Condition1
ELSE Condition2
END
then, likely what you are wanting is this
WHERE
( tb1.col1 = 'Y' AND Condition1 )
OR ( tb1.col1 <> 'Y' AND Condition2 )
You seem to be confused about the purpose of a WHERE clause...
All a WHERE clause does is decide rather or not a given row is returned. It can't change the data being returned.
To change the data, you need a CASE in the columns selected...
something like so...
SELECT DISTINCT
CASE
WHEN tb1.col1 = 'Y'
AND tb1.columnB BETWEEN tb2.columnA and tb2.columnB
then 'MyID'
ELSE tb1.columnA
END as newColumnA,
tb1.columnB, tb2.columnA, tb2.columnB,
CASE
WHEN tb1.col1 = 'Y'
THEN CONCAT(tb1.col2,tb2.col2)
ELSE 'no changes'
END as conditionColumn
FROM
tb1 left outer join tb2 on tb1.columnX tb2.columnX
I need a query based on an exclusive Or statement for this I try using the case but I won't get a result in case of Null...
...
and b.[U_Periode] = CASE
when (b.U_Periode= #period) then #period
when (b.U_Periode is NULL ) then null
end
...
The Case that won't be catched is... if B.U_Status is null and b.U_Periode is null.
If the var Periode match the Value and the U_Status = 0 or 1
the only way getting this working for me was this:
...
and
ISNULL( b.[U_Status],'0') = CASE
when (b.U_Status= '1') then '1'
when (isnull( b.U_Status,'0')= '0') then '0'
end
and
ISNULL (b.[U_Periode],'01.01.1901') = CASE
when (b.U_Periode= #period) then #period
when (ISNULL (b.U_Periode,'01.01.1901') = '01.01.1901' ) then '01.01.1901'
end
are there any other better solutions for this?
Best regards
Oliver
Okay... here is my Problem
Table1
InsID ContractID
1 1
2 1
Table2
ID insid Period Status Count
1 1 null null 100
2 1 30.09.2015 1 500
3 2 null null 100
4 2 30.09.2015 1 500
Case '31.08.2015'
in total Value should be 200
in case of '30.09.2015'
the Value should be 1.000
XOR /OR will do the same in this case.
Value case '31.08.2015' = 200
value Case ' 30.09.2015 = 2200
So this is somesing like a subquery
left join (
[dbo].[Table2]b
inner join [dbo].[Table 3]K on k.DocEntry = b.DocEntry and CAST( k.U_CSetID as int) >0
)
on b.[U_InsID] in(select... but here I should have an if statement...
If there are results matching the Date join this
If not than join the result NULL is matching to periode...
Okay.. here is the complete query
the Table2 with the Date of 31.08.2015 should have one Record that include
B_STATUS = Null and B.Preiode = null and there is no available record with the U_Periode '31.08.2015' and a Staus ...
with a date of 30.09.2015
there is a Record matching U_Period = '30.09.2015' in this case the Record with U_Period=null should not effect the result...
Declare #period as varchar(20)= '31-08-2015 00:00:00'
declare #Customer as Varchar(15)='12345'
declare #Contract as varchar(30) = '123'
declare #test as varchar(1) = null
select SUM(cast(K.U_Count as decimal))as counter, K.U_CounterTyp
from [dbo].[Table1] a
left join (
[dbo].[Table2]b
inner join [dbo].[Table 3]K on k.DocEntry = b.DocEntry and CAST( k.U_CSetID as int) >0
)
on b.[U_InsID]=a.[insID] and b.[U_ObjectType]in ('5','1') and
ISNULL( b.[U_Status],'0') = CASE
when (b.U_Status= '1') then '1'
when (isnull( b.U_Status,'0')= '0') then '0'
end
and
ISNULL (b.[U_Periode],'01.01.1901') = CASE
when (b.U_Periode= #period) then #period
when (ISNULL (b.U_Periode,'01.01.1901') = '01.01.1901' ) then '01.01.1901'
end
where a.[customer] =#Customer and a.[Status]='A' and a.[U_ContrCount]='1'
and a.[manufSN] in(
select c.[ManufSN] from [dbo].[Table4] c
inner join [dbo].[OCTR]d on d.[ContractID] = c.[ContractID]
where c.[ManufSN]=a.[manufSN]
and d.[CstmrCode] = a.[customer] and d.[ContractID]=#Contract
)
group by K.U_CounterTyp
you must use the "^" operand, this is XOR operand in TSQL
expression ^ expression
the expression must return or 0 or 1...
stupid example:
WHERE (name like "stackoverflow") ^ (age > 10)
font: https://msdn.microsoft.com/en-us/library/ms190277(v=sql.105).aspx
Update for your check
WHERE (CONVERT(VARCHAR(10), a.[U_Periode], 104) = '30.08.2015') != (a.U_Periode IS NULL)
Olay here is my funktion that chek either a Date Result is there or not.
The only thing is, that the performance is not the best if I run hundreths of sentences ... foreach record (up to app. 1000) I need to query each subtable... and there are many many records in...
Anyway this is the function
CREATE FUNCTION fn_GetInsCounters(#InsId as Varchar(30), #Date as datetime)
returns datetime
as
begin
declare #count as int = 0
declare #Retruns as Datetime
set #count =
(
select count( b.Docentry)
from [dbo].[Table2]b
where b.U_Insid =#InsID
and b.U_Status <> '2'
and b.[U_ObjectType]in ('5','1')
and b.U_Periode=#Date
)
if(#count>0)
begin
set #Retruns = #date
end
else
begin
set #Retruns = '01.01.1901'
end
return #Retruns
end
if anyone gets a better idea???
Best regards
Oliver
I'm trying a query like this;
WITH CTE AS
(
SELECT
ID, NAME, DEPT,
CASE WHEN Fixed = 1 THEN 'Yes' ELSE '' END,
CASE WHEN NotFixed = 1 THEN 'Yes' ELSE '' END,
)
When I try to execute this query, I'm getting error like
No Column was specified for column 4 of CTE
What's wrong with the query?
Read the error message: You forgot to give your calculated columns a name. This goes for both column 4 and column 5. So change your query to something like this:
WITH CTE AS
(
SELECT
ID, NAME, DEPT,
CASE WHEN Fixed = 1 THEN 'Yes' ELSE '' END AS [Expr1],
CASE WHEN NotFixed = 1 THEN 'Yes' ELSE '' END AS [Expr2],
. . .
)
Feel free to change [Expr1] and [Expr2] to something more meaningful...