I need to query a table to extract maximum values of purchases in any month. However, if there are 2 months with both having the maximum number of purchases, the query should return both. MAX function only returns one of them. How do I pull both or more if available
You can GROUP BY by the month(Do not know the exact names of your columns so I wild guessed):
select max(purchases), month
from db.table
group by month
So you will have something like this(EXAMPLE DATA).
max(purchases) month
100 1
200 2
150 3
150 4
etc.
Related
I am using Oracle SQL Developer.
I got the following result from several joined input-tables:
Working station number
Produced tools
Date
1
150
01.01.2020
2
100
01.01.2020
1
50
01.02.2020
3
70
15.01.2020
1
120
08.02.2020
4
130
08.02.2020
The date in the last column is at TO_Date format YYYY/MM/DD
My goal is to visualize the amount of produced tools per working station for each month and year.
Expected output-table:
Year/Month
Working Station Number
Sum of all WS
2022/01
1
150
2022/02
2
80
2022/03
3
100
2022/04
1
120
I want this output format for all WS per Month and year. Moreover I would like to add the sum of the WS per month and year
The data does also include the amount of tools per WS for 2021. The table should therefore aso show the amount of produced tools for more years.
To achieve this format I need to 1.: sum up the tools per ws and 2. sum up the tools per month per working station number and 3. convert the lines to columns.
I would begin to Sum up the Produced tools per month and afterwards calculate it based on the ws.
Afterwards I would use the pivot-Function in order to turn the lines (working stations) into columns.
My approach would be the following:
(SELECT Working station number, Amount of produced tools, Date from SourceTable
from
(SELECT Working station number, Amount of produced tools, Date from SourceTable from SourceTable) as SourceTable
Pivot
(Max(Amount of produced tools)
For Working Station in ([1] [2] [3] [4])
) as PIVOT_Table
Unfortunately I don't know how to get the 3 steps together.
I am happy about any comments!
I am pretty new to SQL, but i need to use it for my new job as the project requires it and as I am a non-IT-guy, it is more difficult for me, because thats my first time I work professionally with SQL.
Hopefully you can help me with it: (Sry for my english, i am a non-native speaker)
I need to start a query where I get unequal IDs from 2 different reference dates.
So I have one Table with following data:
DATES ID AMOUNT SID
201910 122424 99999 1
201911 41241242 99999 2
201912 12412424 -22222 3
...
GOAL:
So the ID's from the DATE: 201911 shall be compared with those from 201910
and the query should show me the unequal ID's. So only the unmatched ID's shall be displayed.
Out of this query, the Amount should be summed up and grouped into SIDs.
If you have two dates and you want sids that are only on one of them, then:
select sid
from t
where date in (201911, 201910)
group by sid
having count(distinct date) = 1;
I have a large table containing c650,000 records. They are individuals with email addresses and one of the fields is 'dateOfApplication'. I have been asked for a breakdown of how many people signed up in each month.
I'd like the results to look something like
Month Year Total
1 2017 50763
2 2017 34725
And have made a target table in this format to put the results in. I've been able to use Month(dateOfApplication) to get the month component of the date using
SELECT DISTINCT
(SELECT COUNT(1) FROM [UG_Master]
WHERE MONTH([UG_Master].dateOfApplication) = '6') as Total
To return particular months, but don't really know how to get one row for each month it finds.
but don't really know how to get one row for each month it finds.
You can use GROUP BY :
SELECT MONTH([UG_Master].dateOfApplication), COUNT(1)
FROM [UG_Master]
GROUP BY MONTH([UG_Master].dateOfApplication);
If you want year wise months then include year also :
SELECT YEAR([UG_Master].dateOfApplication), MONTH([UG_Master].dateOfApplication), COUNT(1)
FROM [UG_Master]
GROUP BY YEAR([UG_Master].dateOfApplication), MONTH([UG_Master].dateOfApplication);
I have created 2 tables. one table has 4 fields. a unique name, a date and 3 figures. The second table contains the same fields but records the output of a merge function. therefore has a date at which time the update or insert function happened. what I want to do is retrieve a sum of either the difference between 2 days or alternatively the totals of the 2 days to work out how much the value has changed over the day. The merge function only updates if a value has changed or it needs to insert a new value.
so far I have this
select sum(Change_Table_1.Disk_Space) as total,
Change_Table_1.Date_Updated
from VM_Info
left join Change_Table_1
on VM_Info.VM_Unique = Change_Table_1.VM_Unique
where VM_Info.Agency = 'test'
group by Change_Table_1.Date_Updated
but this would just return the sum of that days updated total rather than the difference between the two days. One answer to this question would be to to add all new records to the table but this would contain a number of duplicates. So in my head what I want it to do is loop over the current figures for the day then loop over the next day but also to include all values that haven't updated. sorry if I haven't explained this well. so what I want to achieve is to get some sort of change of the total over time. If its poor design im in a position to accept that also.
Any help is much appreciated.
maybe this would explain it better. show me total for day 1, if the value hasn't changed then show me the same value for day 2 if it has changed show me new value. and so on...
ok to further elaborate.
the Change_Table looks like
vm date created action value_Field1 value_field_2 Disk_Space
abc 14/10/2013 insert 5 5 30
def 14/10/2013 insert 5 5 75
abc 15/10/2013 update 5 5 75
so the out put I want is for the 14th the total for the last column is 105. On the 15th abc has changed from 30 to 75 but def hasn't changed but still neds to be included giving 150
so the output would look like
date disk_Space
14/10/2013 105
15/10/2013 150
Does this help? If not, can you provide a few rows of sample data, and an example of the desired result?
select
(VM_Info.Disk_Space - Change_Table_1.Disk_Space) as DiskSpaceChange,
Change_Table_1.Date_Updated
from
VM_Info
left join Change_Table_1 on VM_Info.VM_Unique = Change_Table_1.VM_Unique and VM_Info.Date = Change_Table_1.Date_Updated
where
VM_Info.Agency = 'test'
I have a form where i enter min price and max price when creating product details. In the product search i have also 2 fields called min and max. So how can i get the result of given range considering the range given when inserting products
Products
min max
10 15
15 30
20 30
In the search form i insert min as 5 and max as 16. Which products i will get in the result and whats the best theory for searching considering practical situations.
Compare opposite ends of each range to find products in the overlap:
select * from products
where min < $max and max > $min
This approach works well for date ranges too.
select * from products where min>=5 and max<=16
so based on this you will get
Row -> 10, 15
The BETWEEN operator selects a range of data between two values. The values can be numbers, text, or dates.
SQL BETWEEN Operator tutorial
For example:
SELECT * FROM Product
WHERE price
BETWEEN $min AND $max