Aggregating Data in SQL - sql

When looking at the picture in the link I need the Imploded Units to Average and the Exploded Units to Sum, I will attach my code below the Results shown below. I have been searching for an answer to this for a few days. I fear I may be trying to code over my head a little bit.
SELECT A.to_load_id AS "Cases",
A.from_qty - A.to_qty AS "Imploded Units",
( A.from_qty - A.to_qty ) * Nvl(SUM(p.qty), 1) AS "Exploded Units",
A.wskusku AS "Sku's",
A.wave AS "Wave",
A.from_loc AS "Processed Location",
A.free_form_text AS "Zone" ,
FROM audits A,
prepack P
WHERE A.from_loc LIKE 'S%'
AND A.to_loc = A.from_loc
AND free_form_text IN ( '01', '02', '03', '04',
'05', '06', '07', '08',
'09', '10', '11', '12',
'13', '14' )
AND A.wskusku = p.sku (+)
AND A.from_load_id = '42419472'
AND A.wave in ('WC055193','','','','','','','','','')
AND To_date(Substr(a.date_wms, 1, 12), 'YYYY/MM/DD HH24:MI') >=
SYSDATE - 4
GROUP BY A.to_load_id,
A.from_qty,
A.to_qty,
P.qty,
A.wskusku,
A.wave,
A.from_loc,
A.free_form_text

You need to use avg() / sum() and not GROUP BY columns that are involved in aggregates:
SELECT A.to_load_id AS "Cases"
,avg(A.from_qty - A.to_qty) AS "Imploded Units"
,sum(A.from_qty - A.to_qty) * Nvl(SUM(P.qty), 1) AS "Exploded Units"
,A.wskusku AS "Sku's"
,A.wave AS "Wave"
,A.from_loc AS "Processed Location"
,A.free_form_text AS "Zone"
FROM audits A
JOIN prepack P ON P.sku (+) = A.wskusku
WHERE A.from_loc LIKE 'S%'
AND A.to_loc = A.from_loc
AND A.free_form_text IN ( '01', '02', '03', '04',
'05', '06', '07', '08',
'09', '10', '11', '12',
'13', '14' )
AND A.from_load_id = '42419472'
AND A.wave in ('WC055193','','','','','','','','','')
AND To_date(Substr(A.date_wms, 1, 12), 'YYYY/MM/DD HH24:MI') >= SYSDATE - 4
GROUP BY A.to_load_id,
,A.wskusku
,A.wave
,A.from_loc
,A.free_form_text
Not sure why you multiple the "Exploded Units", but not the "Imploded Units". I copied what you have there.

Related

SSRS - Cannot read the next row for dataset DataSet1

