postgresql mathematical formula error - postgresql

Hi I am trying to use a mathematical function on each row in postgresql.
But It gives me a error.
My Query:
Select
stock_inventory_line.product_code AS Sku,
COUNT(sale_order_line.name) AS Qty_Sold,
stock_inventory_line.product_qty AS Current_Qty,
(stock_inventory_line.product_qty / Qty_Sold) AS NOM
From
sale_order_line,
product_product,
product_template,
product_category,
stock_inventory_line
WHERE
sale_order_line.product_id = product_product.id AND
product_product.product_tmpl_id = product_template.id AND
product_template.categ_id = product_category.id AND
product_product.default_code = stock_inventory_line.product_code
GROUP BY
Sku,
Current_Qty,
NOM;
On this Query It gives me a error: column qty_sold doesn't exist.
If i change the 5th line to
(stock_inventory_line.product_qty / COUNT(sale_order_line.name)) AS NOM
It gives me an error: Aggregate functions not allowed in group by.

You are trying to use COUNT(sale_order_line.name) as a group by item. Aggreagte functions work on grouped item. They are not for grouping them.
I do not know your tables but try
Select
stock_inventory_line.product_code AS Sku,
COUNT(sale_order_line.name) AS Qty_Sold,
stock_inventory_line.product_qty AS Current_Qty,
(stock_inventory_line.product_qty / COUNT(sale_order_line.name)) AS NOM
From
sale_order_line,
product_product,
product_template,
product_category,
stock_inventory_line
WHERE
sale_order_line.product_id = product_product.id AND
product_product.product_tmpl_id = product_template.id AND
product_template.categ_id = product_category.id AND
product_product.default_code = stock_inventory_line.product_code
GROUP BY
stock_inventory_line.product_code,
stock_inventory_line.product_qty;
Basically I remove NOM from the GROUP BY. It is a product for each group, not something you group by.

I run into problems like this all the time. Using a CTE always works for me. Do your aggregations inside the CTE then when you call them in the outer statement, Postgres sees them as numeric instead of aggregations. Your query might run a bit faster too!
Example:
WITH cte AS(
SELECT
sale_order_line.product_id,
stock_inventory_line.product_code,
stock_inventory_line.product_code AS Sku,
COUNT(sale_order_line.name) AS Qty_Sold,
stock_inventory_line.product_qty AS Current_Qty,
(stock_inventory_line.product_qty / Qty_Sold) AS NOM
FROM
sale_order_line,
stock_inventory_line)
SELECT Sku, Qty_Sold, Current_Qty, NOM
FROM
cte,
product_product,
product_template,
product_category
WHERE
cte.product_id = product_product.id AND
product_product.product_tmpl_id = product_template.id AND
product_template.categ_id = product_category.id AND
product_product.default_code = cte.product_code
GROUP BY
Sku,
Current_Qty,
NOM;

Related

Getting duplicate column ERROR while trying to insert same column with two different datatypes using SELECT INTO clause in PostgreSql

I need to insert createdate column twice with two different datatypes one with the datatype defined in the table itself and another in char datatype.
I can insert it by changing the alias name of createdate column but can't insert with same alias name which i need.
so help me out to get correct way of doing it.
My query:
SELECT DISTINCT TE.id, T.debatchqueuelink, TE.transactionlink,
EC.errorclassification, TE.errorvalue,
EC.errorparameter, TE.classificationlink, TE.description,
TE.createdate AS createdate, TO_CHAR(TE.createdate, 'MM/dd/yyyy') AS createdate,
TE.status, TE.rebutt, TE.rebuttedstatus, BQ.appbatchnumber,
BQ.scanbatchnumber, BQ.clientlink, BQ.locationlink, T.patientid,
(DEUD.firstname|| ' ' ||DEUD.lastname) AS deusername, DEUD.email AS deuseremail,
(QCUD.firstname|| ' ' ||QCUD.lastname) AS qcusername, TE.inactive,
TE.decomment
INTO table373
FROM qctransactionerror TE
INNER JOIN errorclassification EC ON EC.id = TE.classificationlink
INNER JOIN qctransaction T ON T.id = TE.transactionlink
INNER JOIN batchqueue BQ ON T.debatchqueuelink = BQ.id
INNER JOIN batchqueue QCBQ ON T.qcbatchqueuelink = QCBQ.id
INNER JOIN userdetail QCUD ON QCBQ.assignedto = QCUD.id
INNER JOIN userdetail DEUD ON BQ.assignedto = DEUD.id
WHERE TE.inactive='t'
AND TE.status IN ('ERROR','QCCORRECTED')
LIMIT 0
The actual error message I am getting is:
Duplicate column:column "createdate" specified more than once

