I want to execute this from within Excel (SQL query window) where I would normally only execute the select statement. Should I make it into a stored proceedure and then execute tat from within Excel?
BEGIN TRY
drop table GlobalShop.dbo.v_order_hist_dtl_Quote
END TRY
BEGIN CATCH
END CATCH
BEGIN TRY
drop table GlobalShop.dbo.v_order_lines_Quote
END TRY
BEGIN CATCH
END CATCH
select * into GlobalShop.dbo.v_order_hist_dtl_Quote
from GlobalShop.dbo.v_order_hist_dtl where isnull(USER_3,'') <> ''
select * into GlobalShop.dbo.v_order_lines_Quote
from GlobalShop.dbo.v_order_lines where isnull(USER_3,'') <> ''
/* Quotes with Orders 1.0 */
select
QT.QuoteNum,
QT.custID,
QT.QCustNum,
QT.QCustName,
QT.QDate,
COUNT(OL.order_no) as #_Open_Orders,
COUNT(OH.order_no) as #_Closed_Orders
from
"Track Quotes".dbo.TrackQuotesHist as QT
left outer join GlobalShop.dbo.v_order_hist_dtl_quote as OH
on QT.QuoteNum = OH.USER_3
left outer join GlobalShop.dbo.v_order_lines_quote as OL
on QT.QuoteNum = OL.USER_3
where
QT.QDate between '03/01/2015' and '03/06/2015'
Group By
QT.QuoteNum,
QT.custID,
QT.QCustNum,
QT.QCustName,
QT.QDate
If you only want to run the select statements most of the time I would make the drops into one stored procedure (if you ever want to do it) and then the select into another.
In my world I assume an object that starts with v_ is a view but you seem to treat it as a table and if it is change it to something like this:
create proc dbo.procfiles
as
BEGIN
if object_id('GlobalShop.dbo.v_order_hist_dtl_Quote','U') is not null
drop table GlobalShop.dbo.v_order_hist_dtl_Quote
if object_id('GlobalShop.dbo.v_order_lines_Quote','U') is not null
drop table GlobalShop.dbo.v_order_lines_Quote
select * into GlobalShop.dbo.v_order_hist_dtl_Quote
from GlobalShop.dbo.v_order_hist_dtl where user_3 is not null
select * into GlobalShop.dbo.v_order_lines_Quote
from GlobalShop.dbo.v_order_lines where user_3 is not null
/* Quotes with Orders 1.0 */
select
QT.QuoteNum,
QT.custID,
QT.QCustNum,
QT.QCustName,
QT.QDate,
COUNT(OL.order_no) as #_Open_Orders,
COUNT(OH.order_no) as #_Closed_Orders
from
"Track Quotes".dbo.TrackQuotesHist as QT
left outer join GlobalShop.dbo.v_order_hist_dtl_quote as OH
on QT.QuoteNum = OH.USER_3
left outer join GlobalShop.dbo.v_order_lines_quote as OL
on QT.QuoteNum = OL.USER_3
where
QT.QDate between '03/01/2015' and '03/06/2015'
Group By
QT.QuoteNum,
QT.custID,
QT.QCustNum,
QT.QCustName,
QT.QDate
begin catch
select
ERROR_NUMBER(),
ERROR_SEVERITY(),
ERROR_STATE(),
ERROR_PROCEDURE(),
ERROR_LINE(),
ERROR_MESSAGE()
end catch
end
Unless you have a very compelling reason to update those tables every time this query is run, I would avoid them. Just use equivalent query without the use of the tables:
select
QT.QuoteNum,
QT.custID,
QT.QCustNum,
QT.QCustName,
QT.QDate,
COUNT(OL.order_no) as #_Open_Orders,
COUNT(OH.order_no) as #_Closed_Orders
from "Track Quotes".dbo.TrackQuotesHist as QT
left outer join GlGlobalShop.dbo.v_order_hist_dtl OH
on QT.QuoteNum = OH.USER_3
and isnull(QT.QuoteNum,'')<>''
left outer join GlobalShop.dbo.v_order_lines OL
on QT.QuoteNum = OL.USER_3
and isnull(QT.QuoteNum,'')<>''
where QT.QDate between '03/01/2015' and '03/06/2015'
Group By
QT.QuoteNum,
QT.custID,
QT.QCustNum,
QT.QCustName,
QT.QDate
Related
I am trying to make a stored procedure like this
BEGIN
SELECT concat(stationsNavn,' ',city,' ',adresse,' ',postnummer)as navn,id
from AdresseListeBioAnlæg order by stationsNavn,city
inner join bioAlarmer on AdresseListeBioAnlæg.id = bioAlarmer.station;
end
With "as navn,id" i can create it, but i dont get anything, and with "order by stationsNavn,city" i get an error on create. It works fine without any of them like this.
BEGIN
SELECT concat(stationsNavn,' ',city,' ',adresse,' ',postnummer)
from AdresseListeBioAnlæg
inner join bioAlarmer on AdresseListeBioAnlæg.id = bioAlarmer.station;
end
What am i doing wrong?
try this out, I moved the order by and did it by navn and id
BEGIN
SELECT concat(stationsNavn,' ',city,' ',adresse,' ',postnummer)as navn,id
from AdresseListeBioAnlæg
inner join bioAlarmer on AdresseListeBioAnlæg.id = bioAlarmer.station
order by navn, id
end
I have a nasty, nasty data layout that I am forced to work with. I finally got a working query using C# and a for loop executing the same query over and over but adjusting which fields are called, but now I am wondering if it is possible to do it with a while loop. I am getting an error, and I am not sure if it is because I am using Faircom / C-tree as the database, or if there is something wrong with my query. I am normally a Mysql user.
the table has 20 fields I care about and want to extract into a csv list. They are codetype1-codetype20 and I want it to be something like value1, value2, value3... where as it is now I get them all back one at a time. Trouble is that codetype1 is dependent on another field to determine where I go look for the info on that code, which is why the case statements.
DROP PROCEDURE IF EXISTS proc_loop_test;
CREATE PROCEDURE proc_loop_test()
SET #index = 1;
WHILE(#index < 21) DO
SELECT Replace(Concat(To_char(apptid), To_char(.#index) ), ' ', '') AS reference_id,
apptid AS a_reference_id,
CASE
WHEN c.ee > 0 THEN d.amt
ELSE insfee.amt
END AS amount,
CASE
WHEN c.ee > 0 THEN Rtrim(e.moneyname)
ELSE insname.namefeecatid
END AS moneyschedule_name,
CASE codetype#index
WHEN 1 THEN rtrim(a.descript)
ELSE rtrim(b.descript0)
END AS description,
CASE codetype#index
WHEN 1 THEN rtrim(a.abbrevdescript)
ELSE rtrim(b.abbrev0)
END AS abbreviated_description,
CASE codetype#index
WHEN 1 THEN rtrim(a.thiscode)
ELSE rtrim(b.thiscode0)
END AS code
FROM meetings
LEFT JOIN
(
SELECT admin.table2.procid,
admin.table2.this_code_id,
admin.v_table1.descript,
admin.v_table1.abbrevdescript,
admin.v_table1.thiscode
FROM admin.table2
INNER JOIN admin.v_table1
ON admin.table2.this_code_id = admin.v_table1.this_code_id) AS a
ON meetings.codeid#index = a.procid
LEFT JOIN
(
SELECT admin.v_table1.descript AS descript0,
admin.v_table1.abbrevdescript AS abbrev0,
admin.v_table1.thiscode AS thiscode0,
admin.v_table1.this_code_id
FROM admin.v_table1) AS b
ON meetings.codeid#index = b.this_code_id
LEFT JOIN
(
SELECT patid AS id,
ee
FROM admin.customer) AS c
ON meetings.patid = c.id
LEFT JOIN
(
SELECT this_code_id AS redid,
eecategoryid,
amt
FROM admin.eeule) AS d
ON c.ee = d.eecategoryid
AND d.redid = b.this_code_id
LEFT JOIN
(
SELECT eecategoryid AS namefeecatid,
moneyname
FROM admin.eeulenames) AS e
ON d.eecategoryid = e.namefeecatid
LEFT JOIN (SELECT pi.customer_id,
pi.primarykk_id AS picid,
pi.primarykk_name,
pi.first_name,
pi.last_name,
i.groupname,
i.ee
FROM admin.v_pir AS pi
LEFT JOIN admin.money AS i
ON pi.primarykk_id = i.insid) AS
ins
ON ins.customer_id = c.id
LEFT JOIN (SELECT this_code_id AS redid,
eecategoryid,
amt
FROM admin.eeule) AS insfee
ON ins.ee = insfee.eecategoryid
AND insfee.redid = b.this_code_id
LEFT JOIN (SELECT eecategoryid AS namefeecatid,
moneyname
FROM admin.eeulenames) AS insname
ON insfee.eecategoryid = insname.namefeecatid
WHERE codeid#index >= 1
END WHILE;
END;
I have never used a while loop, and while I understand somewhat I am supposed to select this to go INTO something, do I need to create a temp table, or can it all just be stored in memory till the end of the loop and returned.
For what it is worth, the entire SELECT query works in C# when you replace the #index with concatenating format " . index . "
Based on the information that you have provided, it is strongly suggested that you reach out to your vendor for this. You're attempting to create a stored procedure, however, using a mySQL proprietary syntax. Stored procedure support is unique to each database vendor. FairCom's c-treeACE SQL actually uses Java for cross platform support and .NET for Windows.
https://docs.faircom.com/doc/jspt/#cover.htm
Stored procedure development requires a strong knowledge of the database layout which is highly application dependent. In this case, many legacy application dependencies may be involved. Again, your best source of information will be your application vendor.
I want to declare a variable called average, what should be the average of the completiondate-creationdate of the items in the todolist.
however, if I use these lines of code, mysql tells me that I have an error in my syntax.
Can somebody tell me what I should change to store the correct value in #average?
SET #average = AVG(todoitem.completiondate-todoitem.creationdate) from todolist
right join todoitem on todoitem.id=todolist.id
;
SELECT name FROM todolist
right join todoitem on todoitem.id=todolist.id
WHERE (todoitem.completiondate-todoitem.creationdate > average)
;
You are missing a select in the first query:
select #average := AVG(todoitem.completiondate - todoitem.creationdate)
from todolist right join
todoitem
on todoitem.id = todolist.id;
Although I'm leaving it in, the join seems unnecessary. Why are you doing the join if you are using only one table?
select #average := AVG(todoitem.completiondate - todoitem.creationdate)
from todoitem;
You have to add SELECT:
SET #average = (SELECT AVG(todoitem.completiondate-todoitem.creationdate))
from todolist
right join todoitem on todoitem.id=todolist.id
-- or
SELECT AVG(todoitem.completiondate-todoitem.creationdate) INTO #average
from todolist
right join todoitem on todoitem.id=todolist.id
i have a stored procedure in mysql with a couple of queries and i need to perform some operations with that query.
This is some the code from the stored procedure:
BEGIN
SET ##session.collation_connection = ##global.collation_connection;
DROP TEMPORARY TABLE IF EXISTS innerContainers;
CREATE TEMPORARY TABLE `innerContainers` (
`id_container` INT(10) NOT NULL,
`display_name` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id_container`)
)
ENGINE = memory;
INSERT INTO innerContainers(id_container, display_name)
(SELECT c1.id_container, c1.display_name
FROM container_presentation cp
LEFT JOIN presentation p USING(id_presentation)
LEFT JOIN container c1 ON p.id_container = c1.id_container
WHERE c1.isDeleted = 0 AND c1.isActive = 1 AND
cp.id_container = in_id_container)
UNION
(SELECT c1.id_container, c1.display_name
FROM container_assembly_item cp
LEFT JOIN presentation p USING(id_presentation)
LEFT JOIN container c1 ON p.id_container = c1.id_container
WHERE c1.isDeleted = 0 AND c1.isActive = 1 AND
cp.id_container = in_id_container);
SELECT mad.id_container,
mat.sign_stock,
ma.id_management_start_point,
ma.id_management_end_point,
mad.quantity
FROM management_activity ma
LEFT JOIN management_activity_type mat ON ma.id_management_activity_type = mat.id_management_activity_type
LEFT JOIN management_activity_detail mad ON ma.id_management_activity = mad.id_management_activity
LEFT JOIN management_stock_point msp ON ma.id_management_end_point = msp.id_management_stock_point
LEFT JOIN management_stock_point msp1 ON ma.id_management_start_point = msp1.id_management_stock_point
WHERE mad.id_container IN (SELECT id_container FROM innerContainers)
ORDER BY mad.id_container ASC;
END
Now, after the last query.. i need to do some operations and return a value for each id_container inside the temporary table depending on the values in the second query.
Something like this:
foreach id_container in the second query i have a resultValue and i need to:
if the sign_stock == 1 and some other conditions then resultValue -= quantity and if sign_stock == 2 and some other conditions then resultValue += quantity.
And the final resultValue after iterating over the id_container lines will be the one i want for that id_container in the temporary table.
I dont know how to do that operation.. can some one help me with that?
Don't create a temporary table unless you need the data after the procedure call. Either way, in order to iterate over the results of a SELECT query, use a CURSOR.
A simple example is provided in the linked manual page.
I am using two joins on the same table for two different select statements. I am wanting to implement an IF function, due to the fact that the table may contain a zero, I am wanting to substitue the zero for the word 'None'
Here is my SQL that works:
SELECT
CONCAT(`payments`.`AssignedTo`," - ",`people2`.`FName`," ",`people2`.`LName`) AS `AssignedTo`,
CONCAT(`payments`.`PersonID`," - ",`people`.`FName`," ",`people`.`LName`) AS `PersonName`
FROM `mb_payments` as `payments`
LEFT JOIN `mb_people` AS `people` ON `people`.`PersonID` = `payments`.`PersonID`
LEFT JOIN `mb_people` AS `people2` ON `people2`.`PersonID` = `payments`.`AssignedTo`
Here is my SQL with the IF function that does not work:
SELECT
IF(AssignedTo IS 0,'None', CONCAT(`payments`.`AssignedTo`," - ",`people2`.`FName`," ",`people2`.`LName`)),
CONCAT(`payments`.`PersonID`," - ",`people`.`FName`," ",`people`.`LName`) AS `PersonName`
FROM `mb_payments` as `payments`
LEFT JOIN `mb_people` AS `people` ON `people`.`PersonID` = `payments`.`PersonID`
LEFT JOIN `mb_people` AS `people2` ON `people2`.`PersonID` = `payments`.`AssignedTo`
You can use CASE statement in this case or if you want to use IF statement then your statement is wrong.
It should be:
SELECT
IF(AssignedTo = 0,'None', CONCAT(`payments`.`AssignedTo`," - ",`people2`.`FName`," ",`people2`.`LName`)) AS Result,
CONCAT(`payments`.`PersonID`," - ",`people`.`FName`," ",`people`.`LName`) AS `PersonName`
FROM `mb_payments` as `payments`
LEFT JOIN `mb_people` AS `people` ON `people`.`PersonID` = `payments`.`PersonID`
LEFT JOIN `mb_people` AS `people2` ON `people2`.`PersonID` = `payments`.`AssignedTo`
Check out this blog: http://timmurphy.org/2009/08/13/inline-if-and-case-statements-in-mysql/
I think you want ".. = 0", not "... IS 0". You use IS when comparing with NULL.