ORACLE order by MONTH - sql

How can I order by month in full name of the month?
And I want to get the results like this below:
FEBRUARY 0 80
MARCH 0 58
APRIL 0 39
This is my 1st simple script:
select to_char(month,'MONTH')month,mo_incoming,mt_outgoing
from t_raw_settlement_tara_yearly
order by month
output:
APRIL 0 39
FEBRUARY 0 80
MARCH 0 58
2nd Script is almost right but I want the month to be in full MONTH
select to_date(to_char(month,'MONTH'),'MONTH')month,mo_incoming,mt_outgoing
from t_raw_settlement_tara_yearly
order by month
output:
2/1/2015 0 80
3/1/2015 0 58
4/1/2015 0 39

You alias to_char(month,'MONTH') to month. Now when you use month in ORDER BY it references the month string. Either use a different alias or qualify the column:
select to_char(month,'MONTH')month,mo_incoming,mt_outgoing
from t_raw_settlement_tara_yearly t
order by t.month

Related

How to order by for customer week number which contains year number too

I have a table with one row
employee(date)
date
52 week in 2021
23 week in 2022
34 week in 2021
1 week in 2022
52 week in 2022
I tried the below query but it's not working
select date from employee order by date desc
I want to order by in desc for above column. I'm expecting output like below
date
52 week in 2022
23 week in 2022
1 week in 2022
52 week in 2021
34 week in 2021
Extracting year and week part and then performing sort:
SELECT *
FROM tab
ORDER BY RIGHT(date,4) DESC, CAST(LEFT(date,2) AS INT) DESC

How Do I retrieve most Recent record in different years With Date date in different table

I'm working with a database that isn't structured that well and need to retrieve the row with the latest month used in specific years. The main data is stored is stored in the member table and lists one row per member month. The Date for the member month is not specifically stored here but connected by a foreign Date_Key and linked to a Date table. This is where the column for the Year and Month can be derived based on the Date_Key specified in each table. Each row in the Date table represents 1 new month for a year and each of these rows has a unique sequential date_key.
I am using Microsoft SQL Server Studio as the environment
Member Table
MemberKey
Membe_ID
Date_Key
100
1234
89
101
1234
96
102
1234
97
103
1236
96
104
1236
97
Date Table
Date_Key
Year
Month
89
2020
10
90
2020
11
91
2020
12
92
2021
1
93
2021
2
94
2021
3
95
2021
4
96
2021
5
97
2021
6
Looking for the following Results
Member_ID
Year
Month
1234
2020
10
1234
2021
6
1236
2021
6
2020/11 is NOT a date. It is a year/month pair. But it seems like a simple aggregate - select year, max(month) group by year. You join and include member ID so you include that column in the GROUP BY clause to get one row per member per year.
select mbr.Member_ID, dts.Year, max(dts.Month) as Month
from dbo.Members as mbr
inner join dbo.Dates as dts on mbr.Date_Key = dts.Date_Key
group by mbr.Member_ID, dts.Year
order by mbr.Member_ID, dts.Year
;

How to sum with specific range of records

I have table
tax number yearmonth(int)
100 45 202105
2 45 202104
35 45 202102
47 45 202012
58 45 202005
I try to aggregate sum for every number by last 12 month
For instance 202105 - I need sum month between (202012 - 202001)
Main problem -> not every number has all 12 months
I tried over clause but it sums all 12 preceding records. It does not take into account missing year records.
case when yearmonth-lag(yearmonth,1) OVER ( order by number, yearmonth) <> (0) then
sum([tax]) OVER (
PARTITION BY [number]
ORDER BY yearmonth
Rows BETWEEN 11 PRECEDING AND CURRENT ROW ) end

Get YTD sum value

The first two columns work great but I do not know how to get the results for the third column. Please tell me how I can have the third column to show the year to date data with accordance to the week. Please provide assistance.
select "Builder","Traffic", sum(cast("Traffic" as int)) as YTD
from trafficdatapcr
where "Week" = '2016-12-11'
group by "Builder","Traffic"
The sample data:
Week Builder Traffic
2016-12-11 Macys 100
2016-10-11 Bloomingdales 15
2016-08-11 Saks 85
2016-02-11 Cole Haan 95
2015-12-25 Kroger 65
My current results:
Builder Traffic YTD
Macys 100 100
The expected results:
Builder Traffic YTD
Macys 100 100
Saks 0 85
Bloomingdales 0 15
Cole Haan 0 95
Kroger 0 65
Your where clause is eliminating the the records you desire, use a case to conditionally display the traffic for the desired week instead of the where clause
select "Builder"
, case when "Week" = to_date('2016-12-11',YYYY-MM-DD') then "Traffic" else 0 end as "Traffic"
, sum(cast("Traffic" as int)) as YTD
from trafficdatapcr
group by "Builder","Traffic"
Order by week Desc
It does seem odd though that if someone were to select 2016-10-11 the YTD would be all dates.... so perhaps you want to conditionally sum as well...
select "Builder"
, case when "Week" = to_date('2016-12-11','YYYY-MM-DD') then "Traffic" else 0 end as "Traffic"
, sum(case when "week"<=to_date('2016-12-11','YYYY-MM-DD') then cast("Traffic" as int) else 0 end) as YTD
from trafficdatapcr
group by "Builder","Traffic"
Order by week Desc
This way
Macys will show as 0 0
Bloomingdales would be 15 15
So 2nd query should return (assumign date of 2016-10-11) but in correct date order (don't know what order you want)
Builder Traffic YTD
Macys 0 0
Saks 0 85
Bloomingdales 15 15
Cole Haan 0 95
Kroger 0 65

How to fetch data according to date in sql

I have a table truck_data with columns truck_no, diesel_filled, source, destination, amount etc
I want to fetch only truck_no and diesel_filled in such way that it will show the details of diesel_filled in each truck through out the month..
Please tell me the SQL query for that - I had tried this query but it's not working
SELECT
truck_no, diesel_filled, date
FROM
truck-data
ORDER BY
date
Please help me out
Thanks in advance
I want output like this
truck_no 1 2 3 4 5 6 7 8 9 etc(date from 1 to 31))
---------------------------------------------------------------------------
xyz 25 22 33 33 22 22 22 0 0 (diesel filled in truck order by date)
pqr 25 25 22 11 22 00 22 55 22
abc 21 15 12 14 13 00 22 00 00
It's still quite unclear what you want. But I think you are mixing presentation and data
SELECT truck_no,diesel_filled,date
FROM truck-data
ORDER BY date
Will give you all rows ordered by date. Presumably you whant some filter on that to only show a specific month.
SELECT truck_no,diesel_filled,date
FROM truck-data
WHERE date >= 2014-10-05 AND date < 2014-11-01 ORDER BY date
To get all the rows for october this year. If date here is a DateTime column.
Then you could change it to:
SELECT truck_no,sum(diesel_filled),Day(date)
FROM truck-data
WHERE date >= 2014-10-05 AND date < 2014-11-01
GROUP BY truck_no
ORDER BY date"
That would give you only one row per day and truck. If disel_filled is a numeric value of some kind.
Then in your UI you would have to do the presentation in any way you prefer. For example in some kind of pivot table like your description above.
You could of course do that in SQL as well, but usually that is a job for the presentation layer.
If you really want to do in SQL you could look into: http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
If you'r using MsSql.