This is something I am not getting for some or other reason.
I have a list of records
BusinessUnitId Position Date Time
-----------------------------------------------------------
76 Staff 3/30/2009 11:00:00 AM
76 Staff 3/30/2009 04:00:00 PM
76 Management 3/30/2009 05:00:00 PM
78 Exco 3/30/2009 09:00:00 AM
78 Staff 3/30/2009 09:30:00 AM
If I do a group by
select
count(answer.BusinessUnitId)
from
AnswerSelected answer
group by
answer.BusinessUnitId
I retrieve a value of 3 and 2
What I am trying to achieve is get a count of 2
So in total 2 business units submitted answers, 76 and 78(hope this makes sense)
How can I do this?
Thanks
Use COUNT(DISTINCT ...)
SELECT
COUNT(DISTINCT BusinessUnitId)
FROM AnswerSelected
You don't need to use GROUP BY in this particular case to get the distinct count of business units across your entire table.
Demo here:
Rextester
Related
I am trying to take daily data (not every day has data) and sum it by weeks starting on monday. Below is a very small sample for the Data I have. the code is irrelvant for the summing purposes needed here.
Data have:
ID
PKG
Revdt
code
QTY
70
17AB
02AUG2021:00:00:00
01
7
70
17AB
04AUG2021:00:00:00
02
-10
70
17AB
05AUG2021:00:00:00
01
8
70
17AB
10AUG2021:00:00:00
01
7
70
17AB
1QAUG2021:00:00:00
01
7
73
12AC
02AUG2021:00:00:00
09
0
73
17AC
07AUG2021:00:00:00
01
7
Data want
ID
PKG
Revdt
code
QTY
70
17AB
02AUG2021:00:00:00
01
5
70
17AB
09AUG2021:00:00:00
01
14
73
12AC
02AUG2021:00:00:00
01
7
I have tried the below
Proc sql;
connect to odbc (dsn='' id='' p='');
create table work.WeeklySum as select distinct * from connection to odbc
(select ID, Pkg, datepart(week, revdt), code, sum(qty)
from datebase
group by datepart(week, revdt) );
disconnect from odbc;
quit;
However when i run it, it says "error:proc sql requires any created table to have atleast 1 column"
I would just pull the data with the daily rows first into SAS. If it is too big, write a macro to run it in loops with small chunks.
Once you get the daily dataset into SAS, create a separate table with weekly datetimes (here I am creating a table begins with Jan 1, 2021 and has a row for every week from there - change the inputs according to your need):
data weeks;
format week_date datetime19.;
do i = 0 to 52;
week_date = intnx('dtday', "01jan2021:00:00:00"dt, 7*i,'s');
output;
end;
drop i;
run;
Once you have this weeks dataset, left join your daily dataset to it using:
week_date <= revdt < intnx('dtday', week_date, 7, 's')
and then sum your variable by week_date.
select ID, Pkg, datepart(week, revdt), code, sum(qty)
from datebase
group by datepart(week, revdt)
This is not valid in SQL Server, so this will be your problem. SAS lets you do this, but when you do pass-through SQL, you have to follow their rules. In the case of SQL Server, every variable that is not part of a summary function (here, sum(qty) is the only summary function call) has to be part of the group by.
If you want to group by inside SQL Server, you'll have to modify your query to be a join from the (ID,Pkg) query to the (datepart,sum) query. SAS does this for you, but SQL Server won't.
I have a requirement to write a Informix Query that will fetch all the records of the table excluding Friday and Saturday of all the weeks and data between 4PM to 8PM of all the days of the week
Ex:
If I have the following records in my table
Name ProcessStartTime Status
Name1 2017-04-01 17:00:02.000 SUCCESS(Saturday)
Name2 2017-04-02 21:00:02.000 SUCCESS(Sunday)
Name3 2017-04-03 01:00:02.000 SUCCESS(Monday)
Name4 2017-04-04 05:00:02.000 SUCCESS(Tuesday)
Name5 2017-04-05 18:00:02.000 SUCCESS(Wednesday)
Name6 2017-04-05 15:00:02.000 SUCCESS(Wednesday)
Name7 2017-04-06 23:00:02.000 SUCCESS(Thursday)
Name8 2017-04-07 15:00:02.000 SUCCESS(Friday)
If I execute the Query I should get the Following records only,
Name2 2017-04-02 21:00:02.000 SUCCESS(Sunday)
Name3 2017-04-03 01:00:02.000 SUCCESS(Monday)
Name4 2017-04-04 05:00:02.000 SUCCESS(Tuesday)
Name6 2017-04-05 15:00:02.000 SUCCESS(Wednesday)
Name6 2017-04-06 23:00:02.000 SUCCESS(Thursday)
And the remaining data should not fetched because, those records are either Friday or Saturday as well as date time between 4PM to 8PM(Not only Friday and Saturday but for all the days of the week).
I have my SQL Server query here below which is working fine in SSMS,
SET DATEFIRST 1
SELECT Name, ProcessStartTime, Status
FROM #events
WHERE datepart(DW, ProcessStartTime) NOT IN (5,6)
AND (datepart(hh, ProcessStartTime) < 16
OR
ProcessStartTime > DATEADD(dd, DATEDIFF(dd, 0, ProcessStartTime), '08:00:00'))
GO
but I need to change this Query into Informix Query. I Googled a lot to find the way to get the one but nothing worked. Could anyone please help me on this?
Using this WHERE clause for the query should work:
WHERE WEEKDAY(ProcessStartTime) NOT IN (5,6)
AND EXTEND(ProcessStartTime, HOUR TO HOUR)::char(2)::int
NOT BETWEEN 16 AND 20
A double cast is necessary as there is no builtin cast operation from datetime to integer.
This is similar to Simon Riddle's answer, but uses an explicit time range notation:
SELECT Name, ProcessStartTime, Status
FROM Events
WHERE WEEKDAY(ProcessStartTime) NOT IN (5, 6)
AND (EXTEND(ProcessStartTime, HOUR TO SECOND) < DATETIME(16:00:00) HOUR TO SECOND
OR EXTEND(ProcessStartTime, HOUR TO SECOND) >= DATETIME(20:00:00) HOUR TO SECOND
)
I prefer this because I think it is easier to tweak it to include/exclude the exact ends of the date ranges that you want. I think the notation is probably what you want; the >= might need to be > if events at exactly 8 pm should be included rather than excluded. Note that the BETWEEN…AND operators include both the end points in the range.
I want to fetch some data from DB by giving multiple date ranges. Example,in February I want to get weekly report from a table in this order Feb 01 to 07, Feb 07 to 14, Feb 14 to 21, Feb 21 to 28 and Feb 28 to Mar 01. In DB the records are stored in a daily wise not in weekly wise. I want to cluster it as weekly wise and calculate sum then show the result. Please help me if you know this case.
For clear cut view, consider 3 tables & its columns.
Table A:id,timestamp (comment-data is inserted daily)
Table B:id,fruits
Table C:id,fruits_type
Result:
fruits_type count(id) timestamp
apple 3 01-02-2016 to 07-02-2016
orange 5 01-02-2016 to 07-02-2016
pineapple 8 01-02-2016 to 07-02-2016
apple 4 07-02-2016 to 14-02-2016
orange 5 07-02-2016 to 14-02-2016
Conditions:id should match among 3 tables;fetch data by providing group by fruits_type and timestamp should be in weekly wise.
Please help if you know this
To get the sum of all values between two dates you would do it like this:
SELECT SUM(Column1)
FROM Table1
WHERE Date1 BETWEEN '2/1/2016' AND Date1 <'2/7/2016'
If you want to make it more flexible and have the query get the last week's sum you can use the DATEADD function to lag by one week:
SELECT SUM(Column1)
FROM Table1
WHERE Date1 BETWEEN DATEADD(week, -1, GETDATE()) AND Date1 < GETDATE()
If you want the result set to include a row for each week, you can use UNION to merge the queries.
I have a View that has a structure similar to the following:
Id Name State ZipCode #Requests AmtReq Price Month Year
1 John IN 46202 203 33 $300 1 2015
1 Jane IN 46202 200 45 $100 2 2015
...
Queries require reports to be generated for given quarters (1st quarter will include the first three months ...) grouped by state
The result should look like this:
Ist Quarter ...
January February ...
State ZipCode #Requests AmtReq Price #Requests AmtReq Price ...
IN 46202 203 33 45 200 45 100
I feel that this can be done using pivoting but I do not have experience with it. I tried with single column pivoting and had some success, but not in this scale.
Another approach would be to create a stored procedure that will generate the data for me and then just fix some formating (e.g., the first two rows) in the client. Any suggestions on how to approach this problem?
I am using SQL Server as a DBMS.
If you have MS Excel on your machine then you can export the view to Excel and summarize it to a pivot table. From there you can create table and diagrams as you needed.
This one is tricky for me, can't figure it out.
We have a system where we calculate inventory numbers on the fly. If there's a month where a customer doesn't have any orders, there's no record for that month but the beginning inventory is still calculated and displayed as a rolling calculation based on the previous transaction data.
I'm now pulling this data but need to "fill in the blanks" so to speak.
For example, the table has the following fields:
MonthYear DATETIME
WarehouseID INT
Quantity DECIMAL(18,2)
If I put all this in a temporary table to do calculations, I'll end up with something like this:
2010-01-01 00:00:00.000 135 1000.00
2010-04-01 00:00:00.000 135 2000.00
2010-07-01 00:00:00.000 135 3000.00
2010-06-01 00:00:00.000 235 1000.00
2010-07-01 00:00:00.000 235 2000.00
2011-02-01 00:00:00.000 135 1000.00
2011-03-01 00:00:00.000 135 2450.00
etc., etc.
What I need to do is for each warehouse, if a record exists for that year, add a blank row for any months that aren't in the table.
In the example above for warehouse 135 I need to add a record for 02, 03, 05, 08, etc.
Is there any easier way of doing this rather than with cursors and loops?
Thanks.
Search Recursive CTE. Then LEFT JOIN. Gotta bolt out the door. Sorry for quick answer.