SQL add ALL to select statement - sql

I have a select statement which displays a list of companies.
SELECT distinct [Company]
FROM [Records]
How can I add the entry "ALL" as the first item on the returned list?

Use union all and order by:
select company
from ((select distinct company, 1 as ordering from records)
union all
(select 'ALL', 0)
) t
order by ordering;
In practice, the following would seem to work:
select 'ALL' as company
union all
select distinct company from records;
However, SQL Server does not guarantee that these are executed in order. In practice, I have never found a case where this statement would not put ALL first, but it is not guaranteed as far as I know.

You can use UNION ALL and add order:
SELECT [Company] FROM
(SELECT 'All' as [Company], 0 as RecordOrder
UNION ALL
SELECT distinct [Company], 1 as RecordOrder
FROM [Records]) X
ORDER BY RecordOrder

try with union all
SELECT 'ALL'
UNION ALL
SELECT distinct [Company]
FROM [Records]

SELECT 'ALL'
UNION ALL
SELECT distinct [Company]
FROM [Records]

Related

SQL - Insert NULL entry with Union then order by results

Im trying to order a list of results by their description (not id) but i also need to insert another result with a null Id and description of 'ALL'.
To do this I use UNION SELECT NULL, 'ALL'
It should be a pretty simple query but i cant figure out how to insert 'ALL' before the query orders the actual result set.
Here is what I have so far:
SELECT *
FROM [dbo].[HouseType]
WHERE [TypeId] = COALESCE(#TypeId, [TypeId])
UNION SELECT NULL, 'ALL'
ORDER BY [TypeDesc]
Ive also tried calling UNION SELECT before the results are ordered but it doesnt look SQL allows this.
UNION SELECT NULL, 'ALL'
SELECT * FROM
(
SELECT *
FROM [dbo].[HouseType]
WHERE [TypeId] = COALESCE(#TypeId, [TypeId])
ORDER BY [TypeDesc]
)types
Basically I want to see the following results (ordered):
ALL
Bungalow
Detached
Semi-Detached
Terrace
Try using another column:
SELECT t.ID, t.description, 2 AS o
FROM [dbo].[HouseType] AS t
WHERE [TypeId] = COALESCE(#TypeId, [TypeId])
UNION ALL
SELECT NULL, 'ALL', 1 AS o
ORDER BY o, [TypeDesc]
This way ALL will always precede the results of your original query.
Note: As already noted in a comment there must be a match between the number and type of fields of the subqueries used in the UNION operation.
Note 2: As noted in the other comment by #A ツ using UNION ALL is always preferable over UNION, so use this instead when you have no worries about duplicate values.
Try it in a subquery
SELECT *
FROM(
SELECT *
FROM [dbo].[HouseType]
WHERE [TypeId] = COALESCE(#TypeId, [TypeId])
UNION
SELECT NULL, 'ALL')
ORDER BY [TypeDesc]
DECLARE #HouseType TABLE (HouseType VARCHAR(30))
INSERT INTO #HouseType
SELECT HouseType
FROM [dbo].[HouseType]
WHERE [TypeId] = COALESCE(#TypeId, [TypeId])
ORDER BY [TypeDesc]
INSERT INTO #HouseType
SELECT 'ALL'
SELECT HouseType FROM #HouseType ORDER BY HouseType
I think you are looking for conditional sorting:
SELECT *
FROM [dbo].[HouseType]
WHERE [TypeId] = COALESCE(#TypeId, [TypeId])
UNION ALL
SELECT NULL, 'ALL'
ORDER BY CASE WHEN [TypeDesc] = 'ALL' THEN 0 ELSE 1 END, [TypeDesc]
btw, I would suggest agains the use of * and explicitly specify the column names in the select list.
This is always best practice, espcially when using UNION - this query will raise an error if you ever add (or remove) a column from the HouseType table.
Assigns a Row to each TypeDesc using ROW_NUMBER function (+1 to go after the 'ALL') sorted by TypeDesc:
select 1 as [Row], 'All' as [TypeDesc]
union
SELECT ROW_NUMBER() OVER (ORDER BY TypeDesc) + 1 AS [Row], [TypeDesc]
FROM [dbo].[HouseType]
WHERE [TypeId] = COALESCE(#TypeId, [TypeId])
ORDER BY [Row]

SQL Having count logic

i need help on HAVING COUNT , i have a result set of data below:
CREATE TABLE #tmpTest1 (Code VARCHAR(50), Name VARCHAR(100))
INSERT INTO [#tmpTest1]
(
[Code],
[Name]
)
SELECT '160215-039','ROBIN'
UNION ALL SELECT '160215-039','ROBIN'
UNION ALL SELECT '160215-046','SENGAROB'
UNION ALL SELECT '160215-046','BABYPANGET'
UNION ALL SELECT '160215-045','JONG'
UNION ALL SELECT '160215-045','JAPZ'
UNION ALL SELECT '160215-044','AGNES'
UNION ALL SELECT '160215-044','AGNES'
UNION ALL SELECT '160215-041','BABYTOT'
UNION ALL SELECT '160215-041','BABYTOT'
UNION ALL SELECT '160215-041','BABYTOT'
i want to show only the rows that have the same code but different name , so in this case my expected result is below since those are have the same code but different name:
160215-045 JAPZ
160215-045 JONG
160215-046 BABYPANGET
160215-046 SENGAROB
but when i try to group the two columns then use the having count, below is my query:
SELECT [Code], [Name] FROM [#tmpTest1]
GROUP BY [Code], [Name] HAVING COUNT([Code]) > 1
It gives me wrong result below which have the rows that have the same code and name, it is the opposite of what i want.
160215-044 AGNES
160215-041 BABYTOT
160215-039 ROBIN
How can i get my expected output ?
Thanks in advance, any help would much appreciated.
I believe this query will give you the result you want, although your original question is a bit unclear.
SELECT t1.[Code], t1.[Name]
FROM [#tmpTest1] t1
INNER JOIN
(
SELECT [Code]
FROM [#tmpTest1]
GROUP BY [Code]
HAVING COUNT(DISTINCT [Name]) > 1
) t2
ON t1.[Code] = t2.[Code]
Follow the link below for a running demo:
SQLFiddle
If you want rows with the same code and name, then use window functions:
select t.*
from (select t.*, count(*) over (partition by code, name) as cnt
from #temptest1 t
) t
where cnt >= 2;
From your comment
if there is 1 different name for the codes , i want to show those
records for me to know that there is one differs to others..
This sounds like an exists query because you want to check if another row with the same code but different name exists.
select * from [#tmpTest1] t1
where exists (
select 1 from [#tmpTest] t2
where t2.code = t1.code
and t2.name <> t1.name
)

multiple select in one query [Teradata]

I'm trying to do multiple select from diff tables and just have a result in one column.
SELECT COUNT(*) FROM tb1 union
SELECT COUNT(*) FROM tb2 union
SELECT COUNT(*) FROM tb3;
output should be like:
593643
18103600
0
Problem with this is that the result is being arranged on desc order.
Like below:
0
593643
18103600
I would want the result to be as I put the select statement.
Please advise. Btw, I'm using teradata.
Thank you.
SQL result sets are inherently unordered, unless you explicitly specify an order by clause. You can do this with a subquery:
select cnt
from ((SELECT COUNT(*) as cnt, 1 as ord FROM tb1)
union all
(SELECT COUNT(*), 2 FROM tb2)
union all
(SELECT COUNT(*), 3 FROM tb3)
) t
order by ord
If you want specific order, add ORDER BY clause. It would also be good to use UNION ALL so you always get 3 rows, even with duplicate results (two tables having the same number of rows):
SELECT 'tbl1' AS tablename, COUNT(*) AS cnt, 1 AS ord FROM tb1 UNION ALL
SELECT 'tbl2', COUNT(*), 2 FROM tb2 UNION ALL
SELECT 'tbl3', COUNT(*), 3 FROM tb3
ORDER BY ord ;

Custom order after distinct statements

I want to make a custom order: IN, OUT, FINISHED.
If I left case statement I am getting in FINISHED, IN, OUT.
I found this solution but it does not work for me - I am getting error.
select distinct 'IN' as STATUS,
(select count(*) ...)
from table
UNION ALL
select distinct 'OUT',
(select count(*) ...)
from table
UNION ALL
select distinct 'FINISHED',
(select count(*) ...)
from table
order by
case STATUS
when 'IN' then 1
when 'OUT' then 2
when 'FINISHED' then 3
end
The query that you provided has some syntax irregularities. I think the following solves your problem:
select *
from ((select distinct 'IN' as statusA, (select count(*) ...
from table
)
union all
(select distinct 'OUT', (select count(*) ...)
from table
)
union all
(select distinct 'FINISHED', (select count(*) ...)
from table
)
) t
order by status,
(case STATUSA when 'IN' then 1
when 'OUT' then 2
when 'FINISHED' then 3
end)
Your original problem could have several causes. You were missing the 'IN' in the first subquery. You are missing the comma after status in the order by. And, some databases apply the final order by in a series of unions to only the last query (although I think DB2 does this correctly).

ORDER BY CASE does not working?

Hi I have SQL statement in DB2 which is working.
select distinct 'IN' as STATUS,
(select count(*) from table.......)
from table
UNION ALL
select distinct 'OUT',
(select count(*) from table.......)
from table
UNION ALL
select distinct 'FINISHED',
(select count(*) from table.......)
from table
order by status
But if I change the last line to
order by
case STATUS
when 'IN' then 1
when 'OUT' then 2
when 'FINISHED' then 3
end
My query does not work.
Can someone tell me how to solve this?
Thanks
Try wrapping the UNION into a derived table and order on that:
select *
from (
.... here goes your statement ...
) t
order by
case STATUS
when 'IN' then 1
when 'OUT' then 2
when 'FINISHED' then 3
end
you could always add the sort # to the status:
select distinct '1-IN' as STATUS,
(select count(*) from table.......)
from table
UNION ALL
select distinct '2-OUT',
(select count(*) from table.......)
from table
UNION ALL
select distinct '3-FINISHED',
(select count(*) from table.......)
from table
order by status
hello try this should work if i remember correctly
order by
case
when STATUS='IN' then 1
when STATUS='OUT' then 2
when STATUS='FINISHED' then 3
end
you could also name this when finishing
end as field_name