SQL query joining and grouping table - mysql

Select table1.id, (table1.total_item-table2.requested) as items_left
from table1, table2
where table2.year = 2015
group by table1.id
i am using SQL Server 2008..
Whats wrong with my code? it cannot be group in id number,
i want to sum all the individual table1.items in table1, and sum all the individual table2.requested in table 2 to
subtract the remaining items as I name it items_left, and group it by table1.id
something like this...
0. id items_left year
1. 1 3 2015
2. 5 10 2015
3. 3 4 2015
this is the output of that code above...
there is a duplication of id and I cant group it to
0. id items_left
1. 1 1
2. 1 1
3. 1 2
4. 5 5
5. 3 2
6. 5 5
7. 3 2
i want an output like this.. please sir/ma'am help me..
something like this...
0. id items_left year
1. 1 3 2015
2. 5 10 2015
3. 3 4 2015

You are missing a "sum()" for the second field; also there is missing joining condition:
SELECT tab1.id, sum(tab1.total_item - ifnull(tab2.requested,0)) AS items_left
FROM tab1
LEFT JOIN tab2 ON tab1.id = tab2.??
WHERE tab2.year = 2015
GROUP BY tab1.id

Related

Sum within a column given two or more conditions in MySQL

In MySQL, I am trying to sum values in a column given certain conditions. I have an example of some data below
Team Season Mth Stat
A 1 1 4
A 1 1 4
A 1 2 7
A 1 2 9
B 1 1 6
B 1 1 6
B 1 2 6
B 1 2 9
C 1 1 1
C 1 1 3
C 1 2 3
C 1 2 6
But I need the output to show up as
Team Season Mth Stat
A 1 1 8
A 1 2 16
B 1 1 12
B 1 2 15
C 1 1 4
C 1 2 9
So the Stat column is now the sum of the cells such that Match, Season, and Team are all the same. I have the code below. I see a lot of answers that use 'case' but that seems to be given logical operators that are not equal to each other. When I do it below, now it doesn't recognise the table where the columns are coming from. I do have a inner joins but the data itself is from one table. I get another error as well on the sum function because it requires one argument.
select
Team
,Season
,Match
--this is where I get lost-----------
sum(
select
Stat
From
table
Where
Mth=Mth
AND Season=Season
AND Team=Team
)
--end of getting lost----------------
FROM
table
Where
Season IN (1,2)
GROUP BY
Team
,Season
,Mth
Order BY
Team ASC
Edit:
It turns out I need to use GROUP BY as the comments suggest. So I am not summing within a table, but I sum the variable given the Group By parameters.
Unless I'm missing something, it's simply:
SELECT Team
,Season
,Match
,Sum(Stat)
FROM table
GROUP BY
Team
,Season
,Match
It's simple as this:
SELECT Team,
Season,
Match,
SUM(Stat)
FROM Table
WHERE Season IN (1,2)
GROUP BY Team,
Season,
Match
ORDER BY Team ASC
Please look at the SQL Fiddle example.

how to create a table like this by mysql

I have a table like this:
ID Payment year
A 10 1
A 15 2
A 12 3
B 11 2
B 15 4
C 25 1
C 17 3
I'm looking for a query that returns row for each ID for the its last year. The year column is ordered increasing for each ID.
I need a result like this:
ID Payment year
A 12 3
B 15 4
C 17 3
I have this query so far:
select ID, Payment, Year from payment_table
where year = (select max(year) from ?????????);
I don't know what shall I write instead of ????????
It would be appreciated If anybody gives me some idea.
Use subquery :
select t.*
from table t
where year = (select max(t1.year) from table t1 where t1.id = t.id);

Count occurrences of distinct values