with TotCFS as (select count(*)*1.0 as TotalCFS,
'Total CFS' as RowTitle
from PrivilegeData.TABLENAMEC c
where cast(CallCreatedDateTime as date) between #StartDate and #EndDate and CallPriority in ('1', '2', '3', '4', '5') and AreaCommand in ('FH', 'VA', 'NE', 'NW', 'SE', 'SW') and IsBolo = 0
)
select AreaCommand, CallPriority,
avg(datediff(second, CallCreatedDateTime, CallEntryDateTime)) as AverageSeconds,
left(dbo.[ConvertTimeToHHMMSS](avg(datediff(second, CallCreatedDateTime, CallEntryDateTime)), 's'), 7) as DisplayAvg,
'Create to Entry' as RowTitle, 1 as RowSort, b.SortOrder as ColumnSort
from PrivilegeData.TABLENAMEC c
inner join (select distinct AreaCommandAbbreviation, SortOrder from dimBeat) b on c.AreaCommand = b.AreaCommandAbbreviation
where cast(CallCreatedDateTime as date) between #StartDate and #EndDate and CallPriority in ('1', '2', '3', '4', '5') and AreaCommand in ('FH', 'VA', 'NE', 'NW', 'SE', 'SW') and IsBolo = 0
group by AreaCommand, CallPriority, SortOrder
UNION
select AreaCommand, CallPriority,
avg(datediff(second, CallEntryDateTime, CallDispatchDateTime)) as AvgEntryToDispatchSeconds,
left(dbo.ConvertTimeToHHMMSS(avg(datediff(second, CallEntryDateTime, CallDispatchDateTime)), 's'), 7) as DisplayAvgEntryToDispatchSeconds,
'Entry to Dispatch' as RowTitle, 2 , b.SortOrder
from PrivilegeData.TABLENAMEC c
inner join (select distinct AreaCommandAbbreviation, SortOrder from dimBeat) b on c.AreaCommand = b.AreaCommandAbbreviation
where cast(CallCreatedDateTime as date) between #StartDate and #EndDate and CallPriority in ('1', '2', '3', '4', '5') and AreaCommand in ('FH', 'VA', 'NE', 'NW', 'SE', 'SW') and IsBolo = 0
group by AreaCommand, CallPriority, SortOrder
I have about 8 unions I'm doing for this code. the difference is the name of the Row titles. this report has been running for about a year without any problems. I use this code in SSRS query type text. I also have one of my rowset name 'AverageSeconds' configured to read this expression
=IIf((Fields!RowSort.Value) < 7,Format(DateAdd("s", Avg(Fields!AverageSeconds.Value), "00:00:00"), "H:mm:ss"), Sum(Fields!AverageSeconds.Value))
the report some how broke and I have tried everything I find searching to fix it. Please help me with this error 'rsErrorReadingNextDataRow'.
This has got to be an issue with the data being operated upon. Maybe a 0 or NULL value condition.. I would start with reviewing records that were added or changed around the time that the problem began.
I Dropped and recreated the fact table and run my ssis package, which seams to fix it. The reason I did that is because I couldn't find a NULL or 0 value.

LEAST(STRING) and GREATEST(STRING) for long STRINGS in Legacy BigQuery SQL

I'd like to run the following SQL query in a BigQuery table:
SELECT
LEAST(origin, destination) AS point_1,
GREATEST(origin, destination) AS point_2,
COUNT(*) AS journey_count,
FROM route
GROUP BY point_1, point_2
ORDER BY point_1, point_2;
on a table like:
INSERT INTO route
( route_id, origin, destination, dur)
VALUES
( 1, 'AA', 'BB', 2),
( 2, 'CC', 'DD', 4),
( 3, 'BB', 'AA', 6),
( 4, 'CC', 'AA', 2),
( 5, 'DD', 'CC', 12);
But BigQuery tells me that, although the query is syntactically correct, string is not a valid argument type for the LEAST function, for string length > 1. I tried to cast it to numeric, like LEAST(cast(origin as numeric), cast(destination as numeric)) AS point_1 but it tells me LEAST cannot handle bytes.
How do I make LEAST and GREATEST compare long strings in BigQuery?
#legacydSQL
SELECT
IF(origin < destination, CONCAT(origin, ' - ', destination), CONCAT(destination, ' - ', origin)) route,
COUNT(1) journey_count
FROM [project:dataset.table]
GROUP BY route
ORDER BY route
if to apply to sample data from your example - result is
Row route journey_count
1 AA - BB 2
2 AA - CC 1
3 CC - DD 2
see this
with t as (
(select 1 as route_id, 'AA' as origin, 'BB' as destination, 2 as dur)
union all
(select 2, 'CC', 'DD', 4)
union all
(select 3, 'BB', 'AA', 6)
union all
(select 4, 'CC', 'AA', 2)
union all
(select 5, 'DD', 'CC', 12))
select
if(origin<destination,origin,destination) as point_1,
if(origin<destination,destination,origin) as point_2,
count(1) as journey_count
from t
GROUP BY point_1, point_2
ORDER BY point_1, point_2;

Utilizing Case When & Possibly "Lead" or "Lag"

