Is there a possibility to filter with IS NULL and another variable?
table1.id | table1.product | table2.product_id | table2.attribute_id
1 | Product 1 | 1 | 1
1 | Product 1 | 1 | NULL
1 | Product 1 | 1 | 3
2 | Product 2 | 2 | NULL
2 | Product 2 | 2 | 2
2 | Product 2 | 2 | 3
3 | Product 3 | 3 | 1
3 | Product 3 | 3 | 2
3 | Product 3 | 3 | NULL
So, I would like to filter products with no attribute_id 1 and 2.
Related
This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 2 years ago.
I have two tables:
Suppliers:
+-------+------+---------------+
| supid | prid | supplier_name |
+-------+------+---------------+
| 2 | 2 | Supplier 1 |
| 3 | 2 | Supplier 2 |
| 4 | 2 | Supplier 3 |
+-------+------+---------------+
Supplier_items:
+-----------+------+-------+--------+------------+
| supitemid | prid | supid | itemid | prod_tcost |
+-----------+------+-------+--------+------------+
| 3 | 2 | 2 | 3 | 6200 |
| 4 | 2 | 2 | 4 | 810 |
| 5 | 2 | 3 | 3 | 5900 |
| 6 | 2 | 3 | 4 | 807 |
| 7 | 2 | 4 | 3 | 6680 |
| 8 | 2 | 4 | 4 | 825 |
+-----------+------+-------+--------+------------+
Please help me to achieve this result getting the min value with supplier_name from Suppliers:
(where prid = 2, group by itemid)
+--------+---------------+----------------+
| itemid | supplier_name | min(prod_tcost) |
+--------+---------------+----------------+
| 3 | Supplier 2 | 5900 |
| 4 | Supplier 2 | 807 |
+--------+---------------+----------------+
You can join and aggregate:
select si.item_id, s.supplier_name, min(si.prod_tcost) min_prod_tcost
from supplier_items si
inner join suppliers s on s.supid = si.supid
where si.item_id in (3, 4) and si.prid = 2
group by si.item_id, s.supid, s.supplier_name
I have a table like this structure:
| event id | item 1 id | item 2 id | set |
| 1 | 1 | 2 | 1 |
| 1 | 1 | 3 | 1 |
| 1 | 2 | 1 | 1 |
| 1 | 2 | 3 | 1 |
| 1 | 3 | 1 | 1 |
| 1 | 3 | 2 | 1 |
| 1 | 2 | 4 | 2 |
| 1 | 4 | 2 | 2 |
| 2 | 1 | 4 | 3 |
| 2 | 1 | 5 | 3 |
| 2 | 4 | 1 | 3 |
| 2 | 4 | 5 | 3 |
| 2 | 5 | 1 | 3 |
| 2 | 5 | 4 | 3 |
now I wish to count the occurrence of item1 as well as item1 combined with item2 separately
I tried following:
with count_item1 AS (
select event_id, item_1_id, count(distinct set) AS c1 from table
group by event_id, item_1_id
), count_item1_and_item2 AS (
select event_id, item_1_id, item_2_id, count(distinct set) AS c2 from table
group by event_id, item_1_id, item_2_id
)
select t1.event_id, t1.item_1_id, t1.item_2_id, t1.c2, t2.c1
from count_item1_and_item2 AS t1
inner join count_item1 AS t2
on t1.event_id=t2.event_id and t1.item_1_id=t2.item_1_id
for example as table above
so the result for this should be:
| event id | item 1 id | item 2 id | c1 | c2 |
| 1 | 1 | 2 | 1 | 1 |
| 1 | 1 | 3 | 1 | 1 |
| 1 | 2 | 1 | 2 | 1 |
| 1 | 2 | 3 | 2 | 1 |
| 1 | 3 | 1 | 1 | 1 |
| 1 | 3 | 2 | 1 | 1 |
| 1 | 2 | 4 | 2 | 1 |
| 1 | 4 | 2 | 1 | 1 |
| 2 | 1 | 4 | 1 | 1 |
| 2 | 1 | 5 | 1 | 1 |
| 2 | 4 | 1 | 1 | 1 |
| 2 | 4 | 5 | 1 | 1 |
| 2 | 5 | 1 | 1 | 1 |
| 2 | 5 | 4 | 1 | 1 |
the meaning of each row is: in event_id, item 1 appeared c1 in distinct set and (item1, item2) appeared c2 in distinct set.
also count of (item1, item2) is equal to (item2, item1)
then I find a strange thing:
the occurrence of item1 itself should not less than the occurrence of item1 combined with item2, but I find that sometimes item1 combined with item2 has more count than count of item1, is there anything I did wrong here? I think my idea is right but I didn't get the result. I have being stacked in here for a weekend.
We have a Mysql table user_documents. This table maps document with a user.
Data Definition of the table
id - primary key of the table
document_id - foreign key to the document table
user_id - foreign key to the user table.
hierarchy_order - order of hierarchy.
Sample table data
| id | document_id | user_id | hierarchy_order |
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 2 |
| 3 | 1 | 3 | 3 |
| 4 | 1 | 4 | 4 |
| 5 | 2 | 1 | 1 |
| 6 | 2 | 3 | 2 |
| 7 | 2 | 2 | 3 |
| 8 | 2 | 4 | 4 |
| 9 | 3 | 2 | 1 |
| 10 | 3 | 1 | 2 |
| 11 | 3 | 3 | 3 |
| 12 | 3 | 4 | 4 |
| 13 | 4 | 1 | 1 |
| 14 | 4 | 3 | 2 |
| 15 | 4 | 4 | 3 |
| 16 | 4 | 2 | 4 |
We have a hierarchy table list the user in a simple hierarchy. This table is changed frequently and order of users gets changed.
We cannot rely on the hierarchy table as it changes frequently. Hence we are logging the document_id with user_id and what hierarchy order that user was.
For the document_id = 1 in the user_documents table, the hierarchy was.
level 1 (top level) - user_id - 1
level 2 - user_id - 2
level 3 - user_id - 3
level 4 - user_id - 4
Problem Statement
We want to select all records/rows for user_id 2 and other users where their hierarchy order is greater than or equal to user_id 2 hierarchy_order for all document_id.
Expected output
| id | document_id | user_id | hierarchy_order |
| 2 | 1 | 2 | 2 |
| 3 | 1 | 3 | 3 |
| 4 | 1 | 4 | 4 |
| 7 | 2 | 2 | 3 |
| 8 | 2 | 4 | 4 |
| 9 | 3 | 2 | 1 |
| 10 | 3 | 1 | 2 |
| 11 | 3 | 3 | 3 |
| 12 | 3 | 4 | 4 |
| 16 | 4 | 2 | 4 |
We are using MySQL Database and not sure how to achieve this.
i want to combine multiple rows into one row.
the table type is dynamic
Table Room
room_id | room_name
-------------------
1 | room_A
2 | room_B
3 | room_C
Table Type
type_id | type_name
-------------------
1 | type_I
2 | type_II
3 | type_II
4 | type_IV
table Price
price_id | room_id | type_id | price
------------------------------------
1 | 1 | 1 | 100
2 | 1 | 2 | 150
3 | 1 | 3 | 200
4 | 1 | 4 | 250
5 | 2 | 1 | 100
6 | 2 | 2 | 200
7 | 2 | 3 | 300
8 | 2 | 4 | 400
9 | 3 | 1 | 150
10 | 3 | 2 | 250
11 | 3 | 3 | 350
12 | 3 | 4 | 450
what i want is something like this
| price
room |---------------------------------------
| type_I | type_II | type_III | type_IV
-----------------------------------------------
room_A | 100 | 150 | 200 | 250
room_B | 100 | 200 | 300 | 400
room_C | 150 | 250 | 350 | 450
Try this Query, Hope this helps you:
SELECT TR.Room_Name
,MAX(CASE WHEN P.Type_ID=1 THEN Price END)type_I
,MAX(CASE WHEN P.Type_ID=2 THEN Price END)type_II
,MAX(CASE WHEN P.Type_ID=3 THEN Price END)type_III
,MAX(CASE WHEN P.Type_ID=4 THEN Price END)type_IV
FROM TABLE_PRICE P
INNER JOIN TABLE_ROOM TR ON TR.Room_ID=P.Room_ID
INNER JOIN TABLE_TYPE TT ON TT.Type_ID=P.Type_ID
GROUP BY TR.Room_Name
I now find my original table structure was not good, so want to change it.
But I am having a hard time designing queries to obtain totals in rows with the new structure.
current structure:
+----------+-------+-------+-------+-------+
| state | shop | item0 | item1 | item2 |
+----------+-------+-------+-------+-------+
| 5 | 0 | 1 | 2 | 3 |
| 5 | 1 | 1 | 2 | 3 |
| 5 | 2 | 1 | 2 | 3 |
| 4 | 3 | 1 | 2 | 3 |
+----------+-------+-------+-------+-------+
(quantities of items at shop)
I want to change to these 2 tables:
shops table
+---------+--------+
| shop_id | state |
+---------+--------+
| 0 | 5 |
| 1 | 5 |
| 2 | 5 |
| 3 | 4 |
+---------+--------+
items table
+------------+--------------+
| shop | item | quantity |
+------------+--------------+
| 0 | 0 | 1 |
| 0 | 1 | 2 |
| 0 | 2 | 3 |
| 1 | 0 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 3 |
| 2 | 0 | 1 |
| 2 | 1 | 2 |
| 2 | 2 | 3 |
| 3 | 0 | 1 |
| 3 | 1 | 2 |
| 3 | 2 | 3 |
+------------+--------------+
The old layout allowed simple queries for getting totals by row:
SELECT state,SUM(item0) t0,SUM(item1) t1,SUM(item2) t2
FROM shops
WHERE state=5
+--------+---------+---------+----------+
| state | t0 | t1 | t2 |
+--------+---------+---------+----------+
| 5 | 3 | 6 | 9 |
+--------+---------+---------+----------+
With the new structure,
I can get the totals in column as follows:
SELECT item,SUM(quantity) total
FROM shops
LEFT JOIN items ON shop=shopid
WHERE state=5
GROUP by item
+--------+---------+
| item | total |
+--------+---------+
| 0 | 3 |
+--------+---------+
| 1 | 6 |
+--------+---------+
| 2 | 9 |
+--------+---------+
but how do I get the totals in rows:
+--------+---------+---------+----------+
| state | t0 | t1 | t2 |
+--------+---------+---------+----------+
| 4 | 1 | 2 | 3 |
| 5 | 3 | 6 | 9 |
+--------+---------+---------+----------+
You might try using a few more JOINs:
SELECT S.state,
SUM(T0.quantity) AS "T0",
SUM(T1.quantity) AS "T1",
SUM(T2.quantity) AS "T2"
FROM shops AS S
LEFT JOIN items AS T0 ON S.shop_id = T0.shop_id AND T0.item=0
LEFT JOIN items AS T1 ON S.shop_id = T1.shop_id AND T1.item=1
LEFT JOIN items AS T2 ON S.shop_id = T2.shop_id AND T2.item=2
GROUP BY S.state
There might be an easier way.