I am trying to find a MySQL query that will display the number of occurrences of an ID value within a year.
Table:
a_id year
---- ----
1 2010
1 2011
1 2012
1 2012
1 2013
1 2014
1 2015
2 2010
2 2011
2 2013
2 2014
2 2014
2 2015
3 2010
3 2010
3 2011
Expected output:
a_id year occurrences
---- ----- -----------
1 2010 1
1 2011 1
1 2012 2
1 2013 1
1 2014 1
1 2015 1
2 2010 1
2 2011 1
2 2013 1
2 2014 2
2 2015 1
3 2010 2
3 2011 1
I'm trying with the something along the lines of following sql query, but it gives me nothing like the expected output. It's the 3rd column im struggling with.
SELECT a__id, year, count(distinct a_id) as occurrences
FROM table1
GROUP by year
ORDER by a_id
How can i create that 3rd column?
Scince you are grouping by a_id and year you of course get only 1 distinct value for group. Simply change count(distinct a_id) to count(*).
For example you get group:
1 2012
1 2012
Notice in this group distinct a_id values is 1. But you want count of all rows in group. With distinct you will get 1 as occurence in all groups.
EDIT:
Ok, I have missed that you are grouping only by year, so you should group by a_id also. The rest of the answer stays as is. So you end up with:
SELECT a__id, year, count(*) as occurrences
FROM table1
GROUP by a__id, year
SELECT a__id, year, count(*) as occurrences
FROM table1
GROUP by a__id, year
Using the following will get you what you are looking for.
SELECT a_id, year, count(*)
FROM table1
GROUP BY a_id, year
ORDER BY a_id, year
Although, in previous versions, ORDER BY may have been guaranteed by MySQL, it is deprecated now. If you want to ensure your results come back sorted, add ORDER BY. 'Future you' will thank you for it.

Mysql query only select rows with unique result within GROUP_CONCAT

Is there a way to select only the rows that have an other result than the row previous selected?
In one of my tables I store advertisement data, that’s one row per advertisement. I also store in an other table the prices for rental per dag, week, month, this table contain more than one row per advertisement.
I want to select al the rows from table 2 where there is a change in one of the prices (in the example row 1 and 3 in table 2) in the same query as the data selection. I know that I have to use a GROUP_CONCAT to get one row instead of a 2 row result in this case, but how to get 2 result rows from table 2 and 1 result row in total?
The outcome of the query has to be something like: tre,234,” 12345678911,12,45, 32555678911,12,67 ”
Table 1 (advertisements)
ID_adv data1 data2
1 tre 234
2 ghj 34
3 jk 098
4 jfjk 12
Table 2 (dates)
ID_dates ID_adv timestamp_day price1 price2
1 1 12345678911 12 45
2 1 22345677771 12 45
3 1 32555678911 12 67
4 2 42345671231 34 34
I tried
SELECT
t1.*,
GROUP_CONCAT(t2.date) AS dates
FROM Table1 t1
LEFT JOIN Table2 t2 ON t2.ID_adv = t1.ID_adv
WHERE t1.ID_adv = 3 GROUP BY t1.ID_adv
Can you try this one:
SELECT T3.ID_adv
, T3.data1
, T3.data2
, CAST(GROUP_CONCAT(CONCAT(T3.timestamp_day, ',', T3.price1, ',', T3.price2)) AS CHAR) AS DatePrice
FROM (
SELECT T1.*
, MIN(T2.timestamp_day) AS timestamp_day
, T2.price1
, T2.price2
FROM Table1 T1
LEFT JOIN Table2 T2 ON T2.ID_adv = T1.ID_adv
GROUP BY T1.ID_adv, T2.price1, T2.price2
) T3
GROUP BY T3.ID_adv;
I've tried it on SQL Fiddle.

Select all rows with certain year, plus all rows of affected group

I have a table in which each hundret rows have the same id (see "chart_id" in shortened Example below).
If we consider every row with the same "chart_id" a "chart" how do I select every chart having a certain year in it with a mysql query.
For example how do I select every chart with 2009 in it (chart2 and chart3):
edit:
rows with id 4,5,6 and 7,8,9 should be selected if I look for 2009.
id chart_id time
1 1 2008
2 1 2008
3 1 2008
4 2 2008
5 2 2008
6 2 2009
7 3 2009
8 3 2009
9 3 2009
10 4 2010
11 4 2010
12 4 2010
Any suggestions?
Thanks :)
Just use DISTINCT:
SELECT DISTINCT chart_id
FROM myTable
WHERE time = 2009
Or, do you mean you want all the rows for that chart SQL Fiddle:
SELECT *
FROM myTable t1
WHERE EXISTS(SELECT 1
FROM myTable t2
WHERE t2.chart_id = t1.chart_id AND t2.time = 2009)
Seems like a SELECT DISTINCT chart_id WHERE year ... would work.
SELECT DISTINCT chart_id FROM table_name WHERE time = 2009