I know what I want the data to display but can't seem to figure out the correct logic for it. Example below.
Dataset
**'ID', 'Admission Mth', 'Admission Yr', 'Category', 'Facility', 'ID_Yr_Cat', 'ID_Yr_Cat_Fac',**
'123456', 'Jan', '2017', 'Hospital', 'NYMC', '123456-2017-Hospital', '123456-2017-Hospital-NYMC',
'123456', 'Jul', '2017', 'Hospital', 'NYMC', '123456-2017-Hospital', '123456-2017-Hospital-NYMC',
'123456', 'Oct', '2018', 'Hospital', 'NYMC', '123456-2018-Hospital', '123456-2018-Hospital-NYMC',
'123456', 'Nov', '2018', 'Hospital', 'NJMC', '123456-2018-Hospital', '123456-2018-Hospital-NJMC',
'789123', 'Feb', '2017', 'Clinic', 'Philly Clinic', '789123-2017-Clinic', '789123-2017-Clinic-Philly Clinic',
'987654', 'May', '2018', 'Hospital', 'PAMC', '987654-2018-Hospital', '987654-2018-Hospital-PAMC',
'456123', 'Sept', '2017', 'Clinic', 'Philly Clinic', '456123-2017-Clinic', '456123-2017-Clinic-Philly Clinic',
'456123', 'Aug', '2018', 'Hospital', 'NYMC', '456123-2018-Hospital', '456123-2018-Hospital-NYMC',
'456123', 'Nov', '2018', 'Hospital', 'NYMC', '456123-2018-Hospital', '456123-2018-Hospital-NYMC',
'456123', 'Dec', '2018', 'Hospital', 'NJMC', '456123-2018-Hospital', '456123-2018-Hospital-NJMC'
I want the final results to display "1" flags for the hospital readmit.
Final results should show:
**'Hospital Readmit per Yr'**,
'0',
'1',
'0',
'1',
'0',
'0',
'0',
'0',
'1',
'1'
**'Hospital Readmit per Yr & Fac'**,
'0',
'1',
'0',
'0',
'0',
'0',
'0',
'0',
'1',
'0'
My thoughts were to use some sort of case when with a lead function including a partition. Just not sure how to write it out. I'm using SQL Server MS 2008.
This can be achieved either with CASE WHEN or with LEAD or LAG function. However, since you mentioned you are using SQL server 2008, LEAD or LAG may not work as LEAD and LAG are the analytical functions which can be used in SQL Server 2012 or higher version. You may want to try something like this if you want to use CASE WHEN.
For getting [Hospital Readmit per Yr] flag value:
SELECT
CASE WHEN A.[RowNumber]>1 THEN 1 else 0 END As [Hospital Readmit per Yr]
FROM (select ROW_NUMBER() OVER (PARTITION BY [ID_Yr_Cat] order by [Admission Mth], [Admission Yr]) as [RowNumber], * from hospital) as A
For getting [ID_Yr_Cat_Fac] flag value:
SELECT
CASE WHEN A.[RowNumber]>1 THEN 1 else 0 END As [Hospital Readmit per Yr & Fac]
FROM (select ROW_NUMBER() OVER (PARTITION BY [ID_Yr_Cat_Fac] order by [Admission Mth], [Admission Yr]) as [RowNumber], * from hospital) as A
In case you want to look at ALL the columns to understand how the query is returning results, check the screenshots below:

Two counts in three tables

