Mysql order column and not null - mysql

My sql table looks like
ID C1 R1 R2
1 1 1 1
2 1 1 NULL
3 1 1 2
4 1 1 3
5 1 2 NULL
6 1 2 4
I want to retrieve the rows with C1 ID of 1 and after that sort by R1 ascending and first get the NULL value and after that ascending R2.
So my results would be:
ID C1 R1 R2
1 1 1 NULL
2 1 1 1
3 1 1 2
4 1 1 3
5 1 2 NULL
6 1 2 4
Select * from Table t where t.C1 = 1 ......
How can I make my sql query to do what I want?

Below query will work:
select * from table_name where C1=1 order by R1, R2 ASC

select * from table1 where c1=1 order by r1, r2

Related

sql or python solution for getting merging duplicate rows into one in an ordered table and reordering them

Can someone help me do this in SQL in a select statement?
I have a table xyz as follow:
ColumnID
Column A
Column B
1
1
A
1
2
B
1
3
C
1
4
D
2
1
A
2
2
B
2
3
C
2
4
C
3
1
A
3
2
A
3
3
B
3
4
B
4
1
A
4
2
B
4
3
V
4
4
V
I want it to change to this:
Column A
Column B
1
A
2
B
3
C
4
D
1
A
2
B
3
C
1
A
2
B
1
A
2
B
3
V
haven't tried anything
Here is the solution for anyone who has the same question:
with abc as (select columnID, ROW_NUMBER() over (PARTITION by columnID, column_b ORDER BY columnID) as column_a, column_b
from xyz)
select row_number() over (partition by columnID order by column_a asc) as column_a, column_b
from abc where row_num = 1

How to write a sql to generate same id column when any of three columns is the same?

We have a table T1 like below:
col1 col2 col3
1 1 1
1 2 1
1 2 3
4 4 4
We want to generate one more column, and then new table T2 is like below:
col1 col2 col3 id
1 1 1 1
1 2 1 1
1 2 3 1
4 4 4 2
The first three rows has at lease one same value in col1/col2/col3, so they have the same id 1. For example, row1 and row3 have same value 1 in col1, so they have the same id. The forth row don't have any same value with the first three row in col1/col2/col3, so it have a new id 2.
To be more percise, when T1 have one more column (4,2,4) like below, all rows have same id 1.
col1 col2 col3
1 1 1
1 2 1
1 2 3
4 4 4
4 2 4
My Idea:
1、We can join table T1 with itself to eliminate different rows
select * from T1 t11 join T1 t12 on t11.col1 = t12.col1 or t11.col2 = t12.col2 or t11.col3 = t12.col3.
t11.col1 t11.col2 t11.col3 t12.col1 t12.col2 t12.col3
1 1 1 1 1 1
1 1 1 1 2 1
1 1 1 1 2 3
1 2 1 1 1 1
1 2 1 1 2 1
1 2 1 1 2 3
1 2 3 1 1 1
1 2 3 1 2 1
1 2 3 1 2 3
4 4 4 4 4 4
2、Maybe we can distinct or group by the result above, but I don't kown how to do?
Can somebody help me out with this?
First of all, your table is lacking a fourth column which provides the suggested ordering of the records. For the purpose of this answer, I will assume that there exists an id column as follows:
id | col1 | col2 | col3
1 | 1 | 1 | 1
2 | 1 | 2 | 1
3 | 1 | 2 | 3
4 | 4 | 4 | 4
We can use analytic functions here:
WITH cte AS (
SELECT *, CASE WHEN col1 = col2 AND col2 = col3 AND col1 = col3
THEN 1 ELSE 0 END AS val
FROM yourTable
)
SELECT col1, col2, col3, SUM(val) OVER (ORDER BY id) AS new_col
FROM cte
ORDER BY id;
Demo

Merging Multiple Queries - Same Table

I have 1 table called itemmovement : It has Item Id , Quantity In , Quantity Out , Invoice Id, Date. I need to make in one query to show how many pieces are sold and beside the sold column there will be the current on hand quantity .
itemmovement
Id itemid qtyin qtyout invid purchasereturnid date
1 1 10 2019-01-04
2 2 8 2019-01-06
3 2 2 1 2019-01-08
4 1 3 2 2019-01-12
5 2 1 2019-02-04
6 3 4 2019-03-04
7 1 1 3 2019-04-04
8 1 1 1 2019-04-14
9 3 1 2 2019-04-24
I need the query to show this result
Id itemid Sold Quantity OnHandQty
1 1 4 5
2 2 2 7
3 3 0 3
I'm Trying to use this query but not working
SELECT *
FROM
(SELECT itmv.itemid,
sum(itmv.qtyout)-sum(itmv.qtyin)
FROM itemmovement itmv
WHERE (itmv.systemdate BETWEEN '2019-01-01' AND '2019-06-01')
AND invid>0
GROUP BY itmv.itemid) AS result1,
(SELECT sum(itmv2.qtyin)-sum(itmv2.qtyout)
FROM itemmovement itmv2
WHERE itmv.itemid=itmv2.itemid
GROUP BY itmv2.itemid) AS result2
ORDER BY sum(itmv.qtyin)-sum(itmv.qtyout)
I'm getting :
Unknown column 'itmv.itemid' in 'where clause it for this syntax :
where itmv.itemid = itmv2.itemid
Here's your query.
select itemid
, sum(case when COALESCE(invid,0) > 0 then qtyout else 0 end) as Sold_Qantity
, sum(qtyin)-sum(qtyout) as OnHandQty
from itemmovement
group by itemid

