I am trying to perform this query inside a procedure
set mailSeq = (
select MailSequence
from MailIdentity
where Customer = customer
and MailDate = mailDate
);
i have 4 records in this table
Id Customer MailDate MailSequence
1 1 2014-04-05 00:00:00 5
2 1 2014-04-06 00:00:00 2
3 2 2014-04-05 00:00:00 9
4 2 2014-04-06 00:00:00 5
i call the procedure as
call my_procedure(2, '2014-04-05')
the parameters are customer and mail date
if I perform the query outside the procedure it returns a single record, the one i want, but when i call the procedure it returns the 4 records.
Can any help with this?
I had the same problem and after reading this I realized what it was. Don't have you input parameter be the same name as the fieldname in your where clause.
I also got this problem,and I found that the parameter after where:
like where Customer = customer
and MailDate = mailDate
the left and right side is the same ,so where is true forever
Related
The below query is a sample query. I am trying to get an idea from u guys. I wrote a query which return those results which has found in table but i used many rolls in where clause list, but all rolls are not found in database.
SELECT roll,name,phone FROM my_tbl where roll in (1,2,3,4,5,6)
This query gives the result like
Roll Name Phone
2 name1 123
3 name2 345
6 name3 785
That query returns only those result which is stored into the table.
But I want to get all of rolls which is used in clause (1,2,3,4,5,6).
My expecting result should be like
Roll Name Phone
1 n/a 0
2 name1 123
3 name2 345
4 n/a 0
5 n/a 0
6 name3 785
Is it possible ? If possible then how ?
You need a derived table with the values:
select *
from (select 1 as roll union all
select 2 as roll union all
. . .
select 6 as roll
) r left join
my_tbl t
using (roll);
This fills in the values for the unknown rolls with NULL, which seems like a suitable values.
I want to have the sum of the beginning inventory of the entire year. The beginning inventory is based of the end_inventory of another month. The beginning_inventory_id contains the ID of another row which points to the end_inventory. How do I properly get the sum of the beginning_inventory of a certain year when it's based of another row's end_inventory. I have the following table
id
time_period
beginning_inventory_id
end_inventory
gross_sales
1
2020-09-01
null
1000
500
2
2020-10-01
1
2000
500
3
2020-11-01
2
3000
500
4
2020-12-01
3
4000
500
5
2021-01-01
4
5000
500
I have the following SQL query
SELECT SUM(a.gross_sales) as gross_sales, SUM(a.end_inventory) as end_inventory,
(SELECT SUM(b.end_inventory) FROM fs_summary as b WHERE a.beginning_inventory_id = b.id) as beginning_inventory
FROM fs_summary as a
WHERE YEAR(a.time_period) = 2020
Output I would like to generate is:
beginning_inventory = 6000
end_inventory = 10000
gross_sales = 2000
Instead, I am getting null on the beginning_inventory.
Any help would be great!
I am Assuming that you want to retrieve data from 1 table with self join.
SELECT SUM(a.gross_sales),SUM(a.end_inventory),SUM(b.end_inventory)
FROM fs_summary a, fs_aummary b
WHERE b.id=a.beginning_inventory_id AND YEAR(a.time_period) = 2020
using self join can help you in this situation
EDIT: You can also write this script as,
SELECT SUM(a.gross_sales),SUM(a.end_inventory),SUM(b.end_inventory)
FROM fs_summary a
INNER JOIN fs_aummary b
ON b.id=a.beginning_inventory_id
WHERE YEAR(a.time_period) = 2020
Using self-join SQL you can achieve your result instead of sub-queries.
You should specify the same table with two different names. Your query looks as below
select sum(virtual_tb.end_inventory) as 'beginning_inventory', sum(org_tb.end_inventory) as 'end_inventory', sum(org_tb.gross_sales) as 'gross_sales'
from fs_summary org_tb left join fs_aummary virtual_tb on (virtual_tb.beginning_inventory_id = org_tb.id)
where year(org_tb.time_period) = 2020;
(Approx Output)
beginning_inventory
end_inventory
gross_sales
6000
10000
2000
I am currently working on a.Net web form solution which generates a brief service report for admins to monitor the services done by technicians.As of now , i am having some trouble in coming up with an efficient SQL (for MySQl) which return data rows along with the missing rows based on the SertvicePrtNum , which is in order.
For Example :-
This is my raw data in the table :-
Id ServiceRptNum Customer_ID Date of Service
---- ------------- ----------- ---------------
1 1001 3 09/10/1997
2 1003 8 10/06/2005
3 1005 1 21/02/2003
4 1007 7 1/06/2011
5 1010 4 4/11/2012
6 1002 2 16/01/2003
Here the ServiceRptNum , 1004 is missing in the table. So i want the db to return the result as : -
Id ServiceRptNum Customer_ID Date of Service
---- ------------- ----------- ---------------
1 1001 3 09/10/1997
2 1002 2 16/01/2003
3 1003 8 10/06/2005
- 1004 - -
4 1005 1 21/02/2003
- 1006 - -
5 1007 7 1/06/2011
- 1008 - -
- 1009 - -
6 1010 4 4/11/2012
Here , the sql additionally generated 1004,1006,1008,1009 since it cannot find those records.
Please note that the Id is automatically generated (auto_increment)while insert of the data.But the Service ReportNum is not , this is to enable the admin to add the service report later on with the manually generated report Num (report num in the hardcopy of the company Servicebook).
You basically need to invent a constant, sequential stream of numbers and then left join your real data to them. For this method to work, you need a table with enough rows in it to generate a counter big enough:
select ID, 1000+n as servicerptnum, customer_id, `Date of Service` from
(
SELECT #curRow := #curRow + 1 AS n
FROM somebigtable
JOIN (SELECT #curRow := 0) r
WHERE #curRow<100
) numbergen
LEFT JOIN
tablewithmissingservicerptnum
ON
servicerptnum = 1000+n
You need to alter some things in the code above because you never told us the name of your table with missing rptnums. You also need to utilise another table in your database with more rows than this table because the way this method works is to count the rows in the bigger table, giving each a number. If you don't have any table bigger than this one, we can probably get enough rows by cross joining a smaller table to itself or by using this table. Replace somebigtable with thistable CROSS JOIN thistable where this table is the name of the table with missing servicerptnums
If you want just the rows that are missing, add a WHERE servicerptnum is null to the end of the sql
Edit, I see you've changed your numbering from:
1001
1002
...
1009
10010
To:
1009
1010
The join condition used to be servicerptnum = concat('100', cast(n as varchar)), it is now servicerptnum = 1000+n..
Look here for ideas on how to generate a group of continuous integers, then select from that left outer join your table. You should get a row for every number but all the values will be null for the missing numbers.
I have one stored procedure with one in parameter that returns multiple rows with 3 different columns. I want this result to be inserted inside another table with these 3 column filed and other tables individual fields.
However, when I create the temporary table it shows a blank resultset.
Can anybody help me to sort out this?
CREATE DEFINER=`root`#`localhost` PROCEDURE `getContactRolePermission`(
in contactroleid double
-- inout PermissionTableID double
)
BEGIN
select -- entitydetails.id,
contact.id as userid,
-- contacttyperolemap.contacttypeId ,
contacttyperolemap.roleid ,rolepermission.permissionid
from contacttyperolemap
join rolepermission on contacttyperolemap.roleid=rolepermission.roleid
join entitydetails on entitydetails.id = contacttyperolemap.ContactTypeID
join contact on contact.contactTypeID = entitydetails.id
where contacttyperolemap.contactTypeID = contactroleid
order by contact.id;
CREATE temporary TABLE tmp(
ID double ,
userid double ,
roleid double ,
permissionid double
);
END
stored procedures result is
uid rid pid
2 1 5
2 1 2
2 1 3
2 1 4
2 1 1
23 1 4
23 1 1
23 1 5
23 1 2
23 1 3
26 1 4
26 1 1
26 1 5
26 1 2
26 1 3
I want to insert this data inside userpermission table
schema is
id uid rid pid
1 10 2 1
Looks like you need to create a view instead of a stored procedure for the first 3 rows. Then in a separate query you can query any columns from that view and join that data with whatever other data you want from other tables. Hopefully I read the question right. Heres an article on creating views: https://msdn.microsoft.com/en-us/library/ms187956.aspx
Use INSERT ... SELECT command to fill target table.
And, your temp table shows empty result, because you have not filled it.
At first I would like greet all Users and apologize for my english :).
I'm new user on this forum.
I have a question about MySQL queries.
I have table Items with let say 2 columns for example itemsID and ItemsQty.
itemsID ItemsQty
11 2
12 3
13 3
15 5
16 1
I need select itemsID but duplicated as many times as indicated in column ItemsQty.
itemsID ItemsQty
11 2
11 2
12 3
12 3
12 3
13 3
13 3
13 3
15 5
15 5
15 5
15 5
15 5
16 1
I tried that query:
SELECT items.itemsID, items.itemsQty
FROM base.items
LEFT OUTER JOIN
(
SELECT items.itemsQty AS Qty FROM base.items
) AS Numbers ON items.itemsQty <=Numbers.Qty
ORDER BY items.itemsID;
but it doesn't work correctly.
Thanks in advance for help.
SQL answer - Option 1
You need another table called numbers with the numbers 1 up to the maximum for ItemsQuantity
Table: NUMBERS
1
2
3
4
5
......
max number for ItemsQuantity
Then the following SELECT statement will work
SELECT ItemsID, ItemsQty
FROM originaltable
JOIN numbers
ON originaltable.ItemsQty >= numbers.number
ORDER BY ItemsID, number
See this fiddle -> you should always set-up a fiddle like this when you can - it makes everyone's life easier!!!
code answer - option 2
MySQL probably won't do what you want 'cleanly' without a second table (although some clever person might know how)
What is wrong with doing it with script?
Just run a SELECT itemsID, ItemsQty FROM table
Then when looping through the result just do (pseudo code as no language specified)
newArray = array(); // new array
While Rows Returned from database{ //loop all rows returned
loop number of times in column 'ItemsQty'{
newArray -> add 'ItemsID'
}
}//end of while loop
This will give you a new array
0 => 11
1 => 11
2 => 12
3 => 12
4 => 12
5 => 13
etc.
Select DISTINCT items.itemsID, items.itemsQty From base.items left outer join (select items.itemsQty as Qty from base.items) As Numbers On items.itemsQty <=Numbers.Qty
order by items.itemsID;
Use DISTINCT to remove duplicates. Read more here - http://dev.mysql.com/doc/refman/5.0/en/select.html
It seems like I understood what you asked differently than everyone else so I hope I answer you question. What I would basically do is -
create a new table for those changes.
Create a mysql procedure which given a line in the original table add new lines to the new table - http://dev.mysql.com/doc/refman/5.6/en/loop.html
Run this procedure for each line in the original table.
try this to get distinct values from both columns
SELECT DISTINCT itemsID FROM items
UNION
SELECT DISTINCT itemsQty FROM items