How to get results in a custom order? [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Instead of ASC or DESC, I want my query results to be in a specific, custom order.
For example, instead of A, B, C, D..., what if I wanted my results in, P, A, L, H...?
I have tried using case but not successfully
SELECT * FROM Customers
ORDER BY case country
when 'P' then 1 …
E.g., here, I'm trying to create a custom order on the Country column:

SELECT * FROM Customers
ORDER BY case when country = 'P' then 1
when country = 'A' then 2
when country = 'L' then 3
when country = 'H' then 4
else 5
end asc

Related

case when with count [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a quick sql data as below,
Customerid,Type
1,Adult
1,Adult
2,Adult
3,Adult
4,Teenager
4,Adult
I want the query that lists those customer no.s that do not have any other Type associated with them. For eg. 1 as only Adult associated with, same with 2. But 3 and 4 have multiple Types associated with them.
I am trying to get an output as below.
Customerid,Type
1,Adult
2,Adult
3,null
How should we tackle this.
You seem to want:
select customerid
, (case when min(type) = max(type) then min(type) end) as type
from table t
group by customerid;

Count by ID if value is lower than and set the value 1 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a problem.
My table is tableXYZ
I have a row a where i set the year.
I have a row b where i need to set value 1 if the year from row a is lower than 2019 and 0 if is > 2019.
After all i need to make a count from row b only the 1 values as total.
How can i do that, because i tried a lot of examples but doesn't work.
This might help:
SELECT COUNT(1) AS [b_count]
FROM (
-- Use case to evaluate column a and set value for column b
SELECT a,
CASE WHEN a < 2019 then '1' else '0' end as [b]
FROM tableXYZ
) AS X -- Alias of subquery
WHERE X.b = '1' -- Select only rows where b = '1'

Calculate BACKLOG_M_1 [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I need to calculate the BLACKLOG_M value corresponding to the Snapdate of the first of the month. It becomes a constant value repeated for each line of the month
As it is explained in the picture:
SELECT T1.SNAP, T1.PERI, T1.ANNEE, T1.MOIS, T1.[BCKL], T2.[BCKL] AS BCK2,[NBR1]
,[NBR2]
FROM stg.FACTSALES AS T1 LEFT OUTER JOIN
(SELECT YEAR(SNAP) AS ANNEE, MONTH(SNAP) AS MOIS, [BCKL]
FROM stg.FACTSALES
WHERE (DAY(SNAP) = 1)) AS T2 ON T2.ANNEE = T1.ANNEE AND T2.MOIS = T1.MOIS
The script returns the correct values, but is there any another best idea or improvement?
You want to copy the value from the first day of the month through the rest of the month. That should be possible.
select fs.*,
(case when day(snapdate) = 1
then max(case when day_snapdate() = 1 then backlog_m end) over (partition by year_month)
end) as backlog_m_1
from stg.FACTSALES fs;
This assigns the value on the first day as well. It is not clear what you want for that value.

query to check multiple revsions on a table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Need a query to check ids with multiple revisionnums
id reviionnum
1 0
2 0
1 1
3 1
2 1
The query should result
id revisionnum
3 1
Please help
If you want ids with only one revision number, you can use aggregation:
select id, min(revissionnum)
from t
group by id
having count(*) = 1;
The min() is the value if there is only one row.

Hive output query for selecting particular value based on groupby clause [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a table with data like:
cust_id, acct_no, ind
123111, 1233, Y
123111, 2311, N
222111, 1112, N
222111, 2111, N
I have to get output as cust_id, 1 (a binary indicator if any of the acct under that customer is Y)
so from the above table I have to get below output.
123111 1
222111 0
A simple way to achieve this is something like:
select cust_id, max(case when ind = 'Y' then 1 else 0 end) as flag from customers group by cust_id;