I'm working on a Report in Visual Basic. It has a query to show the information below.
So, I need a query that sets all the rows that are between 2 values that the user choose. The problem is that the value exists several times.
To explain better:
ID Category_ID SubCategory_ID Description Period_ID Data
----------- ----------- -------------- ------------- ----------- --------
1 1 1 PRUEBA 1 100.00
2 2 5 Total 1 2.00
3 1 1 sgsdg 2 25.00
4 1 1 fsdf 2 5.00
5 1 1 sdf 2 54.00
There will be a lot more of Period_ID. So, if the user chooses Period 1 and Period 5, it will show all the data between Period 1 and Period 5 (i.e. Period1, Period2, Period3, Period4 and Period5).
Is there a query which can do this?
I appreciate your help!
I would do:
Select *
FROM SAMPLE_TABLE
WHERE SAMPLE_TABLE.PERIOD_ID
IN (SELECT ID FROM PERIOD
WHERE ID >= 1 AND ID <= 5)
This is called a subquery. This relies on you have a period table, which I assume you have
Related
I have three tables in MS SQL Server 2014. Each of them holds a couple of numeric values, a description and a date. For the sake of brevety, let's assume the following tables:
table "beverages"
day beverage amount
---------- -------- ------
2018-12-01 water 2
2018-12-01 tea 1
2018-12-01 coffee 7
2018-12-02 water 4
2018-12-02 tea 2
table "meals"
day meal amount
---------- ------ ------
2018-12-01 burger 1
2018-12-01 bread 2
2018-12-02 steak 1
table "fruit"
day fruit amount
---------- ------ ------
2018-12-01 apple 4
2018-12-01 banana 1
2018-12-02 apple 2
Then I have another table holding only a list of dates.
table "dates"
day
----------
2018-12-01
2018-12-02
What I need is a query that returns one row for each of the rows in the dates table, and in each row has the date, the total amount of beverages, the total amount of meals and the total amount of fruit for that day. I do not care for the different types of beverages, meals and fruit, just the sum. The result should be:
expected result
day beverages meals fruit
---------- ----------- ----------- -----------
2018-12-01 10 3 5
2018-12-02 6 1 2
But instead I receive
received result
day beverages meals fruit
---------- ----------- ----------- -----------
2018-12-01 40 18 30
2018-12-02 6 2 4
I already know what the problem is, just not how to fix it. Even worse, I'm sure that I knew the answer once, but now I can't even figure the right search terms to make Google tell me...
When I do the query like this (I used table variables for testing)
SELECT
[d].[day]
,SUM([b].[amount]) AS [beverages]
,SUM([m].[amount]) AS [meals]
,SUM([f].[amount]) AS [fruit]
FROM #dates AS [d]
LEFT OUTER JOIN #beverages AS [b]
ON [d].[day] = [b].[day]
LEFT OUTER JOIN #meals AS [m]
ON [d].[day] = [m].[day]
LEFT OUTER JOIN #fruit AS [f]
ON [d].[day] = [f].[day]
GROUP BY [d].[day]
it sums each row from the different tables more than once, because it returns every possible combination of the three tables. Removing the SUM() and GROUP BY proves that:
day beverages meals fruit
---------- ----------- ----------- -----------
2018-12-01 2 1 4
2018-12-01 2 1 1
2018-12-01 2 2 4
2018-12-01 2 2 1
2018-12-01 1 1 4
2018-12-01 1 1 1
2018-12-01 1 2 4
2018-12-01 1 2 1
2018-12-01 7 1 4
2018-12-01 7 1 1
2018-12-01 7 2 4
2018-12-01 7 2 1
2018-12-02 4 1 2
2018-12-02 2 1 2
So, what do I need to change in the query to make it sum the values for each of the three tables without multiplying it with the number of the rows in the other tables?
Group the Tables before joining like so:
SELECT
[d].[day]
,[b].[amount] AS [beverages]
,[m].[amount] AS [meals]
,[f].[amount] AS [fruit]
FROM #dates AS [d]
LEFT OUTER JOIN (SELECT day, SUM(amount) as amount FROM #beverages GROUP BY day) AS [b]
ON [d].[day] = [b].[day]
LEFT OUTER JOIN (SELECT day, SUM(amount) as amount FROM #meals GROUP BY day) AS [m]
ON [d].[day] = [m].[day]
LEFT OUTER JOIN (SELECT day, SUM(amount) as amount FROM #fruit GROUP BY day) AS [f]
ON [d].[day] = [f].[day]
How about a PIVOT instead?
Example
Select *
From (
Select day,Item='beverage',amount from beverages
Union All
Select day,Item='meals' ,amount from meals
Union All
Select day,Item='fruit' ,amount from fruit
) src
Pivot ( sum(amount) for Item in ([beverages],[meals],[fruit]) ) pvt
I have got a CTE with this sort of data
ID | Name | UserID | SupervisorID
1. Mark 1 2
2. John 1 1
3. Julia 3 1
4. Tomm 1 2
5. Eric 2 2
And if the SupervisorID is equal 2, then the rows with UserID equal 1 should NOT be returned.
If the SupervisorID is equal 1, then the rows with UserID equal 1 should NOT be returned.
I have tried with this query, but got stuck and can't figure out how to solve this problem.
SELECT * FROM CTE WHERE UserID <> CASE SupervisorID WHEN 1 THEN ??
I currently have 3 tables.
Table 1: customers
id(PK) name surname
----------------------------------
1 name1 surname1
2 name2 surname2
3 name3 surname3
4 name4 surname4
Table 2: sales
id(FK) game(FK) price(FK)
-----------------------------
1 1 1
2 4 4
3 4 4
4 3 3
1 3 3
2 3 3
3 2 2
Table 3: stock
id(FK) game price
-----------------------------
1 game1 20
2 game2 30
3 game3 40
4 game4 50
What I'm looking to do is find the sum of all the sales listed in the sales table (table 2).
So far, I can display a table showing how much money each game has made in total but cannot get the overall total of sales to display.
I have tried
select sum(sales.price)
from sold
However, this is just calculating the sum of the foreign key (in this case it would be 20). However, I want it to display 270.
You need to join the stock and sales tables to get the correct price of each of the items sold.
Select sum(stock.price) from sales
inner join stock on sales.game = stock.id
I did search, but the uniqueness of each question makes it hard for me to "translate" it for my dataset.
I have table A named: CLOGS17
With a sub-set of the data and fields shown:
SERIALNO EVDATE SYSNO AREA USRNO
4 2017-01-01 02:03:48.000 1 4 10
4 2017-01-01 02:09:00.000 1 4 10
4 2017-01-01 02:24:44.997 1 6 10
4 2017-01-01 02:56:50.000 1 2 18
5 2017-08-08 02:03:48.000 1 4 10
5 2017-01-09 02:09:00.000 1 4 10
6 2017-04-03 02:24:44.997 1 6 10
8 2017-05-05 02:56:50.000 1 2 18
My goal is to retrieve all records where the combination of SERIALNO + SYSNO + AREA + USRNO has not been used in the last 30 days (inactive user essentiallY) so I can delete that USRNO.
Desired output from above data would be (newest record for each SERIALNO, SYSNO, AREA, and USRNO distinct combination):
SERIALNO EVDATE SYSNO AREA USRNO
4 2017-01-01 02:09:00.000 1 4 10
4 2017-01-01 02:24:44.997 1 6 10
4 2017-01-01 02:56:50.000 1 2 18
5 2017-08-08 02:03:48.000 1 4 10
6 2017-04-03 02:24:44.997 1 6 10
8 2017-05-05 02:56:50.000 1 2 18
I am then able to get only those within the last 30 days.
Given the table data below ("Table B"), it is a list of all stored users:
SERIALNO CONTID SYSNO AREA USRID
36 001 1 * 1
36 001 1 * 18
36 001 1 * 2
36 001 1 * 29
36 001 1 * 36
36 001 1 1 10
This table contains ALL users in the system.
How can I return all the users from Table B that have not been used for a given CONTID, SYSNO, and AREA?
For the first part of your question it would be as simples as a group by of a select on the desired fields:
SELECT SERIALNO,
SYSNO,
AREA,
USRNO,
MAX(EVDATE)
FROM CLOGS17
GROUP BY SERIALNO,
SYSNO,
AREA,
USRNO
Since you didn't provide enough information about the second part. This query will give you the output you show in your question.
So, to get all users that doesn't meet your 30 days criteria (whatever it are), you just do a left join of you user table with the above query seeking the nulls for the query above, like this:
SELECT *
FROM tableb tb LEFT JOIN
(SELECT SERIALNO,
SYSNO,
AREA,
USRNO,
MAX(EVDATE)
FROM CLOGS17
GROUP BY SERIALNO,
SYSNO,
AREA,
USRNO) a
ON tb.SERIALNO = a.SERIALNO,
AND tb.SYSNO = a.SYSNO
AND tb.USRNO = a.USRNO
WHERE a.AREA is null
I have a table in which i have columns like SO number, line item number, amendment number and zone.
I need to display the total number of amendments zone wise.
I have 6 zones - a,b,c,d,e,f
so numbers like 1,2 for zone a; 3,4 for zone b; similarly for other zones.
in each so number there might be 1 or more than one line item numbers like 10,20,30 etc.
for each so number there will be just one amendment (i.e., even if an so number has many line item numbers, it will have just one amendment)
now, say in zone a, there are 4 so numbers 1,2,3,4. and in so number 1 there is one line item number 10 and its amendment number is 2. say for so number 2 there are 4 line item numbers 10,20,30,40 and the amendment number is 0. so for this zone a, total number of amendments would be 2 (desired result). how do i do it?
Zone so_number line_item_number amendment_number
a---------- 1 ---------------- 10 ---------------- 2
a---------- 2 ---------------- 10 ---------------- 0
a---------- 2 ---------------- 20 ---------------- 0
a---------- 2 ---------------- 30 ---------------- 0
a ---------- 2 ---------------- 40 ---------------- 0
a ---------- 3 ---------------- 10---------------- 1
a ---------- 4 ---------------- 10 ---------------- 3
a----------- 4 ----------------- 20 ---------------- 3
b ----------- 6 ---------------- 60 ----------------- 6
c------------ 7 ---------------- 80 ----------------- 0
c ---------- 8 ------------------ 10 ----------------- 0
d ------------ 9 ----------------- 10 ----------------- 2
e ------------ 10----------------- 20 ----------------- 3
f ------------ 11----------------- 30 ----------------- 1
f ------------- 11 --------------- 10 ------------------ 1
f ------------- 11 --------------- 20 ------------------- 1
f ------------ 12 ---------------- 10 ------------------ 2
For zone a total number of amendments would be 6, for zone b it would be 6, for c - 0, for d - 2, e - 3, f - 3
a zone may have any number of so numbers, an so number may have any number of line item numbers, but, an so number may have only one amendment number
How do I do it?
i really don't understand your question and your result.
It could seems very simple from my understands
select zone,
sum(amendment_number)
from tablea
group by zone
now my result would be : 9-6-0-2-3-5
Do you only want the last so_number?
select zone,
sumamend
from
(select zone,
sum(amendment_number) as sumamend,
row_number over (partition by zone order by so_number desc) as ord
from tablea
group by zone, so_number) b
where ord = 1
But again i don't have the same result : 6-6-0-2-3-2
if i correctly understand your comment and hoping you have all case in your example you want something like this :
select zone,
sum(amendment_number)
from (select distinct zone,so_number, amendment_number from #test) p
group by zone
it gives the following result : 6-6-0-2-3-3