Column 'city.POPULATION' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause - sql

I was creating a query, and i saw this error-message.
This is the query i'm trying to execute:
select D.ID_city
, D.ID_year
, D.NB_deaths
, D.NB_births_M
, D.NB_births_F
, D.NB_population_AS
, D.NB_population_ACL
, COUNT( NB_births_M+ NB_births_F)/C.population as Birthrate
, COUNT(NB_deaths_M + NB_deaths_F) as Mortality
from Demography D
join region C
on (D.Id_city =C.Id_city)
group
by D.ID_city
, D.ID_year
, D.NB_deaths_M
, D.NB_births_M
, D.NB_births_F
, D.NB_population_AS
, D.NB_population_ACL

You should include c.population in the group by, since you use that column outside the count aggregate function:
COUNT( NB_births_M+ NB_births_F)/C.population
Use this (added D.NB_deaths_F too since it is necessary too):
select D.ID_city
, D.ID_year
, D.NB_deaths
, D.NB_births_M
, D.NB_births_F
, D.NB_population_AS
, D.NB_population_ACL
, COUNT( NB_births_M+ NB_births_F)/C.population as Birthrate
, COUNT(NB_deaths_M + NB_deaths_F) as Mortality
from Demography D
join region C
on (D.Id_city =C.Id_city)
group
by D.ID_city
, D.ID_year
, D.NB_deaths_M
, D.NB_deaths_F -- <-- added
, D.NB_births_M
, D.NB_births_F
, D.NB_population_AS
, D.NB_population_ACL
, C.population -- <-- added

Related

How can i fix this problem with OR/AND clause in SQL

I have a set of patents that I have titles and abstracts on and would like to search in such a way that their final search algorithm requires the keyword “software” to be present, and none of the keywords “chip”, “semiconductor”, “bus”, “circuit” or “circuit” to be present.
i did This:
SELECT distinct
tls201_appln.docdb_family_id
, tls201_appln.appln_id
, [appln_auth]
, [appln_nr]
, [appln_kind]
, [appln_filing_date]
, [receiving_office]
, [earliest_publn_date]
, [granted]
, [nb_citing_docdb_fam]
, [nb_applicants]
, [nb_inventors]
, tls202_appln_title.appln_title
FROM tls201_appln
INNER JOIN tls202_appln_title ON tls201_appln.appln_id = tls202_appln_title.appln_id
INNER JOIN tls203_appln_abstr ON tls201_appln.appln_id = tls203_appln_abstr.appln_id
WHERE (appln_title like '%software%'
or appln_abstract like '%software%')
AND appln_title not like '%chip%'
or '%semiconductor%'
or '%circuity%'
or '%circuitry%'
or '%bus'%'
or appln_abstract not like '%chip%'
or '%semiconductor%'
or '%circuity%'
or '%circuitry%'
or '%bus'%'
AND appln_filing_year between 2003 and 2008
but im getting this error An expression of non-boolean type specified in a context where a condition is expected, near 'or'. What should i do?
This is wrong:
and appln_title not like '%chip%' or '%semiconductor%' or '%circuity%'
This is right:
and appln_title not like '%chip%'
and appln_title not like '%semiconductor%'
and appln_title not like '%circuity%'
Sample data would be helpful so I could test my code.
To help whoever maintains this code in the future, you'd do well to qualify your column names. It's difficult to know what's going on when I can't tell which table each column comes from.
This is considerably more verbose, but possibly easier to understand and maintain. Will this work?
with main as (
SELECT distinct
tls201_appln.docdb_family_id
, tls201_appln.appln_id
, [appln_auth]
, [appln_nr]
, [appln_kind]
, [appln_filing_date]
, [receiving_office]
, [earliest_publn_date]
, [granted]
, [nb_citing_docdb_fam]
, [nb_applicants]
, [nb_inventors]
, tls202_appln_title.appln_title
, appln_abstract
FROM tls201_appln
INNER JOIN tls202_appln_title ON tls201_appln.appln_id = tls202_appln_title.appln_id
INNER JOIN tls203_appln_abstr ON tls201_appln.appln_id = tls203_appln_abstr.appln_id
WHERE appln_title + ' ' + appln_abstract like '%software%'
AND appln_filing_year between 2003 and 2008
)
select docdb_family_id
, appln_id
, [appln_auth]
, [appln_nr]
, [appln_kind]
, [appln_filing_date]
, [receiving_office]
, [earliest_publn_date]
, [granted]
, [nb_citing_docdb_fam]
, [nb_applicants]
, [nb_inventors]
, appln_title
from main
except
select docdb_family_id
, appln_id
, [appln_auth]
, [appln_nr]
, [appln_kind]
, [appln_filing_date]
, [receiving_office]
, [earliest_publn_date]
, [granted]
, [nb_citing_docdb_fam]
, [nb_applicants]
, [nb_inventors]
, appln_title
from main
where exists (
select 1
from (
select '%chip%' as cond
union select '%semiconductor%'
union select '%circuity%'
union select '%circuitry%'
union select '%bus%'
) c
where appln_title like c.cond
or appln_abstract like c.cond
)
based on https://stackoverflow.com/a/1127100/9937026