PostgreSql Group By and aggreate function error

My problem is, when I run the following query in MySQL, it looks like this
Query;
SELECT
CONCAT(b.tarih, '#', CONCAT(b.enlem, ',', b.boylam), '#', b.aldigi_yol) AS IlkMesaiEnlemBoylamImei,
CONCAT(tson.max_tarih, '#', CONCAT(tson.max_enlem, ',', tson.max_boylam), '#', tson.max_aldigi_yol) AS SonMesaiEnlemBoylamImei,
Max(CAST(b.hiz AS UNSIGNED)) As EnYuksekHiz,
TIME_FORMAT(Sec_TO_TIME(TIMESTAMPDIFF(SECOND, (b.tarih), (tson.max_tarih))), '%H:%i') AS DurmaSuresi
FROM
(Select id as max_id, tarih as max_tarih, enlem as max_enlem, boylam as max_boylam, aldigi_yol as max_aldigi_yol from _213gl2015016424 where id in(
SELECT MAX(id)
FROM _213gl2015016424 where (tarih between DATE('2016-11-30 05:45:00') AND Date('2017-01-13 14:19:06')) AND CAST(hiz AS UNSIGNED) > 0
GROUP BY DATE(tarih))
) tson
LEFT JOIN _213gl2015016424 a ON a.id = tson.max_id
LEFT JOIN _213gl2015016424 b ON DATE(b.tarih) = DATE(a.tarih)
WHERE b.tarih is not null And (b.tarih between DATE('2016-11-30 05:45:00') AND Date('2017-01-13 14:19:06')) AND b.hiz > 0
GROUP BY tson.max_tarih
Output is order by date;
Result query
When I try to run a query in PostgreSQL, I get group by mistake.
Query;
SELECT
CONCAT(b.tarih, '#', CONCAT(b.enlem, ',', b.boylam), '#', b.toplamyol) AS IlkMesaiEnlemBoylamImei,
CONCAT(tson.max_tarih, '#', CONCAT(tson.max_enlem, ',', tson.max_boylam), '#', tson.max_toplamyol) AS SonMesaiEnlemBoylamImei,
Max(CAST(b.hiz AS OID)) As EnYuksekHiz,
to_char(to_timestamp((extract(epoch from (tson.max_tarih)) - extract(epoch from (b.tarih)))) - interval '2 hour','HH24:MI') AS DurmaSuresi
FROM
(Select id as max_id, tarih as max_tarih, enlem as max_enlem, boylam as max_boylam, toplamyol as max_toplamyol from _213GL2016008691 where id in(
SELECT MAX(id)
FROM _213GL2016008691 where (tarih between DATE('2018-02-01 03:31:54') AND DATE('2018-03-01 03:31:54')) AND CAST(hiz AS OID) > 0
GROUP BY DATE(tarih))
) tson
LEFT JOIN _213GL2016008691 a ON a.id = tson.max_id
LEFT JOIN _213GL2016008691 b ON DATE(b.tarih) = DATE(a.tarih)
WHERE b.tarih is not null And (b.tarih between DATE('2018-02-12 03:31:54') AND DATE('2018-02-13 03:31:54')) AND b.hiz > 0
GROUP BY tson.max_tarih
Group by error is : To use the aggregate function, you must add the column "b.tarih" to the GROUP BY list.
When I add it I get the same error for another column.I'm waiting for your help.
You are using a feature of MySQL that is not standard SQL and you can also deactivate.
You are grouping by tson.max_tarih in your query. That means that for all rows that share the same value in that field, you will get only one row as a result of that group.
If you have several different values in the rest of the fields (enlem, boylam, etc...) which one are you trying to get in as the result of the query? That's the question that PostgreSQL is asking you.
MySQL is just returning any value for those fields among the rows in the group. PostgreSQL requires you to actually specify it.
Two typical solutions would be grouping by the rest of the fields (b.tarih, b.enlem) or specifying the value those fields to something like MAX(b.tarih), etc.

