Check rows for differences between 2 dates for each specifc name - sql

So I have a table that consists of about 7 columns. Everyday I copy info from an access database into a sql table and throw a date on each record.
What I am looking to do is compare for instance today records to yesterdays records and check for any changes between the names.
Hope the make shift table below may help understand the question. In the example everyday three records get dumps in aa,bb,cc are the name. I want to be able to query if any information for "aa" has changed between 2 dates.
Table ID Name Info1 Info2 AD PH Date
1 aa yg yg a a 10/17
2 bb hg hg a a 10/17
3 cc hg po a a 10/17
4 aa yg yg a a 10/18
5 bb hk hg a a 10/18
6 cc hg po a a 10/18

select date
from your_table
where date between '2013-10-16' and '2013-10-17'
and name = 'aa'
group by date
having count(distinct info1) > 1
or count(distinct info2) > 1
or count(distinct AD) > 1
or count(distinct PH) > 1

Related

SQL Server creating view need a statement after having

I have this SQL Server table with this data:
ID Name Type
-------------------
1 ZZ INPUT
2 AA INPUT
3 CC OUTPUT
4 ZZ OUTPUT
5 AA INPUT
6 CC INPUT
7 KK OUTPUT
8 TT INPUT
9 CC OUTPUT
10 DD OUTPUT
As a result, I would like the only names that are used one time. And of the ones that are used ones only the OUTPUT type.
Correct result
ID Name Type
-------------------
1 KK OUTPUT
2 DD OUTPUT
I can do it by creating two views. Use the first view as a view in between. Can I achieve the result with one view?
you only need a group by query and checks for count(*) = 1
select row_number() over (order by Name) as ID, Name
from your_table
group by Name
having count(*) = 1
and min(Type) = 'OUTPUT';

To calculate Number of days difference between two tables entries and update it in another table using SQL server

enter image description hereI have two tables A and B where, A contains todays data only and B contains historical data,
both tables have same attributes- pcode, product, market, pceneter, date. where All columns have same entry except date as for table A date is todays date and for B date is the first entry date in the table
I am trying to calculate day difference for A table's records available in B
Example: Table A
code product market center date No.of Days
X1 abcd IT04 2G 17/9/2021 0
X1 efgh ER90 MB 17/9/2021 0
Y5 ijkl OK09 MB 17/9/2021 0
Table B
code product market center date
X1 abcd IT04 2G 15/9/2021
X1 efgh ER90 MB 16/9/2021
X1 abcd IT04 2G 11/9/2021
X1 efgh ER90 C8 11/9/2021
expected output - Table A No of days should be updated with date difference for each reco available in talbe B
Example: Table A
code product market center date No.of Days
X1 abcd IT04 2G 17/9/2021 6
X1 efgh ER90 MB 17/9/2021 1
Y5 ijkl OK09 MB 17/9/2021 0
my solution:
1. I grouped the table B to get the minimum date with other columns.
2. Afterwards, i joined this subquery and table B to match and subtruct dates
Note: i may did some syntax problems, since i dont have any data / table to test it. But i hope you get the logic...
select a.*, DATEDIFF(day, a.date, h.date) as no_of_days
from table_a a
left join (select code, product, market, center, min(date) date
from table_b
group by code, product, market, center) h
on a.code = h.code
and a.product = h.product
and a.market = h.market
and a.center = h.center

need to get group by data using join/subQuery on multiple tables

I have table lookup with columns trackingId and string
trackingId
string
1
QQ
3
we
2
QQ
4
rt
.
..
.
..
.
..
select trackingId from lookup where string = 'QQ'
I need to use output of this query in table - content_item. content_item follow below
content_id
tracking_id
approval_status
date
101
1
AP
2014
102
1
PN
2016
103
3
PN
2015
104
4
AP
2018
105
2
RJ
2019
106
4
PN
2019
107
5
PN
2019
108
6
RJ
2019
...
.
..
....
I want to group this data by date
my output should be like
year
count
2014
1
2019
3
....
.
means in each group tracing ids should be unique.
(if one tracking id is counted in one group then it should not be counted in any other group)
Also data of tracking id 3 and 4 should not be counted (b'z I only want data for string QQ).
Please let me know if this is doable. any help will be appreciated...
JOIN the tables to get the 'QQ' content_items. Then GROUP BY and COUNT() the rows:
select c.date, count(*)
from content_item c
join lookup l on c.tracking_id = l.tracking_id
where l.string = 'QQ'
group by c.date

Create a column of sum of values after grouping two columns in Power BI

I am trying to sum values of third column based on first two columns and enter in new column.
Day Product type price total
1/1/2019 A1 T1 3 8
1/1/2019 A1 T2 5 8
1/2/2019 A2 T1 2 3
1/2/2019 A2 T2 1 3
1/1/2019 B1 T1 4 12
1/1/2019 B1 T2 7 12
1/2/2019 B2 T1 3 5
1/2/2019 B2 T2 2 5
1/3/2019 A1 T2 2 8
1/4/2019 A2 T1 9 11
1/3/2019 B1 T1 6 11
1/3/2019 B1 T2 5 11
1/4/2019 B2 T1 4 4
Total is sum of price regardless of type and unique as combination of date Product. check these excel columns
It is normally not recommended to add a column for summarized values. Summarization is supposed to be done with measures.
It is very easy to get the Total for each Day and Product. First you will define a measure. In the Modeling tab, click New Measure and type Total = SUM(Sales[Price]). I'm assuming the name of your table to be "Sales", so you need to replace it with your own table name.
Then in the report, choose an appropriate visualization and drag and drop Day, Product, and Total. The measure Total calculates the sum of Price for each Day and Product on the fly.
It is also possible to keep the Total of Day and Product in a column inside the model. However, this is not a best practice. Before doing this, try to find a way with measures, and only do this if you are an experienced user and you know there is some good reason to do this.
In this case, in the Modeling tab, click New Column and input this formula.
Total of Day and Product = CALCULATE(
SUM(Sales[Price]),
ALLEXCEPT(Sales, Sales[Day], Sales[Product])
)
Go to the Edit Queries > Add Column > Custom Column and use something like this:
= if [Product] = "A1" and [type] = "T1" then [price] * [total] else [price] * [total] * 2
This calcualation is just an example how its done because you didnt provide any information what your criterias are to sum the values in the third column. But with this example you should be able to create your new column by yourself.

Group by in SQL returning error: Selected non-aggregate values must be part of the associated group

I have a table that looks like this:
date store flag
1 5/4/2018 a 1
2 5/4/2018 a 1
3 5/3/2018 b 1
4 5/3/2018 b 0
5 5/2/2018 a 1
6 5/2/2018 b 0
I want to group by date and store and sum the number of flags
i.e. table_a below:
date store total_flag
1 5/4/2018 a 2
3 5/3/2018 b 1
4 5/2/2018 a 1
5 5/2/2018 b 0
This is what I'm trying:
create multiset volatile table flag_summary as (
sel table_a.*, SUM(table_a.flag) as total_flag
group by date, store
)
with data primary index (date, store) on commit preserve rows;
The above gives me an error, "CREATE TABLE Failed. [3504] Selected non-aggregate values must be part of the associated group.
You are selecting all of tableA (including the flag). You should just be pulling the date and the store since you want the sum of the flag.
SELECT date, store, SUM(flag)
FROM tableA
GROUP BY date, store