How do I fetch and process 2 records then consecutive pair?

I need to proces records one by one comparing each to its prior then move to next and do the same until last record.
Structure to proces
id dat qty dif
1 2019-05-01 2 NULL
2 2019-05-01 6 NULL
3 2019-05-01 3 NULL
1 2019-05-02 4 NULL
2 2019-05-02 7 NULL
3 2019-05-02 5 NULL
Expected result
id dat qty dif
1 2019-05-01 2 0
1 2019-05-02 4 2
2 2019-05-01 6 0
2 2019-05-02 7 1
3 2019-05-01 3 0
3 2019-05-02 5 2
For id =1 and dat= '2019-05-01' dif = (2 - 0) which is current qty minus prior qty
For id =1 and dat= '2019-05-02' dif = (4 - 2)
Do I need scrollable cursor ? How to get it ?
Since SQL Server 2008 does not support LAG, we can try simulating it using a correlated subquery:
SELECT
id,
dat,
qty,
qty - COALESCE((SELECT TOP 1 t2.qty FROM yourTable t2
WHERE t2.id = t1.id AND t2.dat < t1.dat
ORDER BY t2.dat DESC), t1.qty) AS dif
FROM yourTable t1
ORDER BY
id, dat;
Demo

MySQL SUM Query

Greetings, I've got a query that I'm struggling with, this is the first time that I am encountering this type of query.
I have two table as shown below.
xid is the primary key in parent_tbl1, while xid is the foreign key in child_tbl2
parent_tbl1
xid pub
1 1
2 1
3 0
4 1
child_tbl2
id ttype fno xid qnty
1 A 0 1 0
2 A 1 1 3
3 B 1 1 4
4 A 1 2 1
5 A 1 3 2
6 A 1 4 3
7 A 1 4 1
8 A 1 1 1
Below is the exlanation of the query in parts, which will then need to make up the whole query.
I need the SUM of qnty in child_tbl2:
1) Who's parent's pub is '1'
Therefore, id 5 is eliminated from child_tbl2, this is because xid 3 is 0 in parent_tbl1
Results:
child_tbl2
id ttype fno xid qnty
1 A 0 1 0
2 A 1 1 3
3 B 1 1 4
4 A 1 2 1
6 A 1 4 3
7 A 1 4 1
8 A 1 1 1
2) AND who's parent table has ttype 'A' in the child table
Therefore, id 3 is eliminated from the existing results because id 3's ttype is B
Results:
child_tbl2
id ttype fno xid qnty
1 A 0 1 0
2 A 1 1 3
4 A 1 2 1
6 A 1 4 3
7 A 1 4 1
8 A 1 1 1
3) AND who's parent has '0' as one it's fno's in the child_tbl2
Therefore, id 4, 6 & 7 are eliminated from the existing results, this is because 0 was not found in one of their fno's, while 0 was found as one of xid 1's fno
Results:
child_tbl2
id ttype fno xid qnty
1 A 0 1 0
2 A 1 1 3
8 A 1 1 1
The answer for the query should be 4
Below is what i've got.
SELECT sum(child_tbl2.qnty), parent_tbl1.xid, parent_tbl1.pub, child_tbl2.ttype, child_tbl2.fno, child_tbl2.xid
FROM parent_tbl1, child_tbl2
WHERE parent_tbl1.xid = child_tbl2.xid
AND parent_tbl1.pub = '1'
AND child_tbl2.ttype = 'A'
AND child_tbl2.fno ?
If it is possible, I do not know how to tell the dbms (MySQL) to check if Zero is one of the fno's.
If I say "AND child_tbl2.fno = '0'", I will be saying that the result's fno should be 0. I do not want that, I need zero to be one of the fno's in order for the query to SUM all the qnty in that particular xid
SELECT SUM(DISTINCT src.qnty) as qnty
FROM tbl2 AS src
INNER JOIN tbl1 AS pub
ON src.xid=pub.xid
INNER JOIN tbl2 AS fno
ON pub.xid=fno.xid
WHERE pub.pub=1
AND src.ttype='A'
AND fno.fno=0