how do I loop through a set of records - sql

how do I loop through a set of records from a select?
Here is my stored procedure query:
ALTER PROCEDURE [dbo].[procedure_rastavljanjeOtpremnica] (#radniNalogID INT) AS
DECLARE #bool BIT = 0
IF(#bool = 'FALSE')
BEGIN
SELECT ROW_NUMBER() OVER(ORDER BY S.PuniNaziv) AS num_row, S.PuniNaziv, a.KolicinaMjeracaIElektronike FROM ArtikliUslugeNaloga a
INNER JOIN SifreArtikala S ON S.Id = a.Artikal1Id
INNER JOIN UslugeNaloga ON UslugeNaloga.Id = a.UslugeNalogaId
WHERE a.RobaSkart = #bool AND UslugeNaloga.RadniNalogID = #radniNalogID
END
The result is:
now_row PuniNaziv Kolicina
1 Komunik internet jed s ugrađ WIFi sensoNET 3
2 Ventil tahret DN15 (1/2") 5
3 Vodokotlić Geberit 1
So what I need to do is to add another column that will print KOM for every row,
This is the result that I am looking for:
now_row PuniNaziv Kolicina Jedinia mjere
1 Komunik internet jed s ugrađ WIFi sensoNET 3 Kom
2 Ventil tahret DN15 (1/2") 5 Kom
3 Vodokotlić Geberit 1 Kom
Can anyone give me idea how to do this

There is no need to "loop". Just add the column to your query:
SELECT ROW_NUMBER() OVER (ORDER BY S.PuniNaziv) AS num_row,
S.PuniNaziv, a.KolicinaMjeracaIElektronike,
'Kom' as Jedinia_mjere
FROM ArtikliUslugeNaloga a JOIN
SifreArtikala S
ON S.Id = a.Artikal1Id JOIN
UslugeNaloga
ON UslugeNaloga.Id = a.UslugeNalogaId
WHERE a.RobaSkart = #bool AND
UslugeNaloga.RadniNalogID = #radniNalogID;
Note: If you want to ensure that the rows are returned in the order specified by num_row, you should add ORDER BY num_row to the query.

Related

How to get Odoo Inventory adjustment value through SQL

I am working on a custom stock valuation module and in one model I am trying to get adjustment value for a lot - product - warehouse wise of the previous day.
QUERY 1
SELECT COUNT(*)
FROM
(
SELECT stock_inventory.date AS stock_adjustment_date,
stock_move_line.lot_id,
stock_move_line.product_id,
SUM(stock_move_line.qty_done) total_stock_adjustment
FROM stock_move_line
LEFT JOIN stock_move ON stock_move_line.move_id = stock_move.id
LEFT JOIN stock_inventory ON stock_move.inventory_id = stock_inventory.id
WHERE stock_move.inventory_id IS NOT NULL
AND stock_move_line.location_id = 5
AND stock_move_line.location_dest_id = 13
AND stock_move_line.lot_id IS NOT NULL
GROUP BY stock_move_line.lot_id, stock_move_line.product_id, stock_inventory.date
ORDER BY total_stock_adjustment DESC
)
testTable;
QUERY 2
SELECT COUNT(*)
FROM
(
SELECT stock_inventory.date AS stock_adjustment_date,
stock_move_line.lot_id,
stock_move_line.product_id,
SUM(stock_move_line.qty_done) total_stock_adjustment
FROM stock_move_line
LEFT JOIN stock_move ON stock_move_line.move_id = stock_move.id
LEFT JOIN stock_inventory ON stock_move.inventory_id = stock_inventory.id
WHERE stock_move.inventory_id IS NOT NULL
AND stock_move_line.location_id = 13
AND stock_move_line.location_dest_id = 5
AND stock_move_line.lot_id IS NOT NULL
GROUP BY stock_move_line.lot_id, stock_move_line.product_id, stock_inventory.date
ORDER BY total_stock_adjustment DESC
)
testTable;
Why these both queries returning same count 14,849 ?
13 is the warehouse ID and 5 is the virtual location used for adjustment. What I am doing wrong here?

SQL Server stored procedure store multiple rows of SELECT statement result into single variable

I have a query with a SELECT statement that will return 2 or more rows as a result. How can I store these rows of data into a variable? Because I need the variable to check whether any of the rows is empty/null. How can I achieve this?
So far I've done this:
BEGIN
SELECT
#AINum = ISNULL(so.U_SI7_DPDocNum, 0), #soDocNum = so.DocNum
FROM
DLN1 doline
INNER JOIN
ORDR so ON doline.BaseRef = so.DocNum
WHERE
doline.DocEntry = #docEntry
WHILE(#AINum IS NOT NULL)
BEGIN
IF(#AINum <= 0)
BEGIN
SELECT #errCode = 003;
RETURN;
END
END
END
UPDATED query using EXISTS
SELECT #errCode = 003
WHERE NOT EXISTS (SELECT so.U_SI7_DPDocNum
FROM DLN1 doline
INNER JOIN ORDR so ON doline.BaseRef = so.DocNum
WHERE doline.DocEntry = #docEntry)
RETURN;
The #AINum will have to store multiple rows of data from the SELECT statement result. #errCode is an output variable.
Thank you.
-- initialize to 0
SELECT #errCode = 0;
-- assign value of 003 if it the DPDocNum is NULL or < 0
SELECT #errCode = 003
FROM DLN1 doline
INNER JOIN ORDR so ON doline.BaseRef = so.DocNum
WHERE doline.DocEntry = #docEntry
AND (so.U_SI7_DPDocNum IS NULL OR so.U_SI7_DPDocNum <= 0)

Get the sum of a count column in SQL

I have the following query
SELECT
dtc.coupon_type_company_name,
COUNT(*) * dtc.coupon_type_company_coupon_amount AS 'Total_Coupon_To_Be_Used',
dtc.coupon_type_company_coupon_months_combinable
FROM
[dbo].[coupon_type_Company_User] dtcu
JOIN
coupon_type_Company dtc ON dtcu.coupon_type_Company_ID = dtc.id
JOIN
person p ON dtcu.userID = p.userID
WHERE
coupon_type_company_coupon_is_combinable = 1
OR coupon_type_company_has_coupon = 1
AND dtc.companyID = 1081
AND p.is_active = 1
GROUP BY
dtc.coupon_type_company_name,dtc.coupon_type_company_coupon_amount,
dtc.coupon_type_company_coupon_months_combinable
This returns the following:
What I want to have however is just one column and one row that should take the SUM of my middle column (count(*)*dtc.coupon_type_company_coupon_amount).
How could I achieve this and prevent doing this in my code backend (C#)
You can wrap your query like this:
SELECT SUM(Total_Coupon_To_Be_Used) AS the_sum
FROM (
your query
) s
Use a "Table Expression", as in:
select sum(Total_Coupon_To_Be_Used) from (
SELECT dtc.coupon_type_company_name,
count(*) * dtc.coupon_type_company_coupon_amount as 'Total_Coupon_To_Be_Used',
dtc.coupon_type_company_coupon_months_combinable
FROM [dbo].[coupon_type_Company_User] dtcu
JOIN coupon_type_Company dtc ON dtcu.coupon_type_Company_ID = dtc.id
JOIN person p ON dtcu.userID = p.userID
WHERE coupon_type_company_coupon_is_combinable = 1
or coupon_type_company_has_coupon = 1
and dtc.companyID = 1081
AND p.is_active = 1
GROUP BY
dtc.coupon_type_company_name,dtc.coupon_type_company_coupon_amount,
dtc.coupon_type_company_coupon_months_combinable
) x

order by first occurence of returned results

I have two tables and want to get technology_tag from table b based on reference id from table a:
select b.dbid, b.technology_tag
from tblConnect a, tblSites b
where a.Site_DBID = 2
and a.Related_Site_DBID = 1
and (b.dbid = a.bsc_tag_dbid or b.dbid = a.related_bsc_dbid or b.dbid = a.related_msc_dbid)
What I want to have here is to order the returned rows based on first occurrence (in where clause)
1st- b.dbid = a.bsc_tag_dbid
2cnd- b.dbid = a.related_bsc_dbid
3rd- b.dbid = a.related_msc_dbid
Does anyone has a clue how to do that?
First, you should switch to using actual JOIN clauses when performing joins. That said, this ORDER BY clause should do what you want:
SELECT
B.dbid,
B.technology_tag
FROM tblConnect A
INNER JOIN tblSites B ON
B.dbid IN (A.bsc_tag_dbid, A.related_bsc_dbid, A.related_msc_dbid)
WHERE
A.Site_DBID = 2 AND
A.Related_Site_DBID = 1
ORDER BY
CASE
WHEN B.dbid = A.bsc_tag_dbid THEN 1
WHEN B.dbid = A.related_bsc_dbid THEN 2
WHEN B.dbid = A.related_msc_dbid THEN 3
ELSE 4 -- Not really necessary, but I always use an ELSE when I use CASE
END

How to Optimize this SQL Query?

I have 3 tables:
CRSTasks (ID,parentID)
CRSTaskReceivers (ID,tskID,receiverID)
UserNames (id,name)
...relation between CRSTasks and CRSTaskReceivers one-to-many
between UserNames and CRSTaskReceivers one-to-one
tasks
ID parent
1 null
10 1
50 1
taskReceivers
id taskID receiverID
1 1 4(john)
1 10 2(mike)
1 50 3(brand)
I need result like that:
taskid Receivers
-------------------
1 jone,mike,brand
ONLY FOR PARENT TASKS IT WILL CONCATE RECEIVERS
SQL Server 2005+:
SELECT t.id AS taskid,
STUFF((SELECT ','+ x.name
FROM (SELECT COALESCE(pu.[ArabicName], aut.Name) AS name
FROM CRSTaskReceivers tr
JOIN AD_USER_TBL aut ON aut.id = tr.receiverid
LEFT JOIN PORTAL_USERS pu ON pu.id = aut.id
WHERE tr.crstaskid = t.id
AND tr.receivertype = 1
UNION
SELECT agt.name
FROM CRSTaskReceiver tr
JOIN AD_GROUP_TBL sgt ON agt.id = tr.receiverid
WHERE tr.receivertype = 3
AND tr.crstaskid = t.id) x
FOR XML PATH('')), 1, 1, '')
FROM CRSTasks t
Don't need the function.
Besides the odd string concatenation going on it sure looks like all that could be done in one query instead of four. It's perfectly fine to have more than one criteria in a join. Something along:
FROM CRSTaskReceiver
INNER JOIN CRSTask
ON CRSTaskReceiver.CRSTaskID = CRSTask.ID
INNER JOIN CRS_BuiltinGroup
ON CRSTaskReceiver.ReceiverID = CRS_BuiltinGroup.ID AND CRSTaskReceiver.ReceiverType = 4
WHERE CRSTask.ParentTask = #TaskID
Also the below part of the function seems to do absolutely nothing. What is it meant to do?
DECLARE #tmpLength INT
SET #tmpLength = 0
SET #tmpLength = LEN(#tmp)
IF #tmpLength > 0
BEGIN
SET #tmp = SUBSTRING(#tmp, 0, #tmpLength)
END