I would like to create a new query for know how many product are in the store.
table: tb_store
+--------+------------+------------------+-----------+
| item_id| nome | data_out | data_in |
+--------+------------+------------------+-----------+
| 1 | Produ1 | null | 2015-01-06|
| 2 | Produ1 | null | 2015-01-06|
| 3 | Produ3 | null | 2015-01-06|
| 4 | Produ3 | null | 2015-01-06|
| 5 | Produ5 | null | 2015-01-06|
| 6 | Produ4 | 2015-01-06 | 2015-02-06|
| 7 | Produ2 | 2015-01-06 | 2015-02-06|
+--------+------------+------------------+-----------+
table: tb_product
+--------+------------+
| item_id| nome |
+--------+------------+
| 1 | Produ1 |
| 2 | Produ2 |
| 3 | Produ3 |
| 4 | Produ4 |
| 5 | Produ5 |
+--------+------------+
i have write this query:
select nome, count(nome) as pezzi from tb_store where data_out is null or data_out="" group by nome order by pezzi desc
the result are:
+--------+------------+
| nome | pezzi |
+--------+------------+
| Produ1 | 2 |
| Produ3 | 2 |
| Produ5 | 1 |
+--------+------------+
i would like to obtain this result:
+--------+------------+
| nome | pezzi |
+--------+------------+
| Produ1 | 2 |
| Produ3 | 2 |
| Produ5 | 1 |
| Produ2 | 0 |
| Produ4 | 0 |
+--------+------------+
is possible? how can rewrite the query?
EDIT
I have create a new query like this:
SELECT DISTINCT nome, pezzi FROM(
SELECT nome,COALESCE(pezzi,0)as pezzi FROM(
SELECT p.nome, COUNT(s.nome) as pezzi
FROM tb_product as p LEFT JOIN tb_store as s
ON p.nome = s.nome
WHERE s.data_out is null
OR s.data_out = ""
GROUP BY p.nome
union
select nome,null as pezzi from tb_product) )
ORDER BY pezzi DESC
but i have some duplicate item to remove....the result is
nome pezzi
crema 2
pistacchio 2
zabajone 2
bacio 1
cassata 1
cioccolato 1
ciocco rum 1
malaga 1
mango 1
mascarpone 1
nocciola 1
stracciatella 1
bacio 0
caramello 0
cassata 0
cioccolato 0
ciocco rum 0
crema 0
fragola 0
limone 0
malaga 0
mango 0
mascarpone 0
nocciola 0
pistacchio 0
zabajone 0
is possible to remove the duplicate that are 0?
SELECT DISTINCT nome, pezzi FROM(
SELECT nome,COALESCE(pezzi,0)as pezzi FROM(
SELECT p.nome, COUNT(s.nome) as pezzi
FROM tb_product as p LEFT JOIN tb_store as s
ON p.nome = s.nome
WHERE s.data_out is null
OR s.data_out = ""
GROUP BY p.nome
union
select nome,null as pezzi from tb_product) GROUP BY nome )
ORDER BY pezzi DESC
You need to do a left join between the two tables like:
SELECT p.nome, COUNT(s.nome) as pezzi
FROM tb_product as p LEFT JOIN tb_store as s
ON p.name = s.name
WHERE data_out is null
OR data_out = ""
GROUP BY p.nome
ORDER BY pezzi DESC
Related
I have the below tables:
CATEGORIES:
id | name | category_group | cate_type_id
1 | Entertainment | Entertainment | 1
2 | Electricity | Utilities | 8
3 | Water | Utilities | 8
4 | Rent | Living Exp | 6
5 | credit card | Finance | 5
BUDGET-ITEMS:
id | budget_id | cat_id | category_group| budget_yr | budget_01 | budget_02 | ... | budget_12
1 | 1 | 1 | Entertainment | 2022 | 500 | |
2 | 1 | 2 | Utilities | 2022 | 1500 | |
3 | 1 | 3 | Utilities | 2022 | | 250 |
I want to pull all items from Category table with mapping budget columns. Below is my JOIN.
SELECT c.id as base_id,c.name,c.category_type_id, c.category_group as base_group, b.*
FROM category c
LEFT JOIN budget_items b ON c.id = b.category_id
WHERE c.category_type_id NOT IN (5)
ORDER BY c.category_type_id, c.category_group ASC
I expect the below output:
id | budget_id | cat_id | catgroup | budget_yr | budget_01 | budget_02 | ... | budget_12
1 | 1 | 1 | Entertainment | 2022 | 500 | |
2 | 1 | 2 | Utilities | 2022 | 1500 | |
3 | 1 | 3 | Utilities | 2022 | | 250 |
4 | 1 | 4 | Living Exp | 2022 | | |
However, I get like below (truncated base* columns here for space):
id | budget_id | cat_id | catgroup | budget_yr | budget_01 | budget_02 | ... | budget_12
1 | 1 | 1 | Entertainment | 2022 | 500 | |
2 | 1 | 2 | Utilities | 2022 | | 1500 |
3 | 1 | 3 | Utilities | 2022 | | |
4 | 1 | 4 | Living Exp | 2022 | | |
My query looks OK, not sure where it is going wrong. Does anyone see the issue?
Thanks in advance for your kind help.
Edit:
I have truncated some columns for space here. the problem is the values are aligned to different budget columns. I get the columns correctly from the left table.
Edit:
Thanks to everyone who pitched in to help, I finally figured the issue was with my data. The query was actually working fine. This community is amazing.
Not sure what you're doing, but it seems to work as expected for me:
Schema (SQLite v3.30)
CREATE TABLE items (
`id` INTEGER,
`budget_id` INTEGER,
`cat_id` INTEGER,
`catgroup` VARCHAR(13),
`budget_yr` INTEGER,
`budget_01` INTEGER,
`budget_02` INTEGER
);
INSERT INTO items
(`id`, `budget_id`, `cat_id`, `catgroup`, `budget_yr`, `budget_01`, `budget_02`)
VALUES
('1', '1', '1', 'Entertainment', '2022', '500', null),
('2', '1', '2', 'Utilities', '2022', '1500', null),
('3', '1', '3', 'Utilities', '2022', null, '250');
CREATE TABLE cats (
`id` INTEGER,
`name` VARCHAR(13),
`category_group` VARCHAR(13),
`cate_type_id` INTEGER
);
INSERT INTO cats
(`id`, `name`, `category_group`, `cate_type_id`)
VALUES
('1', 'Entertainment', 'Entertainment', '1'),
('2', 'Electricity', 'Utilities', '8'),
('3', 'Water', 'Utilities', '8'),
('4', 'Rent', 'Living Exp', '6'),
('5', 'credit card', 'Finance', '5');
Query
SELECT c.id
, budget_id
, cat_id
, catgroup
, budget_yr
, budget_01
, budget_02
FROM cats c
LEFT JOIN items i ON c.id = i.cat_id
WHERE c.cate_type_id <> 5;
id
budget_id
cat_id
catgroup
budget_yr
budget_01
budget_02
1
1
1
Entertainment
2022
500
2
1
2
Utilities
2022
1500
3
1
3
Utilities
2022
250
4
View on DB Fiddle
I have a question about how to add data in the table users_in_queue? I want to add data from another table but field number_in_queue must be like an autoincrement column for each queue_id. I mean that data in the table must have this structure:
user_id | queue_id | number_in_queue
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 1 | 3
The older version have to use some user variables to get to your result.
That is why it is quite ugly
CREATE Table user_in_queues(user_id INTEGER, queue_id INTEGER , number_in_queue INTEGER)
✓
INSERT INTO user_in_queues VALUES (1,1,0),(2,1,0),(3,2,0),(4,1,0),(5,2,0);
✓
SELECT * FROM user_in_queues;
user_id | queue_id | number_in_queue
------: | -------: | --------------:
1 | 1 | 0
2 | 1 | 0
3 | 2 | 0
4 | 1 | 0
5 | 2 | 0
SELECT
user_id, queue_id, number_in_queue
FROM
(SELECT
user_id,
IF(#queue = queue_id, #rn:=#rn + 1, #rn:=1) number_in_queue,
#queue:=queue_id queue_id
FROM
(SELECT
*
FROM
user_in_queues
ORDER BY queue_id , user_id) t1, (SELECT #queue:=0) t2, (SELECT #rn:=0) t3) t4
ORDER BY user_id;
user_id | queue_id | number_in_queue
------: | -------: | --------------:
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 1 | 3
5 | 2 | 2
UPDATE user_in_queues u
SET number_in_queue = (SELECT
number_in_queue
FROM
(SELECT
user_id,
IF(#queue = queue_id, #rn:=#rn + 1, #rn:=1) number_in_queue,
#queue:=queue_id queue_id
FROM
(SELECT
*
FROM
user_in_queues
ORDER BY queue_id , user_id) t1, (SELECT #queue:=0) t2, (SELECT #rn:=0) t3) t4
WHERE user_id = u.user_id AND queue_id = u.queue_id )
✓
SELECT * FROM user_in_queues;
user_id | queue_id | number_in_queue
------: | -------: | --------------:
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 1 | 3
5 | 2 | 2
db<>fiddle here
I'm working with SQL Server and I have this 3 tables
STUDENTS
| id | student |
-------------
| 1 | Ronald |
| 2 | Jenny |
SCORES
| id | score | period | student |
| 1 | 8 | 1 | 1 |
| 2 | 9 | 2 | 1 |
PERIODS
| id | period |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
And I want a query that returns this result:
| student | score1 | score2 | score3 | score4 |
| Ronald | 8 | 9 | null | null |
| Jenny | null | null | null | null |
As you can see, the number of scores depends of the periods because sometimes it can be 4 o 3 periods.
I don't know if I have the wrong idea or should I make this in the application, but I want some help.
You need to PIVOT your data e.g.
select Y.Student, [1], [2], [3], [4]
from (
select T.Student, P.[Period], S.Score
from Students T
cross join [Periods] P
left join Scores S on S.[Period] = P.id and S.Student = T.id
) X
pivot
(
sum(Score)
for [Period] in ([1],[2],[3],[4])
) Y
Reference: https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-20
Consider the following sqlite3 table:
+------+------+
| col1 | col2 |
+------+------+
| 1 | 200 |
| 1 | 200 |
| 1 | 100 |
| 1 | 200 |
| 2 | 400 |
| 2 | 200 |
| 2 | 100 |
| 3 | 200 |
| 3 | 200 |
| 3 | 100 |
+------+------+
I'm trying to write a query that will select the entire table and return 1 if the value in col2 is 200, and 0 otherwise. For example:
+------+--------------------+
| col1 | SOMEFUNCTION(col2) |
+------+--------------------+
| 1 | 1 |
| 1 | 1 |
| 1 | 0 |
| 1 | 1 |
| 2 | 0 |
| 2 | 1 |
| 2 | 0 |
| 3 | 1 |
| 3 | 1 |
| 3 | 0 |
+------+--------------------+
What is SOMEFUNCTION()?
Thanks in advance...
In SQLite, boolean values are just integer values 0 and 1, so you can use the comparison directly:
SELECT col1, col2 = 200 AS SomeFunction FROM MyTable
Like described in Does sqlite support any kind of IF(condition) statement in a select you can use the case keyword.
SELECT col1,CASE WHEN col2=200 THEN 1 ELSE 0 END AS col2 FROM table1
It is my table can i get result for this .
Each user have points,i have to show number of user points got in current month,current year
Thanks.
--------------------------------
| userId | points | date |
--------------------------------
| 1 | 5 | 8/25/2013 |
| 1 | 3 | 8/16/2013 |
| 1 | 2 | 8/01/2013 |
| 1 | 2 | 9/25/2013 |
| 1 | 5 | 8/25/2013 |
| 1 | 3 | 2/16/2012 |
| 2 | NULL | NULL |
| 2 | NULL | NULL |
--------------------------------
They result should be like :
---------------------------------------------------
| userId | CurrentMonthpoints | CurrentYearPoints |
---------------------------------------------------
| 1 | 15 | 17 |
| 2 | NULL | NULL |
---------------------------------------------------
My request :
SELECT userId,
(SELECT sum(points)
from tbl_points
WHERE PointsDate between '8/1/2013' and '8/31/2013') AS CurrentMonthPoints,
(SELECT distinct SUM(points)
from tbl_points
WHERE PointsDate between '1/1/2014' and '12/31/2014' ) AS CurrentYearPoints
from tbl_user_performance_points
But My query shows wrongly as :
---------------------------------------------------
| userId | CurrentMonthpoints | CurrentYearPoints |
---------------------------------------------------
| 1 | 15 | 17 |
| 2 | 15 | 17 |
---------------------------------------------------
Advance Thanks
Give this a try:
select a.userID,
a.points_sum_m as currentmonthpoints,
b.points_sum_y as currentyearpoints
from (select userID, sum(points) as points_sum_m
from tbl_points
where month(date) = month(getdate())
group by userID) a
inner join (select userID, sum(points) as points_sum_y
from tbl_points
where year(date) = year(getdate())
group by userID) b
on a.userID = b.userID;
SQLFiddle: http://sqlfiddle.com/#!3/ff780/12
with month_cte(userid,Current_Month) as
(select userid,sum(Points)Current_Month from tbl_points
where datepart(mm,PointsDate)=datepart(mm,getdate()) and datepart(YY,PointsDate)=datepart(yy,getdate()) group by userid)
, year_cte(userid,Current_year) as
(select userid,sum(Points)Current_Year from tbl_points
where datepart(YY,PointsDate)=datepart(yy,getdate()) group by userid)
select Distinct t.Userid, Current_Month, Current_Year From tbl_points T
left join month_cte mc on T.userid=mc.userid
left join year_cte yc on t.userid=yc.userid
if you just want to correct your query replace it with this...
SELECT userId,
(SELECT sum(points)
from tbl_points
WHERE PointsDate between '8/1/2013' and '8/31/2013' and userId= X.UserId) AS CurrentMonthPoints,
(SELECT distinct SUM(points)
from tbl_points
WHERE PointsDate between '1/1/2014' and '12/31/2014' and userId = X.UserId) AS CurrentYearPoints
from tbl_user_performance_points X