Custom order after distinct statements - sql

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).

Related

Select a third column based on two distant rows within the same table

I want to select a third column based on two distant columns within the same table.
I could only think of this:
select tl.thirdcolumn
from table1 t1
WHERE
EXISTS
(
Select distinct tl.firstcolumn , t1.secondcolumn
From t1
)
This:
select distinct tl.thirdcolumn
from table t1
won't work as I don't want the distinct thirdrow. I want the thirdrow to be based on the first two rows being distinct.
I guess its a kind of nested sql statment with a select top 1... idk
CATEGORY NAME Query
---------------------------------------------------
STUDENTS NUMBER_OF_CHAPTERS QueryA
STUDENTS NUMBER_OF_STUDENT_MEMBERS QueryB
STUDENTS NUMBER_OF_STUDENT_MEMBERS QueryB
MEMBERS NUMBER_OF_MEMBERS_WORLDWIDE QueryC
MEMBERS NUMBER_OF_MEMBERS_WORLDWIDE QueryC
Your question is rather hard to follow, but I think you might simply want group by:
select tl.firstcolumn , t1.secondcolumn, max(tl.thirdcolumn)
from table1 t1
group by tl.firstcolumn , t1.secondcolumn;
If you want rows where the pair of values only appears once, then add having count(*) = 1:
select tl.firstcolumn , t1.secondcolumn, max(tl.thirdcolumn)
from table1 t1
group by tl.firstcolumn , t1.secondcolumn
having count(*) = 1;
Query -
SELECT
CATEGORY,NAME,QUERY
FROM
(
WITH TAB AS (
SELECT
'STUDENTS' AS CATEGORY,
'NUMBER_OF_CHAPTERS' AS NAME,
'QUERYA' AS QUERY
FROM
DUAL
UNION ALL
SELECT
'STUDENTS' AS CATEGORY,
'NUMBER_OF_STUDENT_MEMBERS' AS NAME,
'QUERYB' AS QUERY
FROM
DUAL
UNION ALL
SELECT
'STUDENTS' AS CATEGORY,
'NUMBER_OF_STUDENT_MEMBERS' AS NAME,
'QUERYB' AS QUERY
FROM
DUAL
UNION ALL
SELECT
'MEMBERS' AS CATEGORY,
'NUMBER_OF_MEMBERS_WORLDWIDE' AS NAME,
'QUERYC' AS QUERY
FROM
DUAL
UNION ALL
SELECT
'MEMBERS' AS CATEGORY,
'NUMBER_OF_MEMBERS_WORLDWIDE' AS NAME,
'QUERYC' AS QUERY
FROM
DUAL
) SELECT
CATEGORY,
NAME,
QUERY,
COUNT(*) OVER(PARTITION BY
CATEGORY,
NAME
ORDER BY
CATEGORY,
NAME,
QUERY
) AS RNK
FROM
TAB
)
WHERE
RNK = 1;
Output -
"CATEGORY","NAME","QUERY"
"STUDENTS","NUMBER_OF_CHAPTERS","QueryA"

How to get join multiple queries

In this http://sqlfiddle.com/#!6/aa0e17/4 as you can see id is primary key and auto incremented and values column is int type. To retrieve count based on value I am doing 4 different queries
select count(id) from status where value=1
select count(id )from status where value=2
select count(id) from status where value=3
select count(id) from status where value=4
My requirement is to get all those counts in a single query.
Why I want?
The above table is just a demo table and have only 4 queries but in my scenario I have 35 queries and so I have to do 35 methods in java.
Expected output: 4,,4,4,4 (1st query result, 2nd query result, 3rd query result, 4th query result)
select value, count(id)
from status
group by value
Here are the 2 normal ways of solving it:
Example 1 PIVOT:
SELECT [1] count1,[2] count2,[3] count3,[4] count4
FROM
(
SELECT id, value
FROM status
) p
PIVOT (COUNT(id) FOR [value] IN ([1], [2], [3], [4])) AS pvt
Use CONCAT if you want to combine the columns into one.
To do this, replace first row in first example with:
SELECT CONCAT([1],',',[2],',',[3],',',[4])
Example 2 CASE:
SELECT
COUNT(CASE WHEN value = 1 THEN 1 END) count1,
COUNT(CASE WHEN value = 2 THEN 1 END) count2,
COUNT(CASE WHEN value = 3 THEN 1 END) count3,
COUNT(CASE WHEN value = 4 THEN 1 END) count4
FROM status
You should better count rows and group by the value by the following query:
SELECT COUNT(*) FROM status GROUP BY value
or for a better description and look try this:
SELECT value, COUNT(*) AS COUNT FROM status GROUP BY value
Use UNION (or UNION ALL to preserve duplicate values) like:
select count(id) from status where value=1
UNION
select count(id )from status where value=2
UNION
select count(id) from status where value=3
UNION
select count(id) from status where value=4
Have a look at a similar question here: https://stackoverflow.com/a/6066234
[Edit 1]
Check the fiddle, it works on my machine ;) http://sqlfiddle.com/#!6/b89ef/1/0
Since I removed a (3) from the insert, you get 4,3 (I'm only selecting fours and threes here).
[Edit 2]
I did not catch the part where you wanted it all on one line.
Just wrap a SELECT around your statements like http://sqlfiddle.com/#!6/aa0e17/34/0:
select
(select count(id) from status where value=1),
(select count(id) from status where value=2),
(select count(id) from status where value=3),
(select count(id) from status where value=4)
;
And your result is one row with 4,4,4,4 as result.
If what you're looking for is a comma-delimited string, then this might be helpful:
WITH CTE(N) AS(
SELECT COUNT(ID) FROM STATUS WHERE VALUE=1 UNION ALL
SELECT COUNT(ID )FROM STATUS WHERE VALUE=2 UNION ALL
SELECT COUNT(ID) FROM STATUS WHERE VALUE=3 UNION ALL
SELECT COUNT(ID) FROM STATUS WHERE VALUE=4 UNION ALL
)
SELECT STUFF((
SELECT N', ' + CONVERT(VARCHAR(10), N)
FROM CTE FOR XML PATH(''), TYPE
).value('text()[1]','nvarchar(max)')
, 1 , 2 , N'')

SQL add ALL to select statement

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]

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 ;

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