Conditional group by and select query sql - sql

i am having a query with conditional group by , i was just wondering is there a way to have select statement with that condition
my query is
select sum(totalamount),typetitle,
from #temp group by (case when Type ='1' then typetitle else Type end)
with this query i get error at typetitle as its not in group by statement , is there any workaround for this?

Tr like below
select sum(totalamount),
(case when Type ='1' then typetitle else Type end)
from #temp
group by (case when Type =1 then typetitle else Type end)
In group by projection column name and group by column have to be same

If you don't want to repeat the expression, then apply is one method:
select v.typegroup, sum(v.totalamount),
from #temp t cross apply
(values (case when Type ='1' then typetitle else Type end)
) v(typegroup)
group by v.typegroup;

Related

sql17 - The column 'id' was specified multiple times for 't'

I write below query in mssm 17.
select t.id,
max (case when seq=1 then label end) as label-1,
max (case when seq=2 then label end) as label-2,
max (case when seq=3 then label end) as label-3,
max (case when seq=4 then label end) as label-4,
max (case when seq=5 then label end) as label-5,
from (select t1.*,t2.*,
row-number () over (partition by t1.id order by t2.label) as seq
from table1 t1 inner join table2 t2 on t1.id=t2.sequence
) t
group by t.id
The query don't execute and the error is: The column 'id' was specified multiple times for 't'.
I want to merge all rows with similar id into one row and different columns.
can anybody help me and solve the error?
You have multiple issues with the query. This specific issue is that id is in both table1 and table2. So, just select the columns that you need in the outer query.
Also, - is not allowed for column aliases. And the correct function name is row_number(), not row-number():
select t.id,
max(case when seq=1 then label end) as label_1,
max(case when seq=2 then label end) as label_2,
max(case when seq=3 then label end) as label_3,
max(case when seq=4 then label end) as label_4,
max(case when seq=5 then label end) as label_5,
from (select t1.id, t2.label,
row_number() over (partition by t1.id order by t2.label) as seq
from table1 t1 inner join
table2 t2
on t1.id = t2.sequence
) t
group by t.id

aggregation functions with different where condition sentence

I have SQL query give table of 6 columns from
mytable:
TotalNumberOfRecords
TotalDurationOfCalls
AvgdurationPer
TotalCallednumbers
TotalCallednumbers
Ratiocalledtoallcalls
these columns resulted of aggregation functions. but each one have different where condition sentences in order to select from table.
MY Query like this:
select ID,
count(*) as TotalNumberOfRecords,
sum (isnull(cast(duration as int),0)) {where condition1} as TotalDurationOfCalls ,
AVG(isnull(cast(duration as int),0)){where condition2} as AvgdurationPer,
count(distinct IDS) {where condition3} as TotalCallednumbers ,
count(distinct CGI) {where condition4} as TotalOfLocations,
cast(count(distinct IDS) as float)/cast(count(*) as float) {where condition5} as Ratiocalledtoallcalls
from Mytable
group by ID
Now, my problem is, how can I execute this query in one query to get one table?
You want conditional aggregation. You do this by making a case expression the argument to an aggregation function:
select ID, count(*) as TotalNumberOfRecords,
sum(case when condition1 then cast(duration as int) else 0 end) as TotalDurationOfCalls ,
avg(case when condition2 then cast(duration as int) else 0 end) as AvgdurationPer,
count(distinct case when condition3 then IDS end) as TotalCallednumbers,
count(distinct case when condition4 then CGI end) as TotalOfLocations,
cast(distinct count(case when condition5 then IDS end) as float)/cast(count(*) as float) as Ratiocalledtoallcalls
from Mytable
group by ID

SQL - WHERE on new column