I am trying to count two columns using the following query:
select distinct [District],
count (Distinct [Student Identifier Statewide California])
as '11-12 Enrollment',
(select count (Distinct IncdtKey)
From [dbo].[DisciplineStudentFile1112]
where GrdLvLKey in ('15', '01', '02', '03', '04',
'05', '06', '07', '08', '09',
'10', '11', '12', '18', '19')) as Total_Incidents
From
dbo.SSID1112StudentEnrollmentRecords with (nolock)
inner join
[dbo].[SchoolDetail] on CDSCode = dbo.SSID1112StudentEnrollmentRecords.CDSOrig
where
[EnrollStatCodeOrig] like '10'
and
[Grade Level Code] in ('PS', 'KN', '01', '02', '03',
'04', '05', '06', '07', '08',
'09', '10', '11', '12', 'UE', 'US')
group by [District]
order by [District]
My results are:
District 11-12 Enrollment Total_Incidents
AB Unified 20662 896371
CE Unified 5387 896371
DR Unified 526 896371
FJ Unified 1506 896371
KT Unified 8415 896371
I can't figure out how to get the individual counts in the Total_Incidents column instead of a total 896371 count?
An easy approach is to correlate the subquery with the outer query:
select distinct SD.District,
count ( distinct SER.[Student Identifier Statewide California] ) as [11-12 Enrollment],
( select count( distinct DSF.IncdtKey ) from dbo.DisciplineStudentFile1112 as DSF
where DSF.GrdLvLKey in ( '15', '01', '02', '03', '04', '05', '06', '07', '08', '09',
'10', '11', '12', '18', '19' ) and -- Note additional condition here.
DSF.District = SD.District ) as Total_Incidents
from dbo.SSID1112StudentEnrollmentRecords as SER with (nolock) inner join
dbo.SchoolDetail as SD on SD.CDSCode = SER.CDSOrig
where SER.EnrollStatCodeOrig like '10' and
[Grade Level Code] in ( 'PS', 'KN', '01', '02', '03', '04', '05', '06', '07', '08',
'09', '10', '11', '12', 'UE', 'US' )
group by SD.District
order by SD.District
I have made some assumptions about the table schemas. I would recommend that when using joins that you supply an alias for each table and use the alias on every reference to avoid confusion.
An alternative solution would be to use another join with DisciplineStudentFile1112 and then summarize the results using GROUP BY.

Error in my sql query

Hey guys i am trying to run this query in my postgres db but it returns an error: [Err] ERROR: syntax error at or near ","
LINE 13: and not substr(a.zoneiddest , 1 ,3) = any ('254','255','256'...
The query is like this
SELECT
to_char(a.CALLDATE, 'yyyymm') AS month,
min(a.calldate) AS start_time,
max(a.calldate) AS end_time,
ceil(SUM(a.CALLDURATION::INT) / 60) AS minutes,
COUNT(DISTINCT a.IDENTIFIANT) AS distinct_callers,
a.zoneiddest AS country_code,
b.country
FROM cdr_data a,
country_codes b
WHERE a.CALLSUBCLASS = '002'
AND a.CALLCLASS = '008'
AND a.zoneiddest::INT > 0
AND SUBSTR(a.CALLEDNUMBER, 1, 2) NOT IN
( '77', '78', '75', '70', '71', '41', '31', '39', '76', '79' )
AND NOT substr(a.zoneiddest, 1, 3) = ANY
( '254', '255','256', '211', '257', '250', '256' )
AND trim(a.zoneiddest) = trim(b.country_code)
GROUP BY
to_char(a.CALLDATE, 'yyyymm'),
a.zoneiddest,
b.country
ORDER BY 1
This same query works well in oracle with just a small minor change on a.zoneiddest::integer > 0 to just a.zoneiddest > 0
What could i be doing wrong
The problem is with your ANY operator. If i understand your query correctly, you can just substitute it with a NOT IN statement.
SELECT to_char (a.CALLDATE,'yyyymm') as month,min(a.calldate) as
start_time,max(a.calldate) as end_time,
ceil(SUM (a.CALLDURATION::integer) / 60) AS minutes,
COUNT (DISTINCT a.IDENTIFIANT) AS distinct_callers,
a.zoneiddest as country_code,b.country
FROM cdr_data a,COUNTRY_CODES b
WHERE a.CALLSUBCLASS = '002'
AND a.CALLCLASS = '008'
and a.zoneiddest::integer > 0
AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN
('77', '78', '75', '70', '71', '41', '31', '39', '76','79')
// This line
AND substr(a.zoneiddest , 1 ,3) NOT IN
('254','255','256','211','257','250','256')
// End of line
and trim(a.zoneiddest) = trim(b.country_code)
GROUP BY to_char (a.CALLDATE,'yyyymm') ,a.zoneiddest,b.country
ORDER BY 1
Try using keyword any combine with parameter array like this:
= any (ARRAY['254','255','256','211','257','250','256'])