An efficient way to group columns in MonetDB

I have the next INSERT with SELECT in MonetDB:`
insert into colombia.agregada_region_mes
(
cod_anomes
, cod_produto
, sg_estado
, cod_subcanal
, qtd_vendidas
, valor
, valor_dolar
, valor_euro
, fact_count
)
select
f.cod_anomes
, f.cod_produto
, f.sg_estado
, f.cod_subcanal
, sum(f.qtd_vendidas) as qtd_vendidas
, sum(f.valor) as valor
, sum(f.valor_dolar) as valor_dolar
, sum(f.valor_euro) as valor_euro
, count(*) as fact_count
from colombia.staging_rm_fact f
group by
f.cod_anomes
, f.cod_produto
, f.sg_estado
, f.cod_subcanal;
(Please, note the GROUP BY part).
Table "staging_rm_fact" has 50 million of rows, and MonetDB exceeds 16Gb of memory trying to resolve the INSERT:
Is there any other efficient way to resolve this group by?

Convert Oracle SQL statement into SQL Server statement

A bookings project now requires the same data extract - but from an SQL Server database - instead of Oracle. Can anyone assist converting the following into SQL Server syntax?
SELECT *
FROM (
SELECT o.ot_outlet_code
,v.lab_site_code ot_outlet_code
,v.brand
,v.region
, bd.cd_day_date booking_date, dd.cd_day_date dining_date
, f.last_change_date, f.created_date
, f.modified_date, t15.ts_timeslot_desc
, t.TIME, s.session_type
, tbs.booking_status, f.ADDED_BY_USER
, bp.product, bs.booking_source
, f.SPECIAL_OFFER, f.SEATING_PREFERENCE
, f.Tables_guest_id, covers
, booking_occurrence, breakfast_flag
, row_number() OVER (PARTITION BY f.Tables_guest_id ORDER BY f.last_change_date DESC, f.last_change_time DESC) rank_latest_record
, f.title, f.emailoptout
, f.MOBILE_OPT_IN, f.HIGH_CHAIR_COVERS
, f.GUEST_TYPE, f.Booking_ID
FROM owbi.whs_fact_rest_booking f
, owbi.whs_dim_cal_date bd
, owbi.whs_dim_cal_date dd
, owbi.whs_dim_bat_booking_source bs
, owbi.whs_dim_time_of_day t
, owbi.whs_dim_bat_product bp
, owbi.whs_dim_15_timeslot t15
, owbi.whs_dim_bat_booking_status tbs
, owbi.whs_dim_bat_session s
, owbi.bat_restaurants_v v
WHERE f.whs_dim_outlet = v.outlet
AND f.whs_dim_booking_date = bd.dimension_Key
AND f.whs_dim_dining_date = dd.dimension_key
AND f.whs_dim_bat_session = s.dimension_key
AND f.whs_dim_bat_booking_status = tbs.dimension_key
AND f.whs_dim_bat_product = bp.dimension_Key
AND f.whs_dim_bat_booking_source = bs.dimension_key
AND f.whs_dim_booking_time = t.dimension_Key
AND f.whs_dim_dining_15_timeslot = t15.dimension_key
AND dd.ey_year_code in ('2018')
AND f.whs_dim_dining_date >= 20170303
)
WHERE rank_latest_record = 1
ORDER BY BOOKING_DATE DESC;
The derived table must have an alias. eg
SELECT *
FROM (
SELECT o.ot_outlet_code
,v.lab_site_code ot_outlet_code
,v.brand
,v.region
, bd.cd_day_date booking_date, dd.cd_day_date dining_date
, f.last_change_date, f.created_date
, f.modified_date, t15.ts_timeslot_desc
, t.TIME, s.session_type
, tbs.booking_status, f.ADDED_BY_USER
, bp.product, bs.booking_source
, f.SPECIAL_OFFER, f.SEATING_PREFERENCE
, f.Tables_guest_id, covers
, booking_occurrence, breakfast_flag
, row_number() OVER (PARTITION BY f.Tables_guest_id ORDER BY f.last_change_date DESC, f.last_change_time DESC) rank_latest_record
, f.title, f.emailoptout
, f.MOBILE_OPT_IN, f.HIGH_CHAIR_COVERS
, f.GUEST_TYPE, f.Booking_ID
FROM owbi.whs_fact_rest_booking f
, owbi.whs_dim_cal_date bd
, owbi.whs_dim_cal_date dd
, owbi.whs_dim_bat_booking_source bs
, owbi.whs_dim_time_of_day t
, owbi.whs_dim_bat_product bp
, owbi.whs_dim_15_timeslot t15
, owbi.whs_dim_bat_booking_status tbs
, owbi.whs_dim_bat_session s
, owbi.bat_restaurants_v v
WHERE f.whs_dim_outlet = v.outlet
AND f.whs_dim_booking_date = bd.dimension_Key
AND f.whs_dim_dining_date = dd.dimension_key
AND f.whs_dim_bat_session = s.dimension_key
AND f.whs_dim_bat_booking_status = tbs.dimension_key
AND f.whs_dim_bat_product = bp.dimension_Key
AND f.whs_dim_bat_booking_source = bs.dimension_key
AND f.whs_dim_booking_time = t.dimension_Key
AND f.whs_dim_dining_15_timeslot = t15.dimension_key
AND dd.ey_year_code in ('2018')
AND f.whs_dim_dining_date >= 20170303
) dt
WHERE rank_latest_record = 1
ORDER BY BOOKING_DATE DESC;
In SQL Server it's considered poor form not use ANSI-style JOINs, although it's perfectly legal for inner joins to write them as a cross-join with the join criteria in the WHERE clause.
And it's generally better to use CTEs instead of subqueries/inline views/derived tables in the FROM clause.

ORA-30926: unable to get a stable set of rows in the source tables

I have a customer who gets: ORA-30926: unable to get a stable set of rows in the source tables:
Log show error Massage Error (30926)
13:52:19 (00:00:02.406) ERROR : Error (30926) (00:00:02.406) ORA-30926: Stabile Zeilengruppe in den Quelltabellen kann nicht eingelesen werden
TS03_MIN0100: UpdTable failed. Update inv_value in cMinTimeTable:
MERGE INTO HUBWBPMS5_ENTTS03005400223 a USING ( SELECT DISTINCT a.inv_value +
( a.inv_value_sum - h.inv_value ) AS inv_value , a.rowid xzfd_rid
FROM HUBWBPMS5_ENTTS03005700223 h , HUBWBPMS5_ENTTS03005400223 a
WHERE a.voucher_no = h.voucher_no AND a.sequence_no = h.max_seq_no
AND a.client = h.client ) xzfd_t ON ( xzfd_t.xzfd_rid = a.rowid )
WHEN MATCHED THEN
UPDATE
SET a.inv_value = xzfd_t.inv_value
I have checked for duplicate values in the tables but cant find anything unusual.
Maybe someone has an idea that could be useful.
The query is:
Query causing error (temp table):
INSERT INTO HUBWBPMS5_ENTTS03005700228 ( agg_flag , ace_code , activity , category , client , cost_dep , description , dim1 , dim2 , dim3 , dim4 , inc_ref , inv_value , max_seq_no , pd , period , project , resource_id , resource_typ , trans_date , unit , voucher_no , work_order , work_type )
SELECT agg_flag , ace_code , activity , category , client , cost_dep , description , dim1 , dim2 , dim3 , dim4 , inc_ref , SUM ( inv_value ) inv_value , max_seq_no , pd , period , project , resource_id , resource_typ , trans_date , unit , voucher_no , work_order , work_type
FROM HUBWBPMS5_ENTTS03005400228
WHERE agg_flag = 1
GROUP BY agg_flag , ace_code , activity , category , client , cost_dep , description , dim1 , dim2 , dim3 , dim4 , period , trans_date , voucher_no , max_seq_no , inc_ref , pd , project , resource_id , resource_typ , unit , work_order , work_type
When you get that error, it will be from a MERGE statement, and it indicates that there are multiple rows in the source dataset that match to a row you're joining to in the target table, and as such, Oracle doesn't know which one to use to do the update.
Taking your merge statement:
MERGE INTO HUBWBPMS5_ENTTS03005400223 a
USING (SELECT DISTINCT a.inv_value + ( a.inv_value_sum - h.inv_value ) AS inv_value,
a.rowid xzfd_rid
FROM HUBWBPMS5_ENTTS03005700223 h,
HUBWBPMS5_ENTTS03005400223 a
WHERE a.voucher_no = h.voucher_no
AND a.sequence_no = h.max_seq_no
AND a.client = h.client) xzfd_t
ON (xzfd_t.xzfd_rid = a.rowid)
WHEN MATCHED THEN
UPDATE SET a.inv_value = xzfd_t.inv_value;
it looks like the join between the two tables HUBWBPMS5_ENTTS03005700223 and HUBWBPMS5_ENTTS03005400223 in the xzfd_t subquery causes multiple rows to be returned for one or more of the HUBWBPMS5_ENTTS03005400223 rows (ie. you get multiple rows returned for at least one a.rowid).
To check this, run:
SELECT xzfd_rid,
COUNT(*) cnt
FROM (SELECT DISTINCT a.inv_value + ( a.inv_value_sum - h.inv_value ) AS inv_value,
a.rowid xzfd_rid
FROM HUBWBPMS5_ENTTS03005700223 h,
HUBWBPMS5_ENTTS03005400223 a
WHERE a.voucher_no = h.voucher_no
AND a.sequence_no = h.max_seq_no
AND a.client = h.client)
GROUP BY xzfd_rid
HAVING COUNT(*) > 1;
In order to fix this, you'd need to make the xzfd_t subquery return a single row for each xzfd_rid. Possibly using row_number() to pick a single row, or an aggregate query to sum up all the h.inv_value fields per a.rowid instead of the DISTINCT.

consolidate rows in sql not working as it should

I am having this issue with making consolidate for rows, I have made the query, but the result aren't correct.
It may consolidate the rows but for some result it will divide them into two different rows , what i need is to make them all in one row only if the id
is matching.
And here is the query :
Select
trxTransactionSaleItem.TransactionKey
, 'Sale' As TrxnType
, InvProduct.Id
, InvProduct.UPC
, trxTransactionSaleItem.Description
, invproduct.Description2
, invProduct.ProductGroupKey
, sum (Quantity/ISNULL(UOMBaseQuantity,1)) as Quantity
, Price
, SUM(DiscountAmount) AS DA
, SUM(SurchargeTotal) AS ST
, sum (Total) as Total
, ISNULL(UOM.Description,'') as UOM
From
trxTransactionSaleItem
INNER JOIN
InvProduct on trxTransactionSaleItem.ProductKey = InvProduct.ProductKey
LEFT JOIN
InvUOMGroupDetail UOMD on UOMGroupDetailKey = UOMD.UOMGroupDetailKey
LEFT JOIN
InvUOM UOM on UOMD.UOMKey = UOM.UOMKey
Where
Type = 0
And IsExchange = 0
And trxTransactionSaleItem.TransactionKey = 60000000022537
group by
trxTransactionSaleItem.TransactionKey
, InvProduct.Id
, InvProduct.UPC
, trxTransactionSaleItem.Description
, invproduct.Description2
, invProduct.ProductGroupKey
, Quantity
, Price
, DiscountAmount
, SurchargeTotal
, Total
, UOM.Description
So why its not coming in one row ?
Your group by should have only the fields that are not in aggregation functions. It should look like:
group by trxTransactionSaleItem.TransactionKey,
InvProduct.Id,
InvProduct.UPC,
trxTransactionSaleItem.Description,
invproduct.Description2,
invProduct.ProductGroupKey,
Price,
ISNULL(UOM.Description, '')