I've a table with 3 columns: Id, Price, Total.
I'm writing this sql statement:
SELECT Id, Price, Total,
CASE WHEN [Total] IS NULL THEN '0'
WHEN [Total] IS NOT NULL '1'
ELSE ''
END AS NewColumnName
FROM Table
If I run this sql, I have no error. But if I add a Where, like this:
WHERE NewColumnName= '1' the server return an error: the name of column NewColumnName is not valid.
Please help me!
Thanks a lot!!
RM
Can you try this way?
select *
from (
SELECT Id, Price, Total,
CASE WHEN [Total] IS NULL THEN '0'
WHEN [Total] IS NOT NULL '1'
ELSE ''
END AS NewColumnName
FROM Table
) x
where
NewColumnName= '1'
Try this,
SELECT Id, Price, Total,
CASE WHEN [Total] IS NULL THEN '0'
WHEN [Total] IS NOT NULL '1'
END AS NewColumnName
FROM Table
WHERE CASE WHEN [Total] IS NULL THEN '0'
WHEN [Total] IS NOT NULL '1'
END = '1'
Generally use a subquery,
SELECT Id, Price, Total, NewColumnName
FROM (
SELECT
Id,
Price,
Total,
CASE
WHEN [Total] IS NULL THEN '0'
ELSE '1'
END [NewColumnName]
FROM
Table) [WithNew]
WHERE
NewColumnName = '1';
or in your case you could do,
SELECT
Id,
Price,
Total,
1 [NewColumnName]
FROM
Table
WHERE
Total IS NOT NULL;
USE [AdventureWorks2012]
WITH cte as (
SELECT [AddressID], [AddressLine1]
, CASE WHEN [City] = 'Bothell' THEN 1 ELSE 0 END AS [NewColumn]
FROM Person.Address )
SELECT * FROM CTE
WHERE [NewColumn] = 1
So yours would be re-written like this:
WITH cte as (
SELECT Id, Price, Total,
CASE WHEN [Total] IS NULL THEN '0'
WHEN [Total] IS NOT NULL '1'
ELSE ''
END AS NewColumnName
FROM Table)
SELECT * FROM CTE where [NewColumnName] = 1
You can't use NewColumnName in the WHERE Clause. You will have to use the CASE statement again in WHERE Clause or do what #Lajos suggested.
You can just write your query like this:
SELECT Id, Price, Total,
FROM Table
WHERE Total IS NOT NULL
That's doing the same thing, except without the case statement.

Cant put sqLite query in tableAdapter

I have a question for you guys.
I have an sqlite query that i test on SqlFiddle and i know it work, but i cant put it in a table adapter in Vb.net.
here's my query:
select cola, colb,
SUM(case when (tbl = 'a') then 1 else 0 END) as TableA,
SUM(case when (tbl = 'b') then 1 else 0 END) as TableB,
SUM(case when (tbl = 'c') then 1 else 0 END) as TableC
from
(
select cola,colb,'a' as tbl
from TableA
union all
select cola, colb,'b' as tbl
from TableB
union all
select cola, colb,'c' as tbl
from TableC
) d
group by cola, colb
The error that i get in Visual studio 2010 is:
Error in select clause: expression near 'END'
Missing FROM clause
Error in select clause: expression near ','
Error in select clause: expression near 'FROM'
Unable to parse query text.
Am I doing something wrong?
Thanks in advance.

SQL Select for multiple where clause

I am trying to create SQL Select that returns counts of a certain field based on a field.
So, here is what I am trying to do.
Select count(distinct id) as TotalCount, -- this will be the total of id
count(distinct id where type='A') as TotalA, -- this will be total when type='A'
count(distinct id where type='B') as TotalB -- This will be total when type = 'B'
from MyTable
Basically, TotalCount = TotalA + TotalB.
How can I achieve this in SQL Select Statement?
Thanks.
Select count(distinct id) as TotalCount, -- this will be the total of id
count(distinct case type when 'A' then id else NULL end) as TotalA,
count(distinct case type when 'B' then id else NULL end) as TotalB
from MyTable;
Of course TotalCount may or may not be TotalA + TotalB, depending on the actual data.
You can do it like that:
SELECT
count(distinct id) as TotalCount,
sum(CASE WHEN type = 'A' THEN 1 ELSE 0) as TotalA,
sum(CASE WHEN type = 'B' THEN 1 ELSE 0) as TotalB,
FROM
MyTable
Count per type:
SELECT
type,
count(DISTINCT id)
FROM
MyTable
GROUP BY
type
Why not simply UNION the separate queries.
Select 'all' as which, count(distinct id) as Total from mytable
union
select 'a' as which, count(distinct id) where type='A' as Total from mytable
union
select 'b' as which, count(distinct id) where type='B' as Total from mytable