I have a multi select drop down containing 20 values.
User can select all or few.
I want to write a query
if user has selected all the values then I don't want to pass all the selected values. (I will pass 0 to identify that all values are selected)
If use has selected few(or not all) then I want to pass only those selected values. (I will be passing comma separated values ex 101,102,103,104)
On the basis of the values I want write a join table or use it in a where condition
I have done something like as follows but it seem not working.
WHERE: Following throwing error (Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.)
DECLARE #EntitiyIds AS varchar(2500) = '0'
SELECT *
FROM Tb_CompanyType
WHERE Id IN (
CASE WHEN #EntitiyIds <> '0' THEN (SELECT Item FROM dbo.Split(#EntitiyIds, ','))
WHEN #EntitiyIds = '0' THEN (SELECT Id FROM Tb_CompanyType) END
)
INNER JOIN: (Following is not giving proper output)
DECLARE #EntitiyIds AS VARCHAR(2500) = '101'
SELECT * FROM Tb_CompanyType CT
INNER JOIN Tb_Company TC ON
CASE WHEN #EntitiyIds = '0' AND TC.CompanyId = CT.Id THEN 1
WHEN #EntitiyIds <> '0' AND TC.CompanyId = (SELECT Item FROM dbo.Split(#EntitiyIds, ',')) THEN 1 END = 1
dbo.Split returns table containing comma separated values
Kindly help
You can do it this way.
SELECT *
FROM Tb_CompanyType
WHERE #EntitiyIds = '0'
OR Id IN (SELECT Item
FROM dbo.Split(#EntitiyIds, ','))
Something like this perhaps.
SELECT Id FROM Tb_CompanyType where #EntitiyIds = '0'
UNION ALL
SELECT Item FROM dbo.Split(#EntitiyIds, ',') s where #EntitiyIds <> '0'
From the error message your provided, it seems that your sub-query is returning more than one rows. You last query segment's last line seems problematic:
DECLARE #EntitiyIds AS VARCHAR(2500) = '101'
SELECT * FROM Tb_CompanyType CT
INNER JOIN Tb_Company TC ON
CASE WHEN #EntitiyIds = '0' AND TC.CompanyId = CT.Id THEN 1
WHEN #EntitiyIds <> '0' AND TC.CompanyId = (SELECT Item FROM dbo.Split(#EntitiyIds, ',')) THEN 1 END = 1
Change the last
SELECT Item FROM dbo.Split(#EntitiyIds, ',')
to
SELECT top 1 Item FROM dbo.Split(#EntitiyIds, ',')
Related
Here I Have one ID having different Codes, I have to display if the Id has a Code value then need to display the code value row else null value row.
ID
Code
Name
12
null
Three
12
2345
Three
13
null
four
14
1543
rewq
Essentially we want to lookup up and pick out the entire "first" row giving precedence to a non-null. (The logic would be easy to reverse in all of the options below.) These all then go with the assumption that a maximum of two rows can be present and that one of them must have a null Code in that case.
with data as (select *, count(*) over (partition by ID) as cnt from T)
select ID, Code, Value from data
where cnt = 1 or Code is not null;
or
select distinct t1.ID,
coalesce(t2.Code, t1.Code) as Code, coalesce(t2.Value, t1.Value) as Value
from T t1 left outer join T t2 on t2.ID = t1.ID and t2.Code is not null;
or
--SQL Server
select ID, Code, Value
from T t1 cross apply (
select 1 as Keep from T t2
where t2.ID = t1.ID
having case when t1.Code is null then 0 else 1 end =
case when max(t2.Code) is null then 0 else 1 end) v;
or
with data as (select *, max(Code) over (partition by ID) as maxCode from T)
select ID, Code, Value from data
where coalesce(Code, '!##') = coalesce(maxCode, '!##');
or
with data as (
select *,
row_number() over (
partition by ID
order by case when Code is not null then 0 else 1 end) as rn
from T
) select ID, Code, Value from data where rn = 1;
or
with data as (
select distinct ID,
max(Code) over (partition by ID) as Code,
first_value(Value) over (
partition by ID
order by case when Code is not null then 0 else 1 end) as Value
from T
) select * from data;
or
select * from T t1
where Code is not null or not exists
(select 1 from T t2 where t2.ID = t1.ID and t2.Code is not null);
or
select ID, max(Code) as Code,
/* works better with character values */
substring(max(left(coalesce(Code, '') + ' ', 10) + Value), 10, 50)
from T group by ID;
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=2c08e5bd0b67118d39cf9f6404218b09
Well this worked for me
SELECT * FROM tablename
I am using the following query to gather some information about each ProductId - note that a ProductId can contain several records in the dbo.Sales table:
SELECT
c.ProductId,
COUNT(*) as NumberOfRecords,
(SELECT
(ISNULL(NULLIF(c.Text, ''), 'FALSE'))) as TextFieldHasData
FROM dbo.Sales c
JOIN dbo.Sources s
ON c.ProductId = s.ProductId
AND s.SourceStatusId in (1,2)
GROUP BY c.ProductId, c.Status, s.SourceStatusId, c.Text
ORDER BY c.ProductId
I need to tweak the ISNULL part of the query, and I'm having trouble with the syntax; what I actually need to do is first check the NumberofRecordscount - if the the NumberofRecords count for a given result record is greater than 1, then the TextFieldHadData field for that record should just say 'N/A'. But, if the NumberofRecordscount for a given result record = 1, then it should check whether the c.Text field is NULL or blank. If it is NULL or Blank, the TextFieldHasData field would say 'FALSE.' If it is not NULL or blank, the TextFieldHasData field would say 'TRUE.'
Looking at your code, perhaps you are looking for something like the following (where you would be grouping up to ProductId level):
SELECT
c.ProductId
, COUNT(*) as NumberOfRecords
,
CASE
WHEN COUNT(*) > 1
THEN 'N/A'
ELSE
CASE
WHEN SUM(CASE WHEN ISNULL(c.Text, '') = '' THEN 0 ELSE 1 END) > 0
THEN 'TRUE'
ELSE 'FALSE'
END
END TextFieldHasData
FROM
dbo.Sales c
JOIN dbo.Sources s ON
c.ProductId = s.ProductId
AND s.SourceStatusId in (1, 2)
GROUP BY c.ProductId
ORDER BY c.ProductId
You can use the query:
I can not validate it as I don't have these tables, but it should work, unless you find a minor syntax error.
The idea is to use "case when ..." sql function
select v.productid,v.NumberOfRecords,
case
when v.NumberOfRecords>1 then 'N/A'
when v.NumberOfRecords=1 and isnull(v.TextFieldHasData,'') ='' then 'FALSE'
else 'TRUE' end [textfieldhasdata]
from(
SELECT
c.ProductId,
COUNT(*) as NumberOfRecords,
(SELECT
(ISNULL(NULLIF(c.Text, ''), 'FALSE'))) as TextFieldHasData
FROM dbo.Sales c
JOIN dbo.Sources s
ON c.ProductId = s.ProductId
AND s.SourceStatusId in (1,2)
GROUP BY c.ProductId, c.Status, s.SourceStatusId, c.Text) v
ORDER BY ProductId
I have the following query in PostgreSQL (1=1 is a placeholder for some arbitrary condition as apparently I can't write WHERE TRUE in Sybase)
SELECT EXISTS FROM (
SELECT 1 FROM someTable WHERE 1=1
)
How do I translate them for SQL Server / Sybase syntax ?
A roundabout way is to do:
SELECT COUNT(*) FROM (
SELECT 1 FROM someTable WHERE 1=1
) a
… which can further be simplified to:
SELECT COUNT(*) FROM someTable WHERE 1=1
… but EXISTS is cleaner and I believe it's in the ANSI standard as well.
exists() doesn't return a value that you can select (I don't know why). You can check if exists(), but not select exists(). You can also check where exists() or even case when exists().
select
E = case
when exists(
select 1 from master..spt_values
)
then 1
else 0
end
If you are trying to get counts for multiple different criteria, a common pattern for sql server would be something like:
select
ACount = sum(case when x='A' then 1 else 0 end)
, ABCount = sum(case when x in ('A','B') then 1 else 0 end)
, TotalCount = count(*) /* or sum(1) */
from someTable
Not sure what you expect for 'EXISTS' but this might do the trick
SELECT 1
WHERE EXISTS (SELECT 1 FROM dbo.Table WHERE 1 = 1)
Try this:
SELECT IIF(EXISTS (SELECT 1 FROM mytable WHERE 1=1), 1, 0)
I'm trying to do the following:
SELECT *
FROM myTable
WHERE workplace IN
(CASE #param
WHEN 'a' THEN (SELECT workplace
FROM workPlaceTable
WHERE condition1)
WHEN 'b' THEN (SELECT workplace
FROM workPlaceTable
WHERE condition2)
END)
This will always return:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Has anybody an idea how I can realize this without an if and repeating the query?
I don't think you need the subquery at all, just use a JOIN with a WHERE clause:
SELECT T1.*
FROM myTable T1
INNER JOIN workPlaceTable T2 ON T1.workplace = T2.workplace
WHERE (#param = 'a' AND condition1) OR (#param = 'b' AND condition2)
If #param = 'a' then condition1 will be evaluated, otherwise condition2 will be evaluated.
Has anybody an idea how I can realize this without an if and repeating
the query?
Something like this could work for you.
select *
from myTable
where #Param = 'a' and workplace in (
select workplace
from workPlaceTable
where condition1
) or
#Param = 'b' and workplace in (
select workplace
from workPlaceTable
where condition2
);
To elaborate on #Allan's answer, a join would look something like this:
SELECT myTable.*
FROM myTable
INNER JOIN workPlaceTable ON myTable.workplace = workPlaceTable.workplace AND
(
(#param = 'a' AND <<condition1>>) OR (#param = 'b' AND <<condition2>>)
)
By the way, this question has nothing to do with dynamic-sql.
You can try this;
SELECT *
FROM myTable
WHERE (#param = 'a' and
workplace in
(SELECT workplace FROM workPlaceTable WHERE condition1))
OR (#param = 'b' and
workplace in
(SELECT workplace FROM workPlaceTable WHERE condition2))
I would like to know if the following is possible in SQL server 2005. Column A and B are calculated using other case statements in my actual stored proc. I don't want to repeat the same for another field unnecessarily. If this is not syntactically possible, any other ideas?
SELECT A, B, CASE WHEN column1='1' THEN A ELSE B END Col1.
Modified version of actual query provided as requested. CTE kind of seems to be tough in this model. WANNABE is the column I want to accomplish in the sub select statement.
SELECT 1 AS Region, 'Test',
CAST(Work AS NUMERIC(18,2)) Work,
Work + 2 AS Work2,
WANNABE
FROM
(
SELECT
ROW_NUMBER() OVER(PARTITION BY G.Value, C.C, FR.Mod1 ORDER BY FR.Date DESC, FG.Date DESC, FC.Date DESC) ROW,
CASE WHEN COALESCE(FR.Mod1, '') = '' THEN '' ELSE FR.Mod1 END Mod1,
CASE WHEN #var1=1 AND #var2 = 1 THEN FR.Col1 * G.Value
WHEN #var1=1 AND #var2 = 0 THEN FP.Col1 * G.Value END Work,
CASE WHEN 1=1 THEN Work ELSE 1 END WANNABE,
(
SELECT Col3
FROM Table2
WHERE c = FR.Value
) AS Custom
FROM MainTable FR
JOIN #C C ON FR.Col2 = C.Col2
LEFT JOIN Function1(#VersionDate) cv ON cv.Code = C.Code
LEFT JOIN Function2(#VersionDate) hv ON hv.Code = C.Code
LEFT JOIN #G G ON 1 = 1
LEFT JOIN SubTable1 FG ON FG.Number = G.Value, 2 AND FG.Date = #VersionDate
LEFT JOIN SubTable2 FO ON FO.Number = G.Value
AND FO.Date = #VersionDate AND FO.Code = FR.Code AND FR.Mod1 = FO.Mod1
LEFT JOIN SubTable3 FP ON FP.Code = FR.Code AND FP.VersionDate = #Versiondate
AND CASE WHEN DATALENGTH(FR.Mod1) = 0 THEN '00' ELSE FR.Mod1 END = CASE WHEN DATALENGTH(FP.Mod1) = 0 THEN '00' ELSE FP.Mod1 END
LEFT JOIN SubTable4 FC ON FC.Date = #VersionDate
WHERE FR.Date = #VersionDate
) x
WHERE x.Row = 1
AND RTRIM(LTRIM(x.Col1)) IN ('', '2')
You can define the A,B column aliases in a CTE then reference them in an outer select.
;WITH CTE AS
(
SELECT CASE ... END AS A,
CASE ... END AS B,
column1
FROM your_table
)
SELECT A,
B,
CASE WHEN column1='1' THEN A ELSE B END Col1
FROM CTE
Similarly you can also define them in a CROSS APPLY that is sometimes a bit less verbose.
A silly example just to show the syntax is
SELECT A,
B,
CASE WHEN type='P' THEN A ELSE B END Col1
FROM master..spt_values
CROSS APPLY (SELECT CASE WHEN number %2 = 1 THEN 1 END,
CASE WHEN number %2 = 0 THEN 0 END) T(A,B)
Following your update you can replace the derived table with a CTE and nest CTEs as follows
;WITH x as
(
SELECT
ROW_NUMBER() OVER(PARTITION BY G.Value, C.Code, FR.Mod1 ORDER BY FR.Date DESC, FG.Date DESC, FC.Date DESC) ROW,
...<snip>
WHERE FR.Date = #VersionDate
),
x2 As
(
SELECT *,
CASE WHEN 1=1 THEN Work ELSE 1 END WANNABE
FROM x
)
SELECT 1 AS Region, 'Test',
CAST(Work AS NUMERIC(18,2)) Work,
Work + 2 AS Work2,
WANNABE
FROM x2
WHERE x2.Row = 1
AND RTRIM(LTRIM(x2.Col1)) IN ('', '2')
Yeah it is posible, but how is all your sql statement? You can use the case statement in the select statement as you are using it.
Something like this
SELECT SUM((CASE WHEN column1='1' THEN 10 ELSE 0 END)) AS A, SUM((CASE WHEN column1='2' THEN 10 ELSE 0 END)) AS B
FROM YourTable