I have a query to get data like this : .....
WHERE ((column1=1 OR column1=3) AND
(column2= 0 or column2= 4)
) AND (1)
GROUP BY 1,(2)
I don't know the meaning of "AND (1) GROUP BY 1,(2)" does anyone can explain it? , thank you
Let's format the query a little, that should help clear it up:
WHERE ((column1=1 OR column1=3)
AND (column2= 0 or column2= 4) )
AND (1)
GROUP BY 1,(2)
So the AND (1) part is the same as saying AND (true) or AND (1=1). It always returns true so is effectively doing nothing.
The GROUP BY is just using the column position of your SELECT. So it's grouping byt the first column, then the second.
Related
suppose i have this result set:
Location|Company|Account Number|First Check Date|First Status|Second Check Date|Second Status|Third Check Date|Third Status|Fourth Check Date|Fourth Status|Fifth Check Date|Fifth Status
Westeros|Acme Corp.|1014541|8/23/2018|Denied||||||||
Westeros|Acme Corp.|1014544|8/23/2018|Pending||||||||
Westeros|Acme Corp.|1014561|8/23/2018|Pending||||||||
Westeros|Sirius Cybernetics Corp|1014562|8/22/2018|Finalized||||||||
Westeros|Sirius Cybernetics Corp|1014573|8/23/2018|Pending||||||||
Westeros|MomCorp|1014579|8/22/2018|Denied||||||||
Dorne|MomCorp|1018984|8/20/2018|Pending||||||||
Dorne|Sirius Cybernetics Corp|1019017|8/22/2018|Pending||||||||
The North|MomCorp|1033591|8/16/2018|Pending||||||||
The North|MomCorp|1033910|8/16/2018|Not Found||||||||
The North|Amerigroup|1033964|8/16/2018|Partial Payment||||||||
The North|MomCorp|1034036|8/22/2018|Paid||||||||
The North|MomCorp|1034041|8/23/2018|Partial Payment||||||||
Iron Islands|AG White|1175033|8/22/2018|Pending||||||||
Stormlands|foobar|1220179|8/14/2018|Not Found|8/21/2018|Not Found||||||
which is returnd from a query that can be boiled down to
select Location,Company,Account Number,First Check Date,First Status,Second Check Date,Second Status,Third Check Date,Third Status,Fourth Check Date,Fourth Status,Fifth Check Date,Fifth Status
from subselect as results
what i an trying to do is generate a count of elements in each status column.
for example the first status count would be 15, the second would be 1 and the rest are 0.
I have tried doing this with over() and using a sum(case when 'first_status' then1 else 0 end) but nothing seems to give correct results.
any suggestions on how to accomplish this?
Assuming the missing values are NULL, you can do:
select r.*,
count(status1) over () as status1_cnt,
count(status2) over () as status2_cnt,
. . .
from subselect as results
However, the empty statuses might be something else, such as ''. If so, a simple approach is:
select r.*,
count(nullif(status1, '')) over () as status1_cnt,
count(nullif(status2, '')) over () as status2_cnt,
. . .
from subselect as results
I'm trying to get the last record from this query but i don't know how to do it. I used ROW_NUMBER but my program (Protheus ADVPL) don't have resources to get the last line from a query
SELECT ROW_NUMBER() OVER (ORDER BY B1_MASTER, B1_COD) AS ID,
B1_COD,
B1_DESC,
B1_CATEG,
B1_MASTER,
A2_COMPRAD,
ISNULL((SELECT Sum(C6_QTDVEN * C6_PRCVEN)
FROM SC6010 SC6,
SF4010 SF4,
SC5010 SC5
WHERE C6_FILIAL = '01'
AND C6_PRODUTO = B1_COD
AND SC6.D_E_L_E_T_ <> '*'
AND C5_FILIAL = C6_FILIAL
AND C5_NUM = C6_NUM
AND C5_EMISSAO BETWEEN '20160401' AND '20160404'
AND C5_TIPO = 'N'
AND C5_MODAL = '2'
AND SC5.D_E_L_E_T_ <> '*'
(query have 106 lines so i ll not put everything)
I need the total records in a column, like this:
Tabela
What can i do?
Tks
You can use MAX(field) too.
But, you're using ADVPL, so you could use dbSeek instead to find the last RECNO.
So, using "work area" you can find the last record with this:
TRB->(RECCOUNT())
I changed ROW_NUMBER to ##ROWCOUNT and it works! Tks all
I am having an issue with my SELECT query. I am trying to find items that do not start with "50700" and have an enabled flag of 1, are in category 4 and sub category 4. Below is the query i have but it is not returning any results when i know there are some. Please note TBL1.FIELD1 = ITM_ID, TBL1.FIELD2 = ENABLED, TBL2.FIELD1 = CAT_ID, TBL2.FIELD2 = SUBCAT_ID.
SELECT DB_NAME.dbo.TBL1.FIELD1
, DB_NAME.dbo.TBL1.FIELD2
, DB_NAME.dbo.TBL2.FIELD1
, DB_NAME.dbo.TBL2.FIELD2
FROM DB_NAME.dbo.TBL1
, DB_NAME.dbo.TBL2
WHERE DB_NAME.dbo.TBL1.FIELD1 NOT LIKE '50700%'
AND DB_NAME.dbo.TBL1.FIELD2 = 1
AND DB_NAME.dbo.TBL2.FIELD1 = 4
AND DB_NAME.dbo.TBL2.FIELD2 = 4
You can break the query into different subqueries for debug purpose, this way you can figure out which "AND" condition is not working.
For Example:
Firstly I guess you should try a simple query on TBL1.FIELD1 with the simple query :
select TBL1.FIELD1 from DB_NAME.dbo.TBL1 where DB_NAME.dbo.TBL1.FIELD1 NOT LIKE '50700' ;(*if TBL1.FIELD1 is of String type, else go for TBL1.FIELD1 != 50700 * )
Then if you get results ,try to do "AND" with the TBL1.FIELD2 condition.
select TBL1.FIELD1
from DB_NAME.dbo.TBL1
where DB_NAME.dbo.TBL1.FIELD1 NOT LIKE '50700'
and DB_NAME.dbo.TBL1.FIELD2 =4 ;
This way you can proceed further and debug the query. Hope it helps :)
I'm having a Sql code like as follows
Select a.ItemCode, a.ItemDesc
From fn_BOM_Material_Master('A', #AsOnDate, #RptDate, #BranchID, #CompID)a
Left Outer Join fn_INV_AsOnDate_Stock(#StockDate, #AsOnDate, #RptDate, #BranchID, #CompID, #Finyear)b
On a.ItemCode=b.ItemCode and b.WarehouseCode<>'WAP'
and a.BranchID=b.BranchID and a.CompID=b.COmpID
Where a.ItemNatureCode = 'F' and a.BranchID = #BranchID and a.CompID = #CompID
Group by a.ItemCode, a.ItemDesc
Having sum(b.CBQty)<=0
Here the problem is that im passing an "#ShowZeroStock" value as as bit if the "#ShowZeroStock" value is '1' then Having should not be validated or (i.e: All values from the table should be returned including zero)
So How to change the query based on passed bit value "#ShowZeroStock"
I can Use "If else " condition at the top and remove having in else part, but for a lengthy query i can't do the same.
Is this the logic you want?
Having sum(b.CBQty) <= 0 or #ShowZeroStock = 1
In MySQL, is there a way to set the "total" fields to zero if they are NULL?
Here is what I have:
SELECT uo.order_id, uo.order_total, uo.order_status,
(SELECT SUM(uop.price * uop.qty)
FROM uc_order_products uop
WHERE uo.order_id = uop.order_id
) AS products_subtotal,
(SELECT SUM(upr.amount)
FROM uc_payment_receipts upr
WHERE uo.order_id = upr.order_id
) AS payment_received,
(SELECT SUM(uoli.amount)
FROM uc_order_line_items uoli
WHERE uo.order_id = uoli.order_id
) AS line_item_subtotal
FROM uc_orders uo
WHERE uo.order_status NOT IN ("future", "canceled")
AND uo.uid = 4172;
The data comes out fine, except the NULL fields should be 0.
How can I return 0 for NULL in MySQL?
Use IFNULL:
IFNULL(expr1, 0)
From the documentation:
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used.
You can use coalesce(column_name,0) instead of just column_name. The coalesce function returns the first non-NULL value in the list.
I should mention that per-row functions like this are usually problematic for scalability. If you think your database may get to be a decent size, it's often better to use extra columns and triggers to move the cost from the select to the insert/update.
This amortises the cost assuming your database is read more often than written (and most of them are).
None of the above answers were complete for me.
If your field is named field, so the selector should be the following one:
IFNULL(`field`,0) AS field
For example in a SELECT query:
SELECT IFNULL(`field`,0) AS field, `otherfield` FROM `mytable`
Hope this can help someone to not waste time.
You can try something like this
IFNULL(NULLIF(X, '' ), 0)
Attribute X is assumed to be empty if it is an empty String, so after that you can declare as a zero instead of last value. In another case, it would remain its original value.
Anyway, just to give another way to do that.
Yes IFNULL function will be working to achieve your desired result.
SELECT uo.order_id, uo.order_total, uo.order_status,
(SELECT IFNULL(SUM(uop.price * uop.qty),0)
FROM uc_order_products uop
WHERE uo.order_id = uop.order_id
) AS products_subtotal,
(SELECT IFNULL(SUM(upr.amount),0)
FROM uc_payment_receipts upr
WHERE uo.order_id = upr.order_id
) AS payment_received,
(SELECT IFNULL(SUM(uoli.amount),0)
FROM uc_order_line_items uoli
WHERE uo.order_id = uoli.order_id
) AS line_item_subtotal
FROM uc_orders uo
WHERE uo.order_status NOT IN ("future", "canceled")
AND uo.uid = 4172;