Mysql condition on WHERE clause - mysql

I m using mysql database using php to fetch data. I have distributor column, i m using WHERE clause like WHERE d_id = 1 . i have 1 to 4 distributors. On front end i have HTML select option in which if i select any distributor it shows it's data but i added an option value="0" as Total . Now if i select Total it should show all data, actually where clause should not work then. (show all distributors data)
Dist: Product Sales
1. dis_a abc 100
2. dis_b abc 50
3. dis_c cde 10
4. dis_c cde 10

Here is an example of a select that should do roughly what you want.
select d.[disc:], d.[Product], d.[Sales]
from your_table d
where d.your_table_seq = case when #dist_id = 0 then d.your_table_seq else #dist_id end
The case block will allow you pull every row when the #dist_id parameter is equal to 0, but will only pull id's that are equal to #dist_id when #dist_id is not equal to 0.

Related

mySQL Sum Production_Needed Group BY Part_ID

Want to generate a result of Open orders where Production is needed. At issue is each part may have more than one open order. With the GROUP BY my code gives me only one order but does give me the total Production_Needed (which is also a negative in value for orders with enough inventory).
Does my SUM(...) as Production_Needed need to be in the WHERE ?
Thanks,
SELECT part.part_ID AS Part_Part_ID,
part.Inventory, part.part_number,
ord.part_id AS Order_Part_ID,
ord.order_type, ord.quantity_ordered, ord.quantity_shipped,
SUM(ord.quantity_ordered - ord.quantity_shipped - part.Inventory) AS Production_Needed
FROM production_orders ord
JOIN production_part part ON ord.part_ID = part.part_ID
WHERE ord.is_Active = True AND ord.order_type = 0
GROUP BY Order_Part_ID
ORDER BY part.part_number ASC
Data Production_Part part
Part_ID
Part_Inventory
Part_Number
1
12500
97-528
2
0
FC2569
3
1000
39367
Data Production_Orders Ord
Order_Part_ID
Order_Type
Quantity_Ordered
Quantity_Shipped
1
0
8000
0
2
0
1000
500
2
0
1000
0
3
1
10
0
Desired Result - Only Parts that need production
Part_ID
Quantity_Ordered
Quantity_Shipped
2
1000
500
2
1000
0
Untested: need a sampled data set and structure for testing:
This creates an inline view and totals the inventory order amounts then stubtracts it from the inventory to determine if there is a production needed to fulfil open orders. I'd have to use some additional analytical functions if we needed to do this on an order by order basis however; or join these results back into the orders...
--Show parts which lack inventory to fullfill outstanding open orders.
SELECT
P.Part_ID as Part_Part_ID
, P.Inventory
, P.Part_Number
, O.Part_ID as Order_Part_ID
, UnDel_Units-coalesce(P.Inventory,0) as Production_Needed --use coalesce incase no part record exists for some reason.
FROM Production_Part P
RIGHT JOIN ( --use right join just incase part record doesn't exist for some reason
SELECT part_ID, SUM(quantity_ordered-quantity_shipped) as UnDel_Units
FROM PRODUCTION_ORDERS
WHERE IS_ACTIVE=TRUE
and ORDER_TYPE=0
GROUP BY PART_ID) O --derived table "O" for orders showing sum ottal by part of units undelivered
on O.Part_ID=P.Part_ID
WHERE UnDel_Units > coalesce(P.Inventory,0)
-- If inventory is > undelivered units for the part, ignore as additional production isn't needed

Ignore columns in MySQL query result with null values

