ms access - query null row - sql

Is it possible a query with Group By to show null rows?
let's say that my table has [PlaceID] and [Times].
PlaceID I Times
--------I-------
1 I 2
3 I 1
1 I 1
3 I 2
3 I 4
1 I 2
If I make the following SQL, [PlaceID] will not be visible because there is no data.
SELECT PlaceID, Sum(Times) As SumTimes
FROM tblOrder
GROUP BY PlaceID;
PlaceID I SumTimes
--------I-------
1 I 5
3 I 7
Is it possible to force it and have this output
PlaceID I SumTimes
--------I-------
1 I 5
2 I 0
3 I 7

You need a list of places . . . which I would guess is in the places table.
Then:
select p.placeId, nz(sum(times), 0)
from places as p left join
tblOrder as o
on p.placeId = o.placeId
group by p.placeId;
If there are more than three places, you can add where p.placeId in (1, 2, 3).

Related

Get max record for each group of records, link multiple tables

I seek to find the maximum timestamp (ob.create_ts) for each group of marketid's (ob.marketid), joining tables obe (ob.orderbookid = obe.orderbookid) and market (ob.marketid = m.marketid). Although there are a number of solutions posted like this for a single table, when I join multiple tables, I get redundant results. Sample table and desired results below:
table: ob
orderbookid
marketid
create_ts
1
1
1664635255298
2
1
1664635255299
3
1
1664635255300
4
2
1664635255301
5
2
1664635255302
6
2
1664635255303
table: obe
orderbookentryid
orderbookid
entryname
1
1
'entry-1'
2
1
'entry-2'
3
1
'entry-3'
4
2
'entry-4'
5
2
'entry-5'
6
3
'entry-6'
7
3
'entry-7'
8
4
'entry-8'
9
5
'entry-9'
10
6
'entry-10'
table: m
marketid
marketname
1
'market-1'
2
'market-2'
desired results
ob.orderbookid
ob.marketid
obe.orderbookentryid
obe.entryname
m.marketname
3
1
6
'entry-6'
'market-1'
3
1
7
'entry-7'
'market-1'
6
2
10
'entry-10'
'market-2'
Use ROW_NUMBER() to get a properly filtered ob table. Then JOIN the other tables onto that!
WITH
ob_filtered AS (
SELECT
orderbookid,
marketid
FROM
(
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY
marketid
ORDER BY
create_ts DESC
) AS create_ts_rownumber
FROM
ob
) ob_with_rownumber
WHERE
create_ts_rownumber = 1
)
SELECT
ob_filtered.orderbookid,
ob_filtered.marketid,
obe.orderbookentryid,
obe.entryname,
m.marketname
FROM
ob_filtered
JOIN m
ON m.marketid = ob_filtered.marketid
JOIN obe
ON ob_filtered.orderbookid = obe.orderbookid
;

Select field 1 Where Field 2 ='value1' And field 2 ='value2' when Field 1 is Same

I am currently having troubles with filtering my SQL records. I need something like what it results in the following concept: Table is
A B
1 1
1 3
2 1
2 2
2 3
2 4
3 1
3 2
I want to select value of A , where B=1 and B=2 And B=3 when same A .... result is
A
2
Please help
You can use aggregation:
select a
from mytable
where b in (1, 2, 3)
group by a
having count(*) = 3
This assumes no duplicates in the table - else, you need to change the having clause to:
having count(distinct b) = 3

Left Join Display All Data From Table1 and Table2

I am trying to do a left join so that I get all of my rows from Table 1 even if there is no value corresponding to it in the second table.
My structures are:
Location Table:
ID LocName
1 Trk1
2 Trk2
3 Trk3
4 Unk
Quantity Table:
ID PartID Quantity LocationID
1 1 2 1
2 3 12 2
3 2 6 1
4 6 8 3
5 6 5 1
I am trying to join but also make a query on a specific PartID. My query is:
SELECT
INV_LOCATIONS.ID AS LocationID,
INV_LOCATIONS.NAME AS LocationName,
INV_QUANTITY.QUANTITY AS Quantity
FROM INV_LOCATIONS
LEFT JOIN INV_QUANTITY ON INV_LOCATIONS.ID = INV_QUANTITY.LOCATION_ID
WHERE INV_QUANTITY.PART_ID = 1;
My output right now would be:
ID LocName Quantity
1 Trk1 5
3 Trk3 8
The Desired output is:
ID LocName Quantity
1 Trk1 5
2 Trk2 NULL/0
3 Trk3 8
4 Unk NULL/0
I assume it is because I have the WHERE INV_QUANTITY.PART_ID = 1 and that is forcing it to be in the quantity table. I need to be able to verify it is on the right part but how do I also include it if it doesn't exist. I know I have done something very similar before but I cannot remember which project and so I cannot find the code anywhere.
You need to move the filtering logic to the ON clause:
SELECT il.ID AS LocationID, il.NAME AS LocationName,
iq.QUANTITY AS Quantity
FROM INV_LOCATIONS il LEFT JOIN
INV_QUANTITY iq
ON il.ID = iq.LOCATION_ID AND iq.PART_ID = 1;

How to apply a single query that sum column for individual values

I have 2 tables named user and statistics
user table has 3 columns: id, name and category
statistics table has 3 columns: id, idUser (relational), cal
something like this:
user
Id name category
1 name1 1
2 name2 2
3 name3 3
statistics
Id idUser cal
1 1 1
2 1 1
3 1 1
4 2 1
5 2 1
How can I apply a query that sum the cal column by each category of users and give me something like this:
category totalcal
1 3
2 2
3 0
You want to do a left join to keep all the categories. The rest is just aggregation:
select u.category, coalesce(sum(s.cal), 0) as cal
from users u left join
statistics s
on u.id = s.idUser
group by u.category;
Use LEFT JOIN to get 0 sum for the category=3:
SELECT
user.category
,SUM(statistics.cal) AS totalcal
FROM
user
LEFT JOIN statistics ON statistics.idUser = user.Id
GROUP BY
user.category
Here SUM would return NULL for category=3. To get 0 instead of NULL you can use COALESCE(SUM(statistics.cal), 0).

SQL Join with Master/Detail and Sum

I've got 2 tables that I need to join and need some help.
ProjectList
Recnum ProjectName Contact
1 Test 1 Me
2 Test 2 Myself
3 Test 3 I
TimeTrack
Recnum Regular Overtime
1 8 0
3 4 2
3 8 0
Results
Recnum ProjectName Contact TotalRegular TotalOvertime
1 Test 1 My 8 0
2 Test 2 Myself 0 0
3 Test 3 I 12 2
We've got it to the point of fetching all fields, but not all records from ProjectList. Need some help, please.
Looks like a direct Inner Join with Aggregate Sum should work
SELECT A.Recnum,
ProjectName,
Contact,
Ifnull(Sum(Regular), 0),
Ifnull(Sum(Overtime), 0)
FROM ProjectList A
LEFT JOIN TimeTrack b
ON a.Recnum = b.Recnum
GROUP BY A.Recnum,
ProjectName,
Contact