I have this database
ID Title
1 Unassaign
6 Prima
7 Adi
I want to make ID 1 to be display last but the other are sort by their title
Desired results:
ID Title
7 Adi
6 Prima
1 Unassaign
This is my code:
SELECT a.ID_WB, a.TITLE, a.DESCRIPTION, a.AUTHOR, a.DATECREATE, a.DATEUPDATE
FROM WORKBOOK a
order by case when ID_WB = 1 then 1
else 0 end
I tried insert Order By after else but it always return SQL error..
Is there a workaroud to this problem?
Thanks
PS: The first Order byshould sort by ID
Use two expressions in the order by:
order by (case when ID_WB = 1 then 1 else 0 end), title
Related
I have a set of records which I need to sort in particular order. Basically I need to sort like:
select * from Table
ORDER BY FIELD(code, 'int', (<> 'int' and <> 'nat'), 'nat')
Or
select * from Table
ORDER BY FIELD(id, 5, >= 10, 7)
How can I do it?
I think this conditional sorting meets your requirement:
select * from Table
order by case
when id = 5 then 1
when id >= 10 then 2
when id = 7 then 3
else 4
end
or for the 1st case:
select * from Table
order by case
when code = 'int' then 1
when code <> 'nat' then 2
else 3
end
From your second select statement, You can try -
select * from Table
ORDER BY CASE WHEN id = 5 THEN 1
WHEN id BETWEEN 6 AND 10 AND id NOT <> 7 THEN 2
WHEN id = 7 THEN 3 END)
You can also use multiple keys in order by:
order by (id = 5) desc, -- "5" first
(id = 7) asc -- "7" last
I am using MySQL syntax because FIELD() is a MySQL function.
So my task is to calculate some simple KPIs.
I have already accumulated a view with all the data I need.
Year_CW Is Started Needs Help
-------------------------------------
2018/45 0 1
2018/43 1 1
2018/45 0 1
2018/42 1 0
2018/45 0 1
2018/45 1 1
2018/41 0 1
2018/43 0 0
2018/45 1 1
2018/45 0 0
I then wrote the following query:
SELECT DISTINCT YEAR_CW
FROM TestView
ORDER BY YEAR_CW DESC
Which returns this
Year_CW
--------
2018/45
2018/44
2018/43
2018/42
I would now like to count for each Year_CW how often there is a 1 and how often there is a zero for both of the other rows. This may be a fairly simple question but I'm just starting with SQL and I really don't know what the keyword is for a query based on an outer query.
The other queries would be
Select Count(Is Started)
from Testview
Where Is Started = 1
And so on for the others. But I really don't know how to put them together and base them on the first query.
Thanks for your help.
select Year_CW
, sum(case when Is_Started = 1 then 1 end) as Is_Started_1
, sum(case when Is_Started = 0 then 1 end) as Is_Started_0
, sum(case when Needs_Help = 1 then 1 end) as Needs_Help_1
, sum(case when Needs_Help = 0 then 1 end) as Needs_Help_0
from Test_View
group by Year_CW
So how I did it is I created 4 new fields for you. First one is giving value ‘1’ to every field where ‘Is_Started = 1’ and then I sum the instances. I did the same for 0 values and another two fields for values 1 and 0 for ‘Needs_ Help’ column. I believe this will give you your desired result.
You seems want conditional aggregation :
select Year_CW,
sum(case when col = 1 then 1 else 0 end) as one_count,
sum(case when col = 0 then 1 else 0 end) as zero_count
from (select Year_CW, IsStarted as col
from TestView tv
union all
select Year_CW, NeedsHelp
from TestView tv
) tv
group by Year_CW
order by Year_CW desc;
So, if I'm understanding the question correctly you're just looking for the SUM of the additional two columns GROUP BY the Year_CW field. That would be the following.
SELECT Year_CW, SUM([Is Started]), SUM([Needs Help])
FROM TestView
GROUP BY Year_CW
If it's only 0 or 1, then both 0 or 1 can be summed up.
SELECT YEAR_CW,
SUM("Is Started") AS TotalStarted,
SUM(1 - "Is Started") AS TotalNotStarted,
SUM("Needs Help") AS TotalNeedsHelp,
SUM(1 - "Needs Help") AS TotalNoHelpNeeded
FROM TestView
GROUP BY YEAR_CW
ORDER BY YEAR_CW DESC
I think I need some guidance as to what is wrong in my query. I am trying to do
Watched_Gladiator=CASE WHEN FilmName IN (CASE WHEN FilmName LIKE '%Gladiator%' THEN 1 END) then OVER(PARTITION BY Cust_Nr) THEN 1 ELSE 0 END
Tried this one too:
Watched_Gladiator=CASE WHEN FilmName IN (CASE WHEN FilmName LIKE '%Gladiator%' THEN Filmnamne END) then OVER(PARTITION BY Cust_Nr) THEN 1 ELSE 0 END
The Error I am currently getting is this:
Incorrect syntax near the keyword 'OVER'.
This is basically how my data looks like
Cust_Nr Date FilmName Watched Gladiator
157649306 20150430 Gladiator 1
158470722 20150504 Nick Cave: 20,000 Days On Earth 0
158467945 20150504 Out Of The Furnace 0
158470531 20150504 FilmA 0
157649306 20150510 Gladiator 1
158470722 20150515 Gladiator 1
I want to create a column (1 or zero) that shows if the customer has watched Gladiator then 1 ELSE 0. How can I do that?
I created a test column trying with a simple LIKE '%Gladiator%' THEN 1 ELSE 0. The problem with this solution is that it will show 1(one) more than once if the customer has watched multiple times. I only need 1 or zero.
I feel I am really close to finding a solution. I am very new to using OVER() and CASE WHEN but enjoying the thrill:=)
So you're saying that:
SELECT Cust_Nr, Date, FilmName,
CASE WHEN FilmName LIKE '%Gladiator%' THEN 1 ELSE 0 END as WatchedGladiator
FROM YourTable
WHERE YourColumn = #somevalue
Doesn't work? Because according to the data you've given, it should.
EDIT:
Well based on Tim's comment, I would simply add this bit to the query.
SELECT Cust_Nr, Date, FilmName, WatchedGladiator
FROM
(
SELECT Cust_Nr, Date, FilmName,
CASE WHEN FilmName LIKE '%Gladiator%' THEN 1 ELSE 0 END as WatchedGladiator
FROM YourTable
WHERE YourColumn = #somevalue
) as wg
WHERE WatchedGladiator = 1
The following does what you want for all films:
select r.*,
(case when row_number() over (partition by filmname order by date) = 1
then 1 else 0
end) as IsWatchedFirstAndGladiator
from results r;
For just Gladiator:
select r.*,
(case when filmname = 'Gladiator' and row_number() over (partition by filmname order by date) = 1
then 1 else 0
end) as IsWatchedFirst
from results r;
So you want to group by customer and add a column if this customer watched a specific film?
You could do:
SELECT Cust_Nr, MAX(Watched_Gladiator)
FROM( SELECT Cust_Nr,
Watched_Gladiator = CASE WHEN EXISTS
(
SELECT 1 FROM CustomerFilm c2
WHERE c2.Cust_Nr = c1.Cust_Nr
AND c2.FilmName LIKE '%Gladiator%'
) THEN 1 ELSE 0 END
FROM CustomerFilm c1 ) X
GROUP BY Cust_Nr
Demo
But it would be easier if you used the customer-table instead of this table, then you don't need the group-by.
Try grouping up to the cust/film level:
select
cust_nbr,
case when film_name like '%Gladiator%' then 1 else 0 end
from
(
select
cust_nbr,
film_name
from
<your table>
group by
cust_nbr,
film_name
) t
Or, as an alternative:
select distinct cust_nbr
from
<your table>
where
filmname = 'Gladiator'
My table structure is this
id last_mod_dt nr is_u is_rog is_ror is_unv
1 x uuid1 1 1 1 0
2 y uuid1 1 0 1 1
3 z uuid2 1 1 1 1
I want the count of rows with:
is_ror=1 or is_rog =1
is_u=1
is_unv=1
All in a single query. Is it possible?
The problem I am facing is that there can be same values for nr as is the case in the table above.
Case statments provide mondo flexibility...
SELECT
sum(case
when is_ror = 1 or is_rog = 1 then 1
else 0
end) FirstCount
,sum(case
when is_u = 1 then 1
else 0
end) SecondCount
,sum(case
when is_unv = 1 then 1
else 0
end) ThirdCount
from MyTable
you can use union to get multiple results e.g.
select count(*) from table with is_ror=1 or is_rog =1
union
select count(*) from table with is_u=1
union
select count(*) from table with is_unv=1
Then the result set will contain three rows each with one of the counts.
Sounds pretty simple if "all in a single query" does not disqualify subselects;
SELECT
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_ror=1 OR is_rog=1) cnt_ror_reg,
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_u=1) cnt_u,
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_unv=1) cnt_unv;
how about something like
SELECT
SUM(IF(is_u > 0 AND is_rog > 0, 1, 0)) AS count_something,
...
from table
group by nr
I think it will do the trick
I am of course not sure what you want exactly, but I believe you can use the logic to produce your desired result.
I have a table with columns: id and created(datetime)
id created
6 2011-11-04 20:32:09.673
5 2011-11-04 20:32:09.673
4 2011-11-04 20:29:55.000
3 2011-11-04 20:29:55.000
How to write sql, that will return counts of records that haven't, have 2 or have more than 2 equal creation dates. It can be 3 separate sqls.
Thank you
If I understand your question correctly, I believe this should do the trick, returning any creation dates that occur more than once.
This one will get those with more than 2.
SELECT
created
,COUNT(*) as [occurrences]
FROM
tableName
GROUP BY
created HAVING COUNT(*) > 2
Swap the > 2 for an = 2 to get those with 2 exactly and =1 to get those with only 1 occurrence.
Try:
select case reccount
when 1 then 'No matches'
when 2 then 'Exactly 2 matches'
else 'More than 2 matches'
end as number_of_matches,
count(*) as distinct_dates,
sum(reccount) as record_count
from (select created,
count(*) reccount
from mytable
group by created) v
group by
case reccount
when 1 then 'No matches'
when 2 then 'Exactly 2 matches'
else 'More than 2 matches'
end