currently I'm in midst of developing crystal report template. Any help is much appreciated.
What I want to achieve is to count based on date.
I give example.
I got 3 params:
start period(date): eg: 1/5/2013
end period: eg: 5/5/2013(date)
how many working days(number): eg: 5
and the table is like this
name working date
nameA 2/5/2013
2/5/2013
3/5/2013
nameB 2/5/2013
4/5/2013
5/5/2013
I want it to count how many working days for each name.
Eg result: nameA: 2 working days, nameB: 3 working days.
Please help. I'm new to programming..:( Couldn't think any programming design for this..
You need this query,
select name, count(distinct working_date)
from table where working_date between ? and ?
but most of all, you need to learn SQL. It is impossible to write reports without some knowledge of SQL.
Related
I'm trying to obtain the average time of an "activity" in a moodle database, i am not an sql expert, but i have managed to get to the point showed in the picture, my question is if exists a way to obtain, first the timestamp/time difference (this "activity" does not have a starting time column like many others) by day and then sum them all to get the average of that activity , for the first i tried with the function 'EXTRACT()' and comparing the dates in the format "%Y-%m-%d" but only sums the first row where they are equal, by the way i have been doing this just by a sql statement, i know the existence of store procedures but my level of sql is not that high.
Thanks in advance!
data obtained so far
Data on table logs (the most important i think)
component
action
objecttable
userid
courseid
timecreated
mod_quiz*
viewed
quiz_attempts
6
2
1645287525
mod_forum
viewed
forum
5
2
1645288525
core
loggedout
user
2
0
1645291745
mod_page
viewed
page
5
2
1645291955
Data i've trying to get:
Activity
StartTime
EndTime
Total
forum
19:01
19:10
9 minute(s)
quiz
15:45
16:00
15 minute(s)
page
...
...
...
workshop
...
...
...
but so far i get to assort the data in a column
Time
2022-x-x h:m
....
but when i try to sum by day with the function EXTRACT() and trying to match the dates in a very long query it just get the first record.
NOTE: * half of the "activities" were easy to calculate since they have a "timestart" and "timeend" columns but i can not figure out how to solve the ones that do not have a "timestart" column.
I'm wanting to use GDELT to obtain a CSV containing a count of the number of articles containing a specific theme for all countries for a given number of years. Please note it's not quite the same as the events database, I'm specifically interested in the themes). There are some tutorials on the GDELT website (here), but they appear to be a little out of date regarding the regexp syntax and I'm not super familiar with SQL. Ideally, my output should look something like the following :
Year Country Count
2001 Afghanistan 34234
2002 Afghanistan 11864
...
2001 Zambia 939
2002 Zambia 864
From my understanding and the tutorial, this code counts the number of articles by theme and day of the year rather than year (taken from here). It's close to what I want, but not quite there.
select date(_partitiontime) date, count(theme) occurences
from `gdelt-bq.gdeltv2.gkg_partitioned`, unnest(split(themes,';')) as theme
where _partitiontime >= "2020-11-01 00:00:00" and _partitiontime < "2020-11-07 00:00:00"
and lower(theme) like "%bitcoin%"
group by date
-- order by date
I think I need to
Add something to return and group by V2Locations at the country level
Parse the data to get only the year
but am not sure how to do it. Any help would be greatly appreciated.
Thanks!
I am relatively new to SQL...
I am creating a summary of returned items and I would like the finished result to show the item code, the amount returned (SUM) and the reason for return. So Ideally it would be something like this:
101 - Blue Widget | 13 | Shipment Lost
101 - Blue Widget | 3 | Damaged in Transit
102 - Red Widget | 5 | Shipment Lost
So it is grouping by ITEM and RMACODE and summing the quantities
Here is a simplified version of the query I wrote for this
Select ITEM, SUM(QUANTITY), RMACODE, DATEENTERED
FROM RMAITEMS
group by ITEM, Quantity, RMACODE
I am loading this in SSRS and need DATENETERED for my report parameters to only pull records between #StartDate and #EndDate. I get en error saying DATEENTERED is invalid because it is not in the GROUP BY.
Is there a better/different way to acheive the result I am looking for?
Thanks
Andrew
I made the changes suggested by edkloczko and it appeared everything would work then, but since we removed the date from the select statement I am unable to use it in my report parameters. Here is a screenshot. I have a few ideas I will try out today but if anyone has already climbed this hill and can help me with directions I would be grateful.
Expression Needed is Absent
If you're looking to filter by date and don't actually need the date field...
SELECT ITEM, SUM(QUANTITY), RMACODE
FROM RMAITEMS
WHERE DATEENTERED>=STARTDATE AND DATEENTERED<=ENDDATE
GROUP BY ITEM, QUANTITY, RMACODE
This will give you all the records you need and makes the extra filtering step you're doing unnecessary - it will only select the records between the start and end dates.
I've run into the same issue before with our IBM DB2. As far as I know you need to specify ALL of the SELECT items in the GROUP BY statement. Unsure if this is specific to certain databases or not.
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;
Wondering if anyone can help with the code for this.
I want to query the data and get 2 entries, one for YTD previous year and one for this year YTD.
Only way I know how to do this is as 2 separate queries with where clauses.. I would prefer to not have to run the query twice.
One column called DatePeriod and populated with 2011 YTD and 2012YTD, would be even better if I could get it to do 2011YTD, 2012YTD, 2011Total, 2012Total... though guessing this is 4 queries.
Thanks
EDIT:
In response to help clear a few things up:
This is being coded in MS SQL.
The data looks like so: (very basic example)
Date | Call_Volume
1/1/2012 | 4
What I would like is to have the Call_Volume summed up, I have queries that group it by week, and others that do it by month. I could pull all the dailies in and do this in Excel but the table has millions of rows so always best to reduce the size of my output.
I currently group by Week/Month and Year and union all so its 1 output. But that means I have 3 queries accessing the same table, large pain, very slow not efficient and that is fine but now I also need a YTD so its either 1 more query or if I could find a way to add it to the yearly query that would ideal:
So
DatePeriod | Sum_Calls
2011 Total | 40
2011 YTD | 12
2012 Total | 45
2012 YTD | 15
Hope this makes any sense.
SQL is built to do operations on rows, not columns (you select columns, of course, but aggregate operations are all on rows).
The most standard approach to this is something like:
SELECT SUM(your_table.sales), YEAR(your_table.sale_date)
FROM your_table
GROUP BY YEAR(your_table.sale_date)
Now you'll get one row for each year on record, with no limit to how many years you can process. If you're already grouping by another field, that's fine; you'll then get one row for each year in each of those groups.
Your program can then iterate over the rows and organize/render them however you like.
If you absolutely, positively must have columns instead, you'll be stuck with something like this:
SELECT SUM(IF(YEAR(date) = 2011, sales, 0)) AS total_2011,
SUM(IF(YEAR(date) = 2012, total_2012, 0)) AS total_2012
FROM your_table
If you're building the query programmatically you can add as many of those column criteria as you need, but I wouldn't count on this running very efficiently.
(These examples are written with some MySQL-specific functions. Corresponding functions exist for other engines but the syntax would be a little different.)