Firebird WHERE clause in selecting fields

I Have here firebird query and I dont think if this is correct.
SELECT
COUNT(STATUS) FROM(SELECT STATUS FROM T_TABLE2 WHERE STATUS = 'FAIL') AS FAIL,
DEVICENAME,
SITE_NUM,
COUNT(TEST_NUM)
FROM T_TABLE2 A
INNER JOIN T_TABLE1 B
ON A.TBL1_ID = B.ID
WHERE B.DEVICENAME = 'TANM1A009A_P28'
AND A.TEST_NUM = 'T810'
GROUP BY
DEVICENAME,
TESTER,
SITE_NUM
I want to count the row of TEST_NUM even if it has FAIL value and I also want to count the STATUS that has FAIL value.
Thanks
I really do not know FireBird but from MSSQL point of view why don'y you try this??
SELECT
COUNT(A.STATUS) as 'FAIL',
B.DEVICENAME,
(TableofYourSitenum)SITE_NUM, -- i do no know what table this from
(TableofYourTestnum)COUNT(TEST_NUM) as 'TESTNUM COUNT' --and this also you need to put the table name,
FROM T_TABLE2 A
INNER JOIN T_TABLE1 B
ON A.TBL1_ID = B.ID
WHERE B.DEVICENAME = 'TANM1A009A_P28'
AND A.TEST_NUM = 'T810'
GROUP BY
A.STATUS,
(TableofYourTestnum)TESTNUM

Querydsl - How use group by and sum

I'd like to do a query than in sql it's:
SELECT users.id, SUM(total), SUM(total*price) FROM sales INNER JOIN users ON sales.id_user=users.id GROUP BY users.id
I tried searching for the solution, but the closest I get was:
QSales sales = QSales.sales;
JPAQuery query = from(sales);
QUsers users = QUsers.users;
query.innerJoin(sales.users, users);
List<Object[]> response = query.groupBy(sales.user).list(sales.user, sales.total.sum());
but i don't know how to get this:
SUM(total*price)
SUM(total*price)
can be expressed as
total.multiply(price).sum()
you can use NumberExpression
NumberExpression totalPrice = sales.total.multiply(sales.price);
then use totalPrice.sum()

Why is this field not showing up in the results?

When I run the selects below, I do not get Field3 in the result set, why?
Select
a.Field1,
a.Field2,
a.Field3,
sum(IsNull(a.Field4, 0)) AS SomeAlias1,
a.SomeField5,
a.SomeField6,
a.SomeField7
From SomeTable a
INNER JOIN SomeView1 v on v.au = a.au
inner join (select Username, House from Users userBuildings where UserName = #UserName) as userHouses on userHouses.au = a.au
WHERE
(((where claus logic here....
Group BY a.Field1,
a.Field2,
a.SomeAlias1,
a.Field3,
a.Field4,
a.Field5,
a.Field6,
a.Fielf7
)
Select
transBudget.Field1,
transBudget.Field2,
transDiscount.Field4,
... some other fields...
IsNull(transDiscount.Actual, 0) - IsNull(transBudget.Actual, 0) AS Variance
from (Select * from Transactdions Where TransDesc = 'Budget') AS transBudget
FULL OUTER JOIN
(Select * from Transactions Where TransDesc = 'Discount') AS transDiscount
ON transBudget.Market = transDiscount.Market AND transBudget.SubMarket = transDiscount.SubMarket
I see every field except Field3 for some reason and it's beyond me how the heck this can happen.
In the second part of your query, you are missing field 3.
Select
transBudget.Field1,
transBudget.Field2,
transDiscount.Field4,
... some other fields...
IsNull(transDiscount.Actual, 0)
You appear to have two separate SQL queries there. The first one contains Field3, but the second one does not.