Having "___ in (select distinct ___ from #temp)" in Case statement - tsql

I'm trying to achieve this
select
case
when Org_CD = '1111' or Org_CD in (select distinct New_Org_CD from #temp) then 'International'
end as 'Organisation',
count(*)
from #AnotherTempTable
group by
case
when Org_CD = '1111' or Org_CD in (select distinct New_Org_CD from #temp) then 'International'
end
I received this error:
Column '#AnotherTempTable.Org_Cd' is
invalid in the select list because it
is not contained in either an
aggregate function or the GROUP BY
clause.
Is it because I cannot use "in" keyword inside the case statements? If yes, any known workarounds would be more than helpful!

I'd try this...
select
Org_CD, count(*)
from
#AnotherTempTable A
JOIN
(select distinct New_Org_CD from #temp UNION SELECT '1111') T ON A.Org_CD = T.New_Org_CD
group by
Org_CD
You can't have an inline IN like this (CASE + aggregate)
If this is not OK, please give sample data and output

I solved it with a variation of gbn's solution using 'Union'. Thanks all.

Related

PostgreSQL group by all fields

I have a query like this:
SELECT
table1.*,
sum(table2.amount) as totalamount
FROM table1
join table2 on table1.key = table2.key
GROUP BY table1.*;
I got the error: column "table1.key" must appear in the GROUP BY clause or be used in an aggregate function.
Are there any way to group "all" field?
There is no shortcut syntax for grouping by all columns, but it's probably not necessary in the described case. If the key column is a primary key, it's enough when you use it:
GROUP BY table1.key;
You have to specify all the column names in group by that are selected and are not part of aggregate function ( SUM/COUNT etc)
select c1,c2,c4,sum(c3) FROM totalamount
group by c1,c2,c4;
A shortcut to avoid writing the columns again in group by would be to specify them as numbers.
select c1,c2,c4,sum(c3) FROM t
group by 1,2,3;
I found another way to solve, not perfect but maybe it's useful:
SELECT string_agg(column_name::character varying, ',') as columns
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'your_table
Then apply this select result to main query like this:
$columns = $result[0]["columns"];
SELECT
table1.*,
sum(table2.amount) as totalamount
FROM table1
join table2 on table1.key = table2.key
GROUP BY $columns;

How to avoid duplicates in the STRING_AGG function

My query is below:
select
u.Id,
STRING_AGG(sf.Naziv, ', ') as 'Ustrojstvena jedinica',
ISNULL(CONVERT(varchar(200), (STRING_AGG(TRIM(p.Naziv), ', ')), 121), '')
as 'Partner',
from Ugovor as u
left join VezaUgovorPartner as vup
on vup.UgovorId = u.Id AND vup.IsDeleted = 'false'
left join [TEST_MaticniPodaci2].dbo.Partner as p
on p.PartnerID = vup.PartnerId
left join [dbo].[VezaUgovorUstrojstvenaJedinica] as vuu
on vuu.UgovorId = u.Id
left join [TEST_MaticniPodaci2].hcphs.SifZavod as sf
on sf.Id = vuu.UstrojstvenaJedinicaId
left join [dbo].[SifVrstaUgovora] as vu
on u.VrstaUgovoraId = vu.Id
group by u.Id, sf.Naziv
My problem is that I can have more sf.Naziv and also only one sf.Naziv so I have to check if there is one and then show only one result and if there is two or more to show more results. But for now the problem is when I have only one sf.Naziv, query returns two sf.Naziv with the same name because in first STRING_AGG i have more records about p.Naziv.
I have no idea how to implement DISTINCT into STRING_AGG function
Any other solutions are welcome, but I think it should work with DISTINCT function.
It looks like distinct won't work, so what you should do is put your whole query in a subquery, remove the duplicates there, then do STRING_AGG on the data that has no duplicates.
SELECT STRING_AGG(data)
FROM (
SELECT DISTINCT FROM ...
)
I like this format for distinct values:
(d is required but you can use any variable name there)
SELECT STRING_AGG(LoadNumber, ',') as LoadNumbers FROM (SELECT DISTINCT LoadNumber FROM [ASN]) d
A sample query to remove duplicates while using STRING_AGG().
WITH cte AS (
SELECT DISTINCT product
FROM activities
)
SELECT STRING_AGG(product, ',') products
FROM cte;
Or you can use the following query. The result is same -
SELECT STRING_AGG(product, ',') as products
from (
SELECT product
FROM Activities
GROUP BY product
) as _ ;

How to use the AS name in a query WHERE clause?

given a query like so:
SELECT
id,
(SELECT COUNT(*)
FROM members
WHERE members.network_id = networks.id) AS mem_count
FROM
networks
WHERE mem_count > 2
With this query, the where clause breaks as it does not know what mem_count is... Why can't I use the as var in the where clause?
Thanks
While bernie suggested correct answer to the question, your query can be simplified to:
SELECT
network_id as id,
count(*)
FROM
members
GROUP BY
network_id
HAVING
count(*) > 2
Which, as an additional bonus, can be faster.
You've got the concept down. You just need the right syntax. You could re-write like this and have the added benefit of making your query ANSI-compliant:
SELECT
id,
m.mem_count
FROM
networks n
JOIN (
SELECT m.network_id,
COUNT(*) AS mem_count
FROM members
GROUP BY m.network_id
) m
ON m.network_id = n.id
AND m.mem_count > 2;
Try:
SELECT
id,
(SELECT COUNT(*) as mem_count
FROM members
WHERE members.network_id = networks.id)
FROM
networks
WHERE mem_count > 2
One way would be.
Select * From (
SELECT
id,
(SELECT COUNT(*)
FROM members
WHERE members.network_id = networks.id) AS mem_count
FROM
networks)) mem_counts
WHERE mem_count > 2
A join as suggested by Bernie would be better though. Basically you confused the parser. You get the same sort of issue with group by or order by when you use AS to alias a column name.

IS IN (Select statement, 'value', 'value')

I am trying to run the following SQL
DELETE FROM T_ATH_POSHLD WHERE T_ATH_POSHLD.A_INSID IN (SELECT T_ATH_POSHLD.A_INSID FROM T_ATH_POSHLD LEFT JOIN T_ATH_INS ON T_ATH_POSHLD.A_INSID = T_ATH_INS.A_INSID WHERE T_ATH_INS.A_INSCLSCDE1 = 'CASH' AND T_ATH_POSHLD.A_INSID NOT IN (SELECT A_INSID FROM T_ATH_CCY) AND A_ACCID IN (SELECT A_ACCID FROM T_ATH_EXTACC, '1212OEIC', '5667033ZS'))
and in particular, am trying to check whether an ACCID is in a set of values, some coming from a table and two hardcoded. How would I achieve this?
IN (SELECT A_ACCID FROM T_ATH_EXTACC, '1212OEIC', '5667033ZS')
Doesn't work, I get an 'Incorrect Syntax error'.
Thanks
You need to use UNION to add the 2 hardcoded values to the resultset that you are passing to the in clause.
IN (SELECT A_ACCID FROM T_ATH_EXTACC UNION ALL SELECT '1212OEIC' UNION ALL SELECT '5667033ZS')
IN (SELECT '1212OEIC', '5667033ZS', A_ACCID FROM T_ATH_EXTACC )

using CASE in T-SQL in the where clause?

Im trying to use case to vary the value im checking in a where clause but I'm getting the error:
incorrect syntax near the keyword 'CASE'
SQL Server 2005
select *
from table
where ((CASE when adsl_order_id like '95037%'
then select '000000'+substring(adsl_order_id,6,6)
ELSE select adsl_order_id
END)
not in (select mwebID from tmp_csv_dawis_bruger0105)
Here is one way to include a case statement in a Where clause:
SELECT * FROM sometable
WHERE 1 = CASE WHEN somecondition THEN 1
WHEN someothercondition THEN 2
ELSE ... END
You could try
SELECT *
FROM table
WHERE (SELECT CASE WHEN adsl_order_id LIKE '95037%'
THEN '000000' + SUBSTRING(adsl_order_id, 6, 6)
ELSE adsl_order_id
END)
NOT IN (select mwebID from tmp_csv_dawis_bruger0105)
A correlated subquery is one possibility:
select *
from mytable
where not exists (
select *
from
tmp_csv_dawis_bruger0105
where
mwebID =
CASE when mytable.adsl_order_id like '95037%' then '000000' + substring(mytable.adsl_order_id,6,6)
ELSE mytable.adsl_order_id END
)
I came to this question looking for an answer thinking WHERE CASE ... would be the solution. I could not adapt the answers to my problem, but this technique to use a parameter that could be null, partial, or specific, works:
CREATE PROC myproc #vJobID VARCHAR (11)
AS
SELECT * FROM Jobs
WHERE Jobs.JobID like coalesce(#vJobID+'%','%')
HTH
You have one too many opening parentheses before the CASE expression.
Put it in the SELECT clause...
select *, (CASE when adsl_order_id like '95037%'
then '000000'+substring(adsl_order_id,6,6)
ELSE adsl_order_id
END) AS Id
from table
where not in (select mwebID from tmp_csv_dawis_bruger0105)
Also, you don't need to "SELECT" the result of the CASE.