I have a MySql table as,
Name Month Salary
=======================================
A Salary_Month_Sept 15000
A Salary_Month_Oct 0
B Salary_Month_Sept 12000
B Salary_Month_Oct 0
C Salary_Month_Sept 13000
C Salary_Month_Oct 0
and I am querying that table as
select Name,
max(IF(Month = 'Salary_Month_Sept', Salary, 0)) AS 'Salary_Month_Sept',
max(IF(Month = 'Salary_Month_Oct', Salary, 0)) AS 'Salary_Month_Oct'
from myTable
Which returns the query result as
Name Salary_Month_Sept Salary_Month_Oct
=============================================
A 15000 0
B 12000 0
C 17000 0
How can i ignore the column containing only zero or null values from the above query result.
Don't use *. Name columns you want to have. The query is not a crystal ball. It doesn't know in front if there will be data for the column. To do something like that you need 2 queries, assuming the salaries are only positive:
Select sum(salary_sept), sum(salary_oct), ... for the condition you need.
Create second select only for columns returning sum bigger than zero.
The SQL has no time machine, sorry. You have to do your work yourself.

MySQL - return records for clients unless they have a specific value and only that value

Trying to wrap my head around how to do this query - I want to return a list of client records and need to exclude clients if they had only a specific value and no other values.
For example
c# value
1 X
1 Y
2 X
3 Y
I want all the records for clients 1 and 3, since they had a value other than X. I do not want client 2, because that client had ONLY X.
I for example want returned in this case:
1 X
1 Y
3 Y
Of course, I could have lots of other records with other client id's and values, but I want to eliminate only the ones that have a single "X" value and no other values.
Maybe using a sub-query?
Try this:
SELECT client, value FROM myTable where `client` in
(select distinct(client) from myTable where value !='X');
Returns:
Client Value
1 X
1 Y
3 Y
Something like this
SELECT ABB2.*
FROM
mytable AS ABB2
JOIN
(SELECT c
FROM mytable
WHERE value <> "X"
GROUP BY c) AS ABB1 ON ABB1.c = ABB2.c
GROUP BY ABB2.c, ABB2.value
It's faster than using a WHERE clause to identify the sub query results (as in Mike's answer)

UPDATE using previously updated value

I'm trying to update records using previously updated value in a single query run. It's a running total case, but it's an UPDATE query not a SELECT query.
Table Inventory (Simplified)
Id Qty RunningQty ItShouldBeUpdatedTo InsteadItsUpdatedTo
1 200 0 200 200
2 300 0 500 300
3 400 0 900 400
4 100 0 1000 100
My current query is something like this
UPDATE Inventory
SET RunningQty = ISNULL(A.RunningQuantity, 0) + Quantity
FROM Inventory I
OUTER APPLY
(
-- Take previous row RunningQty
SELECT TOP 1 RunningQty
FROM Inventory
WHERE Id < I.Id
ORDER BY Id DESC
) A
It seems to update next row, sql server not using previously updated value.
Note: It will be quite large table so i calculate it using previous row running quantity and add the value with current row quantity, instead of calculating it from the first row.
What's the correct way to do this?
Thanks in advance.
Let me preface this by saying, this will be significantly easier to do in SQL Server 2012. In that version you will be able to use something like this:
update i1
set i1.runningqty = i2.RunningQty
from inventory i1
inner join
(
select id, SUM(qty) OVER(ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) RunningQty
from inventory
) i2
on i1.id = i2.id;
See SQL Fiddle with Demo
But it is not as easy in SQL Server 2008, however you should be able to use something like this:
declare #total int
set #total = 0
update inventory
set runningqty = #total,
#total = #total + qty;
See SQL Fiddle with Demo.
There are other methods including using a cursor to perform this in sql server 2008.

SQL statement with formula

I am trying to write an SQL statement that will calculate the total value of a purchase order.
Example time!
SELECT PO_Number, LineItem, Quantity, Cost
FROM POs
Say on PO_Number=484 there are 2 units of LineItem-1 at $2 each. There are also 3 units of LineItem-2 at $5 each. Is there a way to output $19? (2 + 2 + 3 + 3 + 3 = 19)
SELECT SUM(Quantity*Cost) WHERE PO_Number = 484;
UPDATE
If you want to show the totals for multiple purchase orders, you need to "group" your results by purchase order:
SELECT SUM(Quantity*Cost)
WHERE PO_Number IN (484,485,486) -- if you want specified ones only, omit for all
GROUP BY PO_Number;
Or...
SELECT PO_Number, SUM(Quantity*Cost)
GROUP BY PO_Number;