aggregate values in SQL columns - sql

I am SQL beginner and I have the following question:
I have the following data (article sales by week)
COUNT WEEK ART
4 1 A
9 1 B
5 2 A
4 2 B
6 3 A
5 3 B
7 4 A
2 4 B
I would like to have the following output
ODD_WEEK EVEN_WEEK ART
10 12 A
14 6 B
in other words, I would like to group the elements by a criteria in a column (ODD_WEEK) and by an other criteria in another column (EVEN_WEEK)
Is it possible in T-SQL?

Try this query
select
art,
sum(case when week%2=0 then count else 0 end) even,
sum(case when week%2<>0 then count else 0 end) odd
from
tbl
group by
art
FIDDLE
| ART | EVEN | ODD |
|-----|------|-----|
| A | 12 | 10 |
| B | 6 | 14 |

Related

Select all the records in the first table that match each of the records in the second

I'm working with an Access database and have two tables:
ID_1
Number
Some other data
1
1
Data
2
2
Data
3
3
Data
4
4
Data
5
3
Data
6
1
Data
7
2
Data
8
3
Data
9
1
Data
10
1
Data
11
2
Data
12
3
Data
13
4
Data
14
1
Data
15
2
Data
16
3
Data
17
4
Data
18
3
Data
19
3
Data
ID_2
Number
Some other data
1
3
Data
2
1
Data
3
2
Data
4
3
Data
5
2
Data
As you see, both tables have duplicate data. I need a query that would select all the records in the first table that match each of the records in the second, they are related by Number field. It's also necessary that these records aren't repeated (that is, that the query doesn't repeat values when selecting). For the given example I should get this result:
ID
ID_1
Number
Some other data
1
3
3
Data
2
5
3
Data
3
8
3
Data
4
12
3
Data
5
16
3
Data
6
18
3
Data
7
19
3
Data
8
1
1
Data
9
6
1
Data
10
9
1
Data
11
10
1
Data
12
14
1
Data
13
2
2
Data
14
7
2
Data
15
11
2
Data
16
15
2
Data
I was thinking that maybe I could use Join, but I still don't know how; tried Where, but also didn't find a use for it. Could you please help me with that?
I don't see where you're generating your output ID field from - or where you're picking your Data field from so here's the best guess.
SELECT Table1.ID_1, Table1.Number, Table1.[Some other data]
FROM Table1
WHERE (Table1.Number In (SELECT Number From Table2))
ORDER BY Table1.Number, Table1.ID_1;
Looks like this:
MySql DB data structure
create table tbl1(ID_1 serial, Number int);
create table tbl2(ID_2 serial, Number int);
insert into tbl1(Number) values (1),(2),(3),(4),(3),(1),(2),(3),(1),(1),(2),(3),(4),(1),(2),(3),(4),(3),(3);
insert into tbl2(Number) values (3),(1),(2),(3),(2);
query (with s), needed to remove duplicates
the window function count(tbl1.Number) OVER(PARTITION BY Number) sorts the result for us by the count of matched numbers
the #rownum variable is needed to count rows
with s as (select distinct Number from tbl2),
f as (select ID_1,tbl1.Number from tbl1 left join s on
(tbl1.Number=s.Number) where s.Number is not null order by
count(tbl1.Number) OVER(PARTITION BY Number) desc)
select #rownum := #rownum + 1 AS ID,ID_1,Number from f, (SELECT #rownum := 0) r;
results
+------+------+--------+
| ID | ID_1 | Number |
+------+------+--------+
| 1 | 3 | 3 |
| 2 | 5 | 3 |
| 3 | 8 | 3 |
| 4 | 12 | 3 |
| 5 | 16 | 3 |
| 6 | 18 | 3 |
| 7 | 19 | 3 |
| 8 | 1 | 1 |
| 9 | 6 | 1 |
| 10 | 9 | 1 |
| 11 | 10 | 1 |
| 12 | 14 | 1 |
| 13 | 2 | 2 |
| 14 | 7 | 2 |
| 15 | 11 | 2 |
| 16 | 15 | 2 |
+------+------+--------+

SQL sum total each column in last row

I wish SQL for SUM each column(IPO and UOR) in TOTAL in second last. And GRAND TOTAL(Sum IPO + UOR) in the last one. Thank you so much
No Code IPO UOR
----------------------
1 D173 1 0
2 D176 3 0
3 D184 1 1
4 D185B 1 0
5 D187 1 2
6 F042 3 0
7 ML004 12 3
8 TTPMC 2 0
9 Z00204 1 0
------------------
TOTAL (NOS) 25 6
-------------------------
GRAND TOTAL (NOS) 31
Here is my code, :
SELECT
SUM(CASE WHEN IPOType = 'IPO' THEN 1 ELSE 0 END) as IPO,
SUM(CASE WHEN IPOType = 'UOR' THEN 1 ELSE 0 END) as UOR
FROM IPO2018
GROUP BY OriProjNo
it can show like this
No Code IPO UOR
----------------------
1 D173 1 0
2 D176 3 0
3 D184 1 1
4 D185B 1 0
5 D187 1 2
6 F042 3 0
7 ML004 12 3
8 TTPMC 2 0
9 Z00204 1 0
------------------
Generally speaking, you want to leave totals and sub-totals to whatever tool you are presenting your data in, as they will be able to handle the formatting with significantly more ease. In addition, your desired output does not have the same number of columns (Grand Total row only has one numeric) so even if you did shoehorn this in to the same dataset, the column headings wouldn't make sense.
That said, you can return group totals via the with rollup statement. This will provide an additional row with the aggregate totals for the group. Where there is more than one group in your data, you will get a sub-total row for each group and a total row for the entire dataset:
declare #t table(c nvarchar(10),t nvarchar(3));
insert into #t values ('D173','IPO'),('D176','IPO'),('D176','IPO'),('D176','IPO'),('D184','IPO'),('D184','UOR'),('D185B','IPO'),('D187','IPO'),('D187','UOR'),('D187','UOR'),('F042','IPO'),('F042','IPO'),('F042','IPO'),('TTPMC','IPO'),('TTPMC','IPO'),('Z00204','IPO'),('ML004','UOR'),('ML004','UOR'),('ML004','UOR'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO');
select row_number() over (order by grouping(c),c) as n
,case when grouping(c) = 1 then 'TOTAL (NOS)' else c end as c
,sum(case when t = 'IPO' then 1 else 0 end) as IPO
,sum(case when t = 'UOR' then 1 else 0 end) as UOR
from #t
group by c
with rollup
order by grouping(c)
,c;
Output:
+----+-------------+-----+-----+
| n | c | IPO | UOR |
+----+-------------+-----+-----+
| 1 | D173 | 1 | 0 |
| 2 | D176 | 3 | 0 |
| 3 | D184 | 1 | 1 |
| 4 | D185B | 1 | 0 |
| 5 | D187 | 1 | 2 |
| 6 | F042 | 3 | 0 |
| 7 | ML004 | 12 | 3 |
| 8 | TTPMC | 2 | 0 |
| 9 | Z00204 | 1 | 0 |
| 10 | TOTAL (NOS) | 25 | 6 |
+----+-------------+-----+-----+

SQL Query to select each row with max value per group

I'm very new to SQL and this one has me stumpted. Can you help me out with this query?
I have the following 2 tables:
TABLE 1: IssueTable
Id | RunId | Value
---
1 | 1 | 10
2 | 1 | 20
3 | 1 | 30
4 | 2 | 40
5 | 2 | 50
6 | 3 | 60
7 | 4 | 70
8 | 5 | 80
9 | 6 | 90
TABLE 2: RunTable
RunId | EnvironmentId
---
1 | 1
2 | 3
3 | 1
4 | 2
5 | 4
6 | 2
I need the IssueTable rows that represent the Max RunId grouped by the EnvironmentId in the RunTable. The result I would need from the tables is:
EXPECTED RESULT:
Id | RunId | Value | EnvironmentId
---
4 | 2 | 40 | 3
5 | 2 | 50 | 3
6 | 3 | 60 | 1
8 | 5 | 80 | 4
9 | 6 | 90 | 2
So only the rows with the most recent/highest RunId from the RunTable per EnvironmentId. For example, for the EnvironmentId of "1", I only want rows that contain a RunId of "3" because the most recent RunId on EnvironmentId "1" from the RunTable is "3". Likewise, the most recent run for EnvironementId "2" was RunId "6"
Use a subquery to get the max runid for each environmentid from the runtable. Join the obtained result to the issuetable and select the required columns.
select i.id, i.runid, i.value, r.environmentid
from (select environmentid, max(runid) maxrunid
from runtable
group by environmentid) r
join issuetable i on i.runid = r.maxrunid
order by i.runid, i.id
These days one can use the analytical functions like RANK, DENSE_RANK, ROW_NUMBER to generate some ranking of your records.
Window functions are part of the ANSI SQL:2003 standard.
And I've at least encountered them on TeraData, Oracle and SQL-Server.
select Id, RunId, Value, EnvironmentId
from (
select i.*, r.EnvironmentId,
dense_rank() over (partition by r.EnvironmentId order by r.RunId desc) as RN
from issuetable i
inner join runtable r on (i.RunId = r.RunId)
) Q
where RN = 1
order by Id;
The inner query would yield the following results :
Id RunId Value EnvironmentId RN
1 1 10 1 2
2 1 20 1 2
3 1 30 1 2
4 2 40 3 1
5 2 50 3 1
6 3 60 1 1
7 4 70 2 2
8 5 80 4 1
9 6 90 2 1

Sum and distinct in acces SQL

I already made a query that this was it result :
7 | 3
8 | 4
8 | 2
8 | 1
10 | 3
12 | 4
12 | 1
13 | 3
I need new query that take this result and return this :
7 | 3
8 | **7**
10 | 3
12 | **5**
13 | 3
In the left column I need that evry number will appears only once,
and in the right column sum the numbers according to the value in the left column as I showed before.
how to do it?
SELECT leftField, SUM(rigthField) as rigthField
FROM YourResult
GROUP BY leftField

Issue doing LEFT JOIN twice, (multiplying rows)

Let's suppose I have 3 tables right now, One table with lessons, one table with the ratings of those lessons, and one table with the users of those lessons.
Lessons is a quite regular table, and the other 2 tables are relation tables like this:
TABLE LESSONS
ID | NAME | DESCRIPTION | CREATED BY | APPROVED BY | LEVEL | DATE CREATED | LAST EDIT
1 les1 desc1 10 12 1 12-12-2000 12-12-2000
2 les2 desc2 23 12 2 12-12-2000 12-12-2000
3 les3 desc3 12 12 3 12-12-2000 12-12-2000
TABLE RATINGS
ID | LESSON | USER | RATING | COMMENT
1 1 60 5 very good
2 2 30 4 nice
3 2 62 4 my comment
4 3 65 3 nice
5 3 78 5 very good
6 1 26 1 very bad
6 1 45 3 other comment
TABLE LESSONSXUSERS
ID | LESSON | USER | STATUS
1 1 60 2
2 1 26 2
2 1 45 2
3 2 30 2
4 2 62 2
5 3 65 2
6 3 78 2
7 1 22 1
8 1 19 1
And I'm trying to generate a view that shows me only approved lessons, with some info extracted from the other two tables:
CREATE OR REPLACE VIEW `skn_approved_lessons` AS
select
`l`.`id_skn_lessons` AS `id_skn_lessons`,
`l`.`name` AS `name`,
`l`.`description` AS `description`,
`l`.`createdBy` AS `createdBy`,
`l`.`approvedBy` AS `approvedBy`,
`l`.`id_skn_lessonsLevels` AS `id_skn_lessonsLevels`,
`l`.`dateCreated` AS `dateCreated`,
`l`.`lastEdit` AS `lastEdit`,
AVG(`lr`.`rating`) AS `avgScore`,
COUNT(`lxu`.`id_skn_users`) AS `students`
from ((`skn_lessons` AS `l`
left join `skn_lessonsRatings` AS `lr` on `l`.`id_skn_lessons` = `lr`.`id_skn_lessons`) left join `skn_lessonsXusers` AS `lxu` on `lxu`.`id_skn_lessons` = `l`.`id_skn_lessons`)
where ((`l`.`approvedBy` is not null) and
(`l`.`approvedBy` <> `l`.`createdBy`))
group by `l`.`id_skn_lessons`;
It's "kinda" working, since it shows me what I want, but the amount of users is wrong. I know why this happens, but I don't know how to fix it.
Issue is, I should get this:
VIEW APPROVED
ID | NAME | DESCRIPTION | CREATED BY | APPROVED BY | LEVEL | DATE CREATED | LAST EDIT | AVG RATING | STUDENTS
1 les1 desc1 10 12 1 12-12-2000 12-12-2000 3 4
2 les2 desc2 23 12 2 12-12-2000 12-12-2000 4 2
3 les3 desc3 12 12 3 12-12-2000 12-12-2000 4 2
But I'm getting this:
VIEW APPROVED
ID | NAME | DESCRIPTION | CREATED BY | APPROVED BY | LEVEL | DATE CREATED | LAST EDIT | AVG RATING | STUDENTS
1 les1 desc1 10 12 1 12-12-2000 12-12-2000 3 15
2 les2 desc2 23 12 2 12-12-2000 12-12-2000 4 4
3 les3 desc3 12 12 3 12-12-2000 12-12-2000 4 4
Notice users column is wrong, what I'm really getting is the product of usersxamountOfRatings. It's making the query with each registry of user by each registry of rating, so I'll always get the usersxratings as the amount of users, which is NOT what I want.
I don't quite understand how to do the second join after grouping by lesson.id, that I guess will solve the issue.
Thanks in advance.
I believe all you need to do is to add the DISTINCT argument to the COUNT aggregate function as follows:
COUNT(DISTINCT lxu.id_skn_users)
Adding the DISTINCT argument will return the number of unique nonnull values from that column.
I see Barmar already put that into the comments above.