Skip to content

Tag: sql

MYSQL inner join counting only once depending on two conditions

I have a SQL query where I’m joining three tables that have information about employees, their departments, their schedules, if they are in vacations or not and reasons why they skipped work on an specific date. In one of the tables I have as foreign keys the employeeId, the scheduleId and the date in w…

SQL Grand total without subtotals

I’m making a large SQL report in Orderwise, very roughly simplified as follows; I want a grand total at the bottom, without a bunch of subtotals dotted in throughout the report – therefore I don’t think I can use ROLLUP. The Subquery in there is of course a sub query and in the real thing th…

Percentage distribution of column occurence?

I am looking for a way to calculate the distribution of column values in my table. e.g. if I have two rows containing “red” and “blue”, each should have 50%. I want to count the occurence of col and compare that to the overall amount of rows. My attempt: Any help? Thank you! Answer Jus…

Find the first AND last date that an ID occured in dataset SQL

I have a very large data set with over 1 million rows and over 70 columns. The dataset contains data about back orders of an organization. Every day a snapshot is being made and all orders that are backorders are added to the dataset. I want to know what the first date is when an OrderID occurs in the dataset…

How to use IF statement in a calculation in a SQL query

I use following query to give me the earned commission of our sales agents. Now I want the output to be sorted by (sales * sph * factor). Where factor is 10 if sph > 1.5 and else 7.5. Is it possible to implement this in the query. If yes how do I achieve this? It would also be good