Running a simple query like the following yields "Error Code: 1054, Unknown column 'GDP_PPP' in 'field list'
SELECT
COUNTRY
, POPULATION
, GDP_PPP_MIL
, GDP_PPP_MIL * 1E6 AS GDP_PPP
, GDP_PPP / POPULATION
FROM COUNTRIES
I am using mysql v5.7.12 and MySQL Workbench 6.3 on OS X 10.11.4 (El Capitan). I just downloaded it a few days ago, then upgraded the OS to El Cap.
The column you just naming(alias) would not reflect in the query
Some alternatives
1.to use the formula again
SELECT
COUNTRY
, POPULATION
, GDP_PPP_MIL
, GDP_PPP_MIL * 1E6 AS GDP_PPP
, GDP_PPP_MIL * 1E6/ POPULATION
FROM COUNTRIES
Have you tried this ?
SELECT COUNTRY , POPULATION , GDP_PPP_MIL , GDP_PPP_MIL * 1E6 AS 'GDP_PPP' , GDP_PPP / POPULATION FROM COUNTRIES
Related
I am working with SQL and I found this snippet, my question is: what does it mean those minus symbols (-) inside the select statement? I know is a kind of some trick, but I can't find information online about how it is used, please any insight would be welcome.
I am referring to:
SELECT - sum(st.sales) AS sales
- sum(st.orders) AS orders
- sum(st.aov) AS aov
It seems to be related to ledger tables, if you have any documentation, blog or pdf please give me the link to check it.
The full SQL looks like this:
INSERT INTO sales_test
WITH source_query AS --find the existing values in the ledger table and invert them
(
SELECT
st.og_date
, st.merchant
, st.store_name
, st.country
, st.kam
, st.class
, st.origin
, - sum(st.sales) AS sales
, - sum(st.orders) AS orders
, - sum(st.aov) AS aov
, et.source_file_name
, et.source_file_timestamp
FROM
sales_test st
INNER JOIN
ext_sales_test et
ON
city_hash(et.og_date, et.merchant, et.store_name, et.country, et.kam, et.class, et.origin) = city_hash(st.og_date, st.merchant, st.store_name, st.country, st.kam, st.class, st.origin)
AND st.og_date = et.og_date
AND st.merchant = et.merchant
GROUP BY
st.og_date
, st.merchant
, st.store_name
, st.country
, st.kam
, st.class
, st.origin
, et.source_file_name
, et.source_file_timestamp
)
, union_query AS --if we union the incoming data with the inverted existing data, we get the difference that needs to be ledgered
(
SELECT *
FROM
source_query
UNION ALL
SELECT *
FROM
ext_sales_text
)
It makes the numeric value negative(if numeric value is negative, - - is positive), in your case it first performs the sum and then it makes it negative or positive:
As an example:
USE tempdb;
GO
DECLARE #Num1 INT;
SET #Num1 = 5;
SELECT #Num1 AS VariableValue, -#Num1 AS NegativeValue;
GO
Result set:
VariableValue NegativeValue
------------- -------------
5 -5
(1 row(s) affected)
Further info here
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?
Sorry you will have to bear with me as i am relativly new to SQL. I am querying an ODBC in EXCEL. At the moment my dataset is massive so i am looking to narrow it down by grouping it by company name and date. not all my columns are calculated fields.I have put the Sum on the two i need adding up. When i try to return the data i get the error of Non-group-by expression in select clause
Please can someone help me out.
SELECT
SopOrder_0.SooOrderNumber
, Company_0.CoaCompanyName
, InvoiceCreditItem_0.InvoiceCreditItemID
, InvoiceCreditItem_0.IciInvoiceApproved
, InvoiceCreditItem_0.InvoiceCreditID
, InvoiceCreditItem_0.CompanySiteID
, InvoiceCreditItem_0.VatID
, InvoiceCreditItem_0.NominalID
, InvoiceCreditItem_0.IciCreatedDate
, Sum(InvoiceCreditItem_0.IciTotalNettValue)
, Sum(InvoiceCreditItem_0.IciVatValue)
FROM
SBS.PUB.Company Company_0
, SBS.PUB.Customer Customer_0
, SBS.PUB.InvoiceCreditItem InvoiceCreditItem_0
, SBS.PUB.SopOrder SopOrder_0
WHERE
SopOrder_0.SopOrderID = InvoiceCreditItem_0.SopOrderID
AND InvoiceCreditItem_0.CompanyID = Customer_0.CompanyID
AND InvoiceCreditItem_0.CompanyID = Company_0.CompanyID
AND (Company_0.CoaCompanyName<>'ATOS')
AND InvoiceCreditItem_0.IciCreatedDate >= ?
GROUP BY
Company_0.CoaCompanyName, InvoiceCreditItem_0.IciCreatedDate
The SQL query below worked... But when I add the "FreightCostsPerOrderSplit" fields (3 new fields), I get the ORA-01747 error message seen below. The conventions are similar to what was being done before in the SQL code, and I was unsure what could be causing this error message.
Error at line 49 ORA-01747: invalid user.table.column, table.column,
or column specification
As I was posting this question, I figured out the solution. I decided to post this answer for others who may come across this same problem.
The solution involved replacing COUNT(OHH.*) with COUNT(OHH.FieldName) in the divisor of the calculated fields. See the answer section for commentary regarding count(*) vs count(column-name) syntax.
, (SUM(cteLoadFreightAgg.TOTALFREIGHT)/ COUNT(OHH.*)) AS "TotalFreight_SplitPerOrder"
SQL code (broken at Line 49 with solution posted in answer):
/* A list of aggregated freight carrier data grouped by LOAD (no BILLTOCUST)
Pulled from [LOAD_CARRIER] table. Another option is [FRT_LOAD] which include LUMPER
SOURCE: Loads Underutilize Report
*/
cteLoadFreightAgg AS
(
SELECT
LOAD_CARRIER.LOAD_SEQ_CODE AS LoadNumber
, SUM(LOAD_CARRIER.FLAT_FRT + LOAD_CARRIER.FUEL_FRT + LOAD_CARRIER.DROP_FRT + LOAD_CARRIER.MISC_FRT + LOAD_CARRIER.OUT_ROUTE_FRT + LOAD_CARRIER.LAYOVER_FRT) AS TotalFreight
, SUM(LOAD_CARRIER.DROP_FRT + LOAD_CARRIER.MISC_FRT + LOAD_CARRIER.OUT_ROUTE_FRT + LOAD_CARRIER.LAYOVER_FRT) AS AssessorialsFreight
, SUM(LOAD_CARRIER.FLAT_FRT) AS FlatRateFreight
, SUM(LOAD_CARRIER.FUEL_FRT) AS FuelFreight
FROM
LOAD_CARRIER
GROUP BY
LOAD_CARRIER.LOAD_SEQ_CODE
)
SELECT
OHH.BILL_COMP_CODE
, LOAD.DEPART_FROM_WHSE_CODE
, OHH.SHIP_FROM_WHSE
, OHH.ORDER_SEQ_CODE
, OHH.LOAD_SEQ_CODE
, OHH.SHIP_DATE
, CARRIER.NAME AS CarrierName
, OHH.DELIVERY_DATE
, CUST.CUST_CODE || '-' || CUST.CUST_SHIPTO_CODE AS ShiptoCustCode
, CUST.NAME AS ShiptoName
, OHH.BILL_PO_ID
, CUST.CITY
, CUST.STATE_CODE
, LOAD.TALLY_DATE_TIME
, OHH.APPT_DATE
, OHH.APPT_TIME
, OHH.EST_PALLETS
, OHH.EST_GROSS_WGT
, OHH.EST_NET_WGT
, LOAD.CLASS_CODE
, SUM(ODH.WEIGHT_SHIPPED) AS "SUM_ODH_WeightShipped"
-- FreightCostLoad
, cteLoadFreightAgg.TOTALFREIGHT
, cteLoadFreightAgg.FUELFREIGHT
, (cteLoadFreightAgg.TOTALFREIGHT - cteLoadFreightAgg.FUELFREIGHT) AS "OTHERFREIGHT"
-- FreightCostsPerOrderSplit
, (SUM(cteLoadFreightAgg.TOTALFREIGHT)/ COUNT(OHH.*)) AS "TotalFreight_SplitPerOrder"
, (SUM(cteLoadFreightAgg.FUELFREIGHT) / COUNT(OHH.*)) AS "FuelFreight_SplitPerOrder"
, ((SUM(cteLoadFreightAgg.TOTALFREIGHT) - SUM(cteLoadFreightAgg.FUELFREIGHT))/ COUNT(OHH.*)) AS "OtherFreight_SplitPerOrder"
FROM
ROCCO.CARRIER CARRIER
INNER JOIN ROCCO.LOAD_CARRIER LOAD_CARRIER
ON CARRIER.CARRIER_CODE = LOAD_CARRIER.CARRIER_CODE
INNER JOIN ROCCO.ORD_HDR_HST OHH
ON LOAD_CARRIER.LOAD_SEQ_CODE = OHH.LOAD_SEQ_CODE
INNER JOIN ROCCO.ORD_DTL_HST ODH
ON OHH.ORDER_SEQ_CODE = ODH.ORDER_SEQ_CODE
INNER JOIN ROCCO.LOAD LOAD
ON OHH.LOAD_SEQ_CODE = LOAD.LOAD_SEQ_CODE
INNER JOIN ROCCO.CUSTOMER_SHIPTO CUST
ON OHH.CUST_CODE = CUST.CUST_CODE
AND OHH.CUST_SHIPTO_CODE = CUST.CUST_SHIPTO_CODE
INNER JOIN cteLoadFreightAgg
ON LOAD.LOAD_SEQ_CODE = cteLoadFreightAgg.LOADNUMBER
WHERE
-- OHH.SHIP_DATE BETWEEN :paramStartDate AND :paramEndDate
-- AND OHH.STATUS_CODE<>'DL'
-- AND OHH.BILL_COMP_CODE IN (:paramCompany)
-- AND LOAD.DEPART_FROM_WHSE_CODE IN (:paramWarehouse)
-- AND OHH.MASTER_ORDER_NBR IS NULL
-- AND LOAD.CLASS_CODE IN (:paramClassCode)
-- AND CUST.CUST_CODE || '-' || CUST.CUST_SHIPTO_CODE IN (:paramShipto)
-- /*
OHH.SHIP_DATE BETWEEN TO_DATE('10/1/2018', 'MM/DD/YYYY') AND TO_DATE('10/31/2018', 'MM/DD/YYYY')
AND OHH.STATUS_CODE<>'DL'
AND OHH.BILL_COMP_CODE=100
AND OHH.MASTER_ORDER_NBR IS NULL
-- */
GROUP BY
OHH.BILL_COMP_CODE
, LOAD.DEPART_FROM_WHSE_CODE
, OHH.SHIP_FROM_WHSE
, OHH.ORDER_SEQ_CODE
, OHH.LOAD_SEQ_CODE
, OHH.SHIP_DATE
, CARRIER.NAME
, OHH.DELIVERY_DATE
, CUST.CUST_CODE || '-' || CUST.CUST_SHIPTO_CODE
, CUST.NAME
, OHH.BILL_PO_ID
, CUST.CITY
, CUST.STATE_CODE
, LOAD.TALLY_DATE_TIME
, OHH.APPT_DATE
, OHH.APPT_TIME
, OHH.EST_PALLETS
, OHH.EST_GROSS_WGT
, OHH.EST_NET_WGT
, LOAD.CLASS_CODE
, cteLoadFreightAgg.FUELFREIGHT
, cteLoadFreightAgg.TOTALFREIGHT
ORDER BY
OHH.SHIP_DATE
*
Error at line 49
ORA-01747: invalid user.table.column, table.column, or column specification
Initially I had the calculated field as:
, (SUM(cteLoadFreightAgg.TOTALFREIGHT)/ COUNT(OHH.*)) AS "TotalFreight_SplitPerOrder".
For all 3 calculate fields, I changed the divisor field to reference a specific field and the error message was resolved. Then once that syntaxed parsed successfully, the SSRS report gave a "#VALUE" error message, and I had to use CAST() function to get a proper evaluation instead of an error message in the SSRS report.
, CAST((cteLoadFreightAgg.TOTALFREIGHT/ COUNT(OHH.ORDER_SEQ_CODE) OVER(PARTITION BY OHH.LOAD_SEQ_CODE)) AS NUMERIC(9,2)) AS "TotalFreight_SplitPerOrder"
So after resolving the original error message, I then figured out I should be doing a WINDOWING function to divide the value as the report requirements dictated.
I did some reading online for an answer to "Why did COUNT(FieldName) evaluate while COUNT(*) gave the error message?". I came across a few blog articles but they were mostly regarding performance concerns, not row count division or this error message I received.
https://www.sqlskills.com/blogs/conor/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over/
https://www.mssqltips.com/sqlservertip/4460/sql-server-count-function-performance-comparison/
https://www.sqlhammer.com/transact-sql-count-vs-countcolumn-vs-count1/
As the title says when I use the sql between clause I am running it against an address range from low to high and I am putting in a number that is outside of the two and getting rows returned to me. And I would like to know why.
http://www.sqlfiddle.com/#!6/49467/2
Quick hit on the query, I am using an address number of 4929 and getting rows returned to me where the address range low and high numbers are 400 and 498 respectively.
Here is the query:
SELECT
ZipCodeLow ,
ZipCodeHigh ,
ZipExtensionLow ,
EndingEffectiveDate ,
BeginningEffectiveDate ,
AddressRangeLow ,
AddressRangeHigh ,
StreetName ,
City ,
ZipCode ,
Zip4,
Zip4High
FROM BoundTable
WHERE
('68503' BETWEEN ZipCodeLow AND ZipCodeHigh) AND
('4929' BETWEEN [AddressRangeLow] AND [AddressRangeHigh]) AND
([StreetName] = '32ND') AND
(GETDATE() BETWEEN [BeginningEffectiveDate] AND [EndingEffectiveDate])
Convert 4929 to numeric first and then run the query:
SELECT
ZipCodeLow ,
ZipCodeHigh ,
ZipExtensionLow ,
EndingEffectiveDate ,
BeginningEffectiveDate ,
AddressRangeLow ,
AddressRangeHigh ,
StreetName ,
City ,
ZipCode ,
Zip4,
Zip4High
FROM BoundTable
WHERE
('68503' BETWEEN ZipCodeLow AND ZipCodeHigh) AND
(convert(numeric,'4929') BETWEEN [AddressRangeLow] AND [AddressRangeHigh]) AND
([StreetName] = '32ND') AND
(GETDATE() BETWEEN [BeginningEffectiveDate] AND [EndingEffectiveDate])