Is there a way do something like this (a conditional select from a nested subquery):
SELECT
Id,
Name WHERE Name LIKE '%Peter%' AS Peters,
Name WHERE Name LIKE '%Mike%' AS Mikes
FROM
(SELECT Id, Name FROM Customer) myset
How about:
SELECT
Id,
case when Name LIKE '%Peter%' then Name else null end AS Peters,
case when Name LIKE '%Mike%' then Name else null end AS Mikes
FROM
(SELECT Id, Name FROM Customer) myset
Additionally, you could use the table directly.
select Id, Peters, Mikes
from(
select
case when Name like '%Peter%' then Name end as Peters,
case when Name like '%Mike%' then Name end as Mikes
from Customer)t
Related
table image
I have this table that I need to sort in the following way:
need to rank Departments by Salary;
need to show if Salary = NULL - 'No data to be shown' message
need to add total salary paid to the department
need to count people in the department
SELECT RANK() OVER (
ORDER BY Salary DESC
)
,CASE
WHEN Salary IS NULL
THEN 'NO DATA TO BE SHOWN'
ELSE Salary
,Count(Fname)
,Total(Salary) FROM dbo.Employees
I get an error saying:
Column 'dbo.Employees.Salary' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Why so?
Column 'dbo.Employees.Salary' is invalid in the select list because it
is not contained in either an aggregate function or the GROUP BY
clause.
Why so?
The aggregate functions are returning a single value for the whole table, you can't SELECT a field alongside them it doesn't makes sense. Like say, you have a students table you apply Sum(marks) for the whole students table, and you are then also selecting student's name Select studentname in your query. Which student's name will the database engine select? Confusing
Column "invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause"
I tried this-
using inner query
SELECT RANK() OVER (ORDER BY SAL DESC) RANK,FNAME,DEPARTMENT
CASE
WHEN SAL IS NULL THEN 'NO DATA TO BE SHOWN'
ELSE SAL
END
FROM
(SELECT COUNT(FNAME) FNAME, SUM(SALARY) SAL, DEPARTMENT
FROM TESTEMPLOYEE
GROUP BY DEPARTMENT) t
I must count a number of unique surnames and names in the Postgresql table.
The problem is usage of distinct is denied by task.
What I tried to do:
SELECT COUNT(SURNAME), COUNT (NAME) FROM PEOPLE GROUP BY NAME, SURNAME;
Output:
1 1 1 1 1 and etc
(4939 rows)
But it looks like I did something wrong because in output I must get only two digits with a count.
Any idea what to do with it?
You can get around using DISTINCT by first grouping by the name or surname, and then taking a count of that intermediate table.
SELECT
(SELECT COUNT(*) FROM
(SELECT SURNAME FROM PEOPLE GROUP BY SURNAME) t) AS surname_cnt,
(SELECT COUNT(*) FROM
(SELECT NAME FROM PEOPLE GROUP BY NAME) t) AS name_cnt
I have a query like below getting the error - 'SELECT DISTINCT, ORDER BY expressions must appear in select list'
select distinct name
from fruits
order by case
when name = 'mango' then 1
else 2
end
This results 4 records, say
apple, mango, pear and grape
How can I make sure I get Mango as the first record always and the rest follow. I tried using the case statement, but not able to get the desired results. Any ideas will be appreciated.
I believe this should accomplish what you describe as needing.
select distinct
name,
case name when 'Mango' then 1 else 2 end as fruitOrder
from fruits
order by
fruitOrder
If you need to always have 'mango' in first position, no matter the other rows, this could be a way:
with fruits(name) as (
select 'apple' from dual union all
select 'mango' from dual union all
select 'pear' from dual union all
select 'grape' from dual
)
select name
from fruits
order by case
when name = 'mango' then 1
else 2
end
If you need to add a DISTINCT, this should work:
select distinct name,
case
when name = 'mango' then 1
else 2
end orderCol
from fruits
order by orderCol
This will give you 'Mango' followed by the others in order;
WITH get_rows AS
(SELECT DISTINCT item_type
FROM the_item)
SELECT item_type
FROM
(SELECT 1 as seq, item_type
FROM get_rows
WHERE item_type = 'Mango'
UNION ALL
SELECT 2 as seq, item_type
FROM get_rows
WHERE item_type <> 'Mango')
ORDER BY seq, item_type
SELECT Country, EmployeeName, Name As [FullName], OutPut1, OutPut2, OutPut3,
(OutPut1 + OutPut2 + OutPut3) AS Total,
CASE WHEN Name IS NULL THEN '.........'
WHEN Name IS NOT NULL THEN '??????'
END)
AS ParentID
FROM CTE_Report;
The CASE WHEN in the above statement is giving trouble with the error below. Any one know what i'm missing?
Common table expression defined but not used..
Common table expression defined but not used..
this is mean that you create CTE but do not use it,
same error as in snapshot below
SELECT Country, EmployeeName, Name As [FullName], OutPut1, OutPut2, OutPut3,
(OutPut1 + OutPut2 + OutPut3) AS Total,
CASE WHEN Name IS NULL THEN '.........'
WHEN Name IS NOT NULL THEN '??????'
END
AS ParentID
FROM CTE_Report;
Its a parenthesis problem.
Try this
SELECT Country,
EmployeeName,
Name AS [FullName],
OutPut1,
OutPut2,
OutPut3,
( OutPut1 + OutPut2 + OutPut3 ) AS Total,
CASE
WHEN Name IS NULL THEN '.........'
ELSE '??????'
END AS ParentID
FROM CTE_Report;
I made a select using the WITH query something like this:
WITH test AS (select id, sum(value) as value from test group by id)
SELECT name, t.value from names JOIN test t ON (t.id = names.id)
But I need to use 2 WITH queries, is there any way to make it?
WITH
name1 AS (SELECT ...),
name2 AS (SELECT ...)
SELECT ...