I got a simple thing to do.
Well, maybe not, but someone somewhere surely can help me out : P
I got a simple data structure that contains
expedition date
delivery date
transaction type
I would need to create a query which could
order the rows by a date specific to the transaction type.
(ie : using the expedition date for transaction of type "selling", and delivery date for transaction of type "purchasing")
I was wondering if there was a more efficient way to do this than
by fetching 2 times the same data with different clause where(while adding a column used to order them(tempDate)) and then using another select to encompass these 2 queries to which I would add the order clause on the tempDate.
--> the initial fetching I would do 2 times works on many tables(many, many, many joins)
Basically my current solution is :
Select * from
(
Select ...
date_exp as dateTemp;
from ...
where conditions* And dateRelatedCondition
UNION
Select ...
date_livraison as dateTemp;
from ...
Where conditions* And NOT(dateRelatedCondition)
) as comboSelect
Order By MIN(comboSelect.dateTemp)
OVER(PARTITION BY(REF_product)),
(REF_product),
comboSelect.dateTemp asc;
*
->Those conditions are the same in both inner Select query
Thank you for your time.
Without the UNION:
dateRelatedCondition should be removed from WHERE and put to the SELECT like:
CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END as dateTemp
Without the subquery:
in ORDER BY you need the same expression in the window function:
Order By MIN(CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END)
OVER(PARTITION BY(REF_product)),
(REF_product),
dateTemp asc
You mean like this?:
ORDER BY CASE
WHEN TransactionType = 'Selling' THEN ExpeditionDate
WHEN TransactionType = 'purchasing' THEN DeliveryDate
END
I have an application which handles school vacation. Unfortunately there are three kinds of different school vacations: Country wide, Federal State wide and City wide vacations. I store all the information in a table days, a table vacation_periods and a connection table slots:
days {
id:integer
date_value:date
}
slots {
id:integer
day_id:integer
vacation_period_id:integer
}
vacation_periods {
id:integer
starts_on:date
ends_on:date
name:string
country_id:integer
federal_state_id:integer
city_id:integer
}
I want to select all days within a specific time frame. Let's say Jan 1st of 2017 to Jan 31st of 2017. I can get those days with:
SELECT * FROM days WHERE date_value >= '2017-01-01' AND
date_value <= '2017-01-31';
But for my vacation calendar I don't just need the days but also the information which vacation_periods are within. Assuming I search for all vacation_periods which are in that time frame and which have
country_id == 1 or federal_state_id == 5 or city_id == 30
I've read about JOINS and LEFT JOINS which seem to be the solution to the problem. But I can't get everything together.
Is it possible to send one SQL request which returns all days within the requested time frame and the additional information if a vacation_period that fits the country_id == 1 or federal_state_id == 5 or city_id == 30 rule is connected via slots to each day. Including the name of that vacation_period?
If one request is not possible: Which is the quickest way to solve this within the database? How many requests? What kind of requests?
If possible I'd like to get a result in some kind of this form:
- date_value: "2017-01-01"
- date_value: "2017-01-02"
- date_value: "2017-01-03"
* vacation_period.id: 15
* vacation_period.name: "foobar"
- date_value: "2017-01-04"
* vacation_period.id: 15
* vacation_period.name: "foobar"
- date_value: "2017-01-05"
* vacation_period.id: 15
* vacation_period.name: "foobar"
- date_value: "2017-01-06"
- date_value: "2017-01-07"
...
The following query might give you the answer you are looking for:
SELECT * FROM days WHERE date_value >= '2017-01-01' AND date_value <='2017-01-31'
INNER JOIN slots ON days.id = slots.day_id
INNER JOIN vacation_periods ON vacation_periods.id = slots.vacation_period_id
I think you can get an unformatted version of what you want (that could be processed into a hierarchical output) with
CREATE TYPE vacation_authority AS ENUM
('COUNTY', 'FED-STATE', 'CITY');
/* not necessary, but cleans up the vacation_period table */
change to let vacation_period have only one id, and a new field authority of type vacation_authority. You can now make a primary key out of either the id field or (id, authority), depending on how the vacation data comes into the system.
SELECT date_value, vp.name, vp.id /* is the ID meaningful or arbitrary? */
FROM dates LEFT JOIN vacation_periods vp
WHERE date_value BETWEEN vp.starts_on AND vp.ends_on; -- inclusive range
Now if there are multiple holidays spanning a given date, this will be multiple records in the output. It's not clear what you want in this case.
None of the other answers was able to solve my problem but they let me to the solution so I'm grateful for them. Here's the solution:
SELECT days.date_value, slots.period_id, vacation_periods.name FROM days
LEFT OUTER JOIN slots ON (days.id = slots.day_id)
LEFT OUTER JOIN vacation_periods ON (slots.period_id = vacation_periods.id)
WHERE days.date_value >= '2017-01-05'
AND days.date_value <='2017-01-15'
AND (vacation_periods.id IS NULL
OR vacation_periods.country_id = 1
OR vacation_periods.federal_state_id = 5)
ORDER BY days.date_value;
I got a few problems when trying to fetch only specific data. First I don't know how to create a sql query (current sql query I can grab only one user) so I can grab the data like this.
Second I want to grab 1 year data until current date. Below is my sql query done so far (I need to do it manual one by one).
SELECT type, COUNT(*) FROM (
TABLE_DATE_RANGE([githubarchive:day.events_],
TIMESTAMP('2013-1-01'),
TIMESTAMP('2015-08-28')
)) AS events
WHERE type IN ("CommitCommentEvent","CreateEvent","DeleteEvent","DeploymentEvent","DeploymentStatusEvent","DownloadEvent","FollowEvent",
"ForkEvent","ForkApplyEvent","GistEvent","GollumEvent","IssueCommentEvent","IssuesEvent","MemberEvent","MembershipEvent","PageBuildEvent",
"PublicEvent","PullRequestEvent","PullRequestReviewCommentEvent","PushEvent","ReleaseEvent","RepositoryEvent","StatusEvent","TeamAddEvent",
"WatchEvent") AND actor.login = "datomnurdin"
GROUP BY type;
Reference:
https://www.githubarchive.org/
https://github.com/igrigorik/githubarchive.org
Here is how to properly pivot the data:
SELECT actor.login,
ifnull(sum(if(type='CommitCommentEvent',1,null)),0) as CommitCommentEvent,
ifnull(sum(if(type='CreateEvent',1,null)),0) as CreateEvent,
ifnull(sum(if(type='DeleteEvent',1,null)),0) as DeleteEvent,
ifnull(sum(if(type='DeploymentEvent',1,null)),0) as DeploymentEvent,
ifnull(sum(if(type='DeploymentStatusEvent',1,null)),0) as DeploymentStatusEvent,
ifnull(sum(if(type='DownloadEvent',1,null)),0) as DownloadEvent,
ifnull(sum(if(type='FollowEvent',1,null)),0) as FollowEvent,
ifnull(sum(if(type='ForkEvent',1,null)),0) as ForkEvent,
ifnull(sum(if(type='ForkApplyEvent',1,null)),0) as ForkApplyEvent,
ifnull(sum(if(type='GistEvent',1,null)),0) as GistEvent,
ifnull(sum(if(type='GollumEvent',1,null)),0) as GollumEvent,
ifnull(sum(if(type='IssueCommentEvent',1,null)),0) as IssueCommentEvent,
ifnull(sum(if(type='IssuesEvent',1,null)),0) as IssuesEvent,
ifnull(sum(if(type='MemberEvent',1,null)),0) as MemberEvent,
ifnull(sum(if(type='MembershipEvent',1,null)),0) as MembershipEvent,
ifnull(sum(if(type='PageBuildEvent',1,null)),0) as PageBuildEvent,
ifnull(sum(if(type='PublicEvent',1,null)),0) as PublicEvent,
ifnull(sum(if(type='PullRequestEvent',1,null)),0) as PullRequestEvent,
ifnull(sum(if(type='PullRequestReviewCommentEvent',1,null)),0) as PullRequestReviewCommentEvent,
ifnull(sum(if(type='PushEvent',1,null)),0) as PushEvent,
ifnull(sum(if(type='ReleaseEvent',1,null)),0) as ReleaseEvent,
ifnull(sum(if(type='RepositoryEvent',1,null)),0) as RepositoryEvent,
ifnull(sum(if(type='StatusEvent',1,null)),0) as StatusEvent,
ifnull(sum(if(type='TeamAddEvent',1,null)),0) as TeamAddEvent,
ifnull(sum(if(type='WatchEvent',1,null)),0) as WatchEvent,
FROM (
TABLE_DATE_RANGE([githubarchive:day.events_],
DATE_ADD(CURRENT_TIMESTAMP(), -1, "YEAR"),
CURRENT_TIMESTAMP()
)) AS events
WHERE type IN ("CommitCommentEvent","CreateEvent","DeleteEvent","DeploymentEvent","DeploymentStatusEvent","DownloadEvent","FollowEvent",
"ForkEvent","ForkApplyEvent","GistEvent","GollumEvent","IssueCommentEvent","IssuesEvent","MemberEvent","MembershipEvent","PageBuildEvent",
"PublicEvent","PullRequestEvent","PullRequestReviewCommentEvent","PushEvent","ReleaseEvent","RepositoryEvent","StatusEvent","TeamAddEvent",
"WatchEvent")
GROUP BY 1
limit 100
Tables
tblTracker: [ID, Date]
tblTrackerHours: [tblTracker.ID, Hour]
tblTrackerWTGStatus: [tblTracker.ID, TurbineID, Hour, TurbineStatus]
Desired Output
Columns: Date | Hour (from tblTrackerHours) | TurbineIDs .....>
Data: Ordered dates | 1-24 for each date | TurbineStatus
I'm struggling understand where to begin with creating an SQL statement that will do this. Not every TurbineID has an entry for each hour or each day.
Any hints welcomed :-)
I think what you need is a view, which you can create as follows:
create view desiredView as
select
t.date as DATEorWhateverYouWantToNameThisColumn,
h.hour as HOURorWhateverYouWantToNameThisColumn,
s.turbineId as TirbuneIDorWhateverYouWantToNameThisColumn,
s.turbineStatus as STATUSorWhateverYouWantToNameThisColumn
from
tblTracker t,
tblTrackerHours h,
tblTrackerWTGStatus s
where
h.id = t.id AND
s.id = t.id;
Please note that, h.id and s.id in where clause actually the columns which are foreign keys in relevant tables.You can set each column's name using a word after AS. You can find the syntax of the command here.
After you have this view, you can select from it as you wish
select * from desiredView
I got a table with a date-column and a column like this:
Column
01-00-00-00
01-02-00-00
03-06-00-00
05-13-00-00
....
What I want to do: Find out how many entries start with "01","02","03",... for every day.
Therefore I need to first substract the first 2 characters, count them and group by the date.
I have a code that works fine, but needs really long. Do you have an idea for a more simple query?
SELECT
Date,
sum(IIF(Platz='03',1,0)) as Gang_03,
sum(IIF(Platz='04',1,0)) as Gang_04,
sum(IIF(Platz='05',1,0)) as Gang_05,
sum(IIF(Platz='06',1,0)) as Gang_06,
sum(IIF(Platz='07',1,0)) as Gang_07,
sum(IIF(Platz='08',1,0)) as Gang_08,
sum(IIF(Platz='09',1,0)) as Gang_09,
sum(IIF(Platz='10',1,0)) as Gang_10,
sum(IIF(Platz='11',1,0)) as Gang_11,
sum(IIF(Platz='12',1,0)) as Gang_12,
sum(IIF(Platz='13',1,0)) as Gang_13,
sum(IIF(Platz='14',1,0)) as Gang_14,
sum(IIF(Platz='15',1,0)) as Gang_15,
sum(IIF(Platz='16',1,0)) as Gang_16,
sum(IIF(Platz='17',1,0)) as Gang_17,
sum(IIF(Platz='18',1,0)) as Gang_18,
sum(IIF(Platz='19',1,0)) as Gang_19,
sum(IIF(Platz='20',1,0)) as Gang_20
FROM
(
SELECT
time_neu as Date,
LEFT(Platz_von,2) as Platz
FROM
00_Gesamt_Pickauf
)
GROUP BY
Date