Enter the same value for the entire year - sql-server-2008

I have table Payments
Client Dt Payment
1 201311 10
1 201312 0
2 201401 0
1 201402 0
1 201403 0
And i want select where i add to this select column "OwnerFlag", where if the client has paid in that year then in all rows for that year for that client will be OwnerFlag 1, otherwise its 0. So the final select should look like :
Client Dt Payment OwnerFlag
1 201311 10 1
1 201312 0 1
2 201401 0 0
1 201402 0 0
1 201403 0 0
Thank you.

Presuming that DT is a datetime column:
SELECT Client, Dt, Payment,
OwnerFlag = CASE WHEN EXISTS
(
SELECT 1 FROM dbo.Payments p1
WHERE p1.ClientID = p.ClientID
AND YEAR(p1.Dt) = YEAR(p.Dt)
AND p1.Pament <> 0
) THEN 1 ELSE 0 END
FROM dbo.Payments p

Related

How to use count in sql based on a IF condition

From this table
groupId
flag
flagValue
1
0
500
2
0
100
1
1
10
2
1
50
3
0
100
1
1
200
3
1
1000
2
1
50
I need this result
groupId
flag1
flag0
valFlag1
valFlag0
totalFlags
1
2
1
210
500
3
2
2
1
100
100
3
3
1
1
1000
100
2
where
flag1 is number of times flag is 1 for a particular group
flag0 is number of times flag is 0 for a particular group
valFlag1 is sum of flagVal when flag is 1
valFlag0 is sum of flagVal when flag is 0
totalFlags is sum of total flags associated with a group
I am stuck as to how to actually count values based on an IF condition.
Anyhelp is appreciated. Thanks.
I have used a table named group_table with your values
Try using this:
SELECT
g.`groupId`,
SUM(g.`flag`=1 ) AS flag1,
SUM(g.`flag`=0) AS flag0,
SUM(CASE WHEN g.`flag`=1 THEN g.`flagValue` ELSE 0 END) AS valFalg1,
SUM(CASE WHEN g.`flag`=0 THEN g.`flagValue` ELSE 0 END) AS valFalg0,
COUNT(*) AS totalFlags
FROM
`group_table` g
GROUP BY g.`groupId`
If you have to use the IF,
SELECT
g.`groupId`,
IF(g.`flag`=1,1,0 ) AS flag1,
IF(g.`flag`=0,1,0) AS flag0,
SUM(IF(g.`flag`=1,g.`flagValue`,0 )) AS valFalg1,
SUM(IF(g.`flag`=0,g.`flagValue`,0 )) AS valFalg0,
COUNT(*) AS totalFlags
FROM
`group_table` g
GROUP BY g.`groupId`, flag1, flag0
They'll produce the same result

Unique & Rept Customer Sales based on date

I am new to Mysql, I have the data like this,
Order_dt User_ID Sales
28-03-2022 PRPK-12 84
28-03-2022 PRPK-11 41
28-03-2022 PRPK-10 55
28-03-2022 PRPK-12 76
26-03-2022 PRPK-10 54
27-03-2022 PRPK-11 88
27-03-2022 PRPK-11 51
27-03-2022 PRPK-10 40
27-03-2022 PRPK-10 40
& I need o/p like below the format.
Order_date Unique_Cx Unique_Cx_Sales Rept_Cx Rept_Cx_Sales
26-03-2022 1 54 0 0
27-03-2022 0 0 2 219
28-03-2022 2 96 1 160
I'm getting only unique & Rept count by using the 'Having' function, but im unable to map with the sales. Kindly help.
Thanking you.
First group by dt and user id in sub query then use conditional aggregation
select order_dt,
sum(case when cd = 1 then 1 else 0 end) unique_cx,
sum(case when cd = 1 then sales else 0 end) unique_cx_sales,
sum(case when cd > 1 then 1 else 0 end) repeat_cx,
sum(case when cd > 1 then sales else 0 end) repeat_cx_sales
from
(
select order_dt, user_id,
count(*) cd,sum(sales) sales
from t
group by order_dt,user_id
) s
group by order_dt
;

Select query which give those rows having two or more than two non-zero values

I have a MySQL table with 8 columns (id,user,a,b,c,d,e,f)
in which I have a list of distinct users with (0 through 9) values in columns like this:
Id User A B C D E F
1 0001 0 1 0 0 0 0
2 0002 0 5 8 4 6 5
3 0003 0 0 0 0 0 5
4 0004 2 6 4 5 4 1
5 0005 1 0 0 0 0 0
6 0006 7 6 5 4 0 9
7 0007 4 0 0 0 0 8]
I want a MySQL select query which gives a list of those users having two or more than two non-zero values.
So the query should return record id (2,4,6,7). Thanks
Here is another way to do so
select *
from t
where
(
(A <> 0 )+
(B <> 0) +
(C <> 0 )+
(D <> 0 )+
(E <> 0 )+
(F <> 0 )
) >= 2
Not equal to operator returns boolean(0/1) value against the criteria and you can sum up the results for each column and eliminate the records according to your criteria
DEMO
Try this.
select user
from (
SELECT *,
CASE WHEN a>0 THEN 1 ELSE 0 END +
CASE WHEN b>0 THEN 1 ELSE 0 END +
CASE WHEN c>0 THEN 1 ELSE 0 END +
CASE WHEN d>0 THEN 1 ELSE 0 END +
CASE WHEN e>0 THEN 1 ELSE 0 END +
CASE WHEN f>0 THEN 1 ELSE 0 END chk
FROM yourtable
) a
where chk>=2

How to run evaluating expression in MySQL and update column accordingly?

I have the following tables:
tweets stocks
------------------------------------- -----------------------------
id stock_id nyse_date class quality stock_id _date return
------------------------------------- -----------------------------
1 1 2011-03-12 3 1 2011-03-12 0.44
2 1 2011-03-12 1 1 2011-03-15 0.17
3 1 2011-03-15 2 2 2011-03-15 -0.03
4 2 2011-03-15 2
5 2 2011-03-16 1
I need to evaluate the following expression and fill in the result in the quality column by using solely SQL:
IF (class_value of tweet/return on that day) > 0 THEN quality = 1 ELSE 0
With class_value being:
IF class = 1 THEN class_value = 0
IF class = 2 THEN class_value = 1
IF class = 3 THEN class_value = -1
For each tweet, I need to (obviously) take the stock return of that date. During the weekend, for exapmple at 2011-03-16, there is no stock data available. In that case, quality should default to 0 as well (and not return an error). So let's do this manually for the 4 tweets in the example:
id class class_value return class_value/return quality
-----------------------------------------------------------
1 3 -1 0.44 -1/0.44 = -2.27 0
2 1 0 0.44 0/0.44 = 0 0
3 2 1 0.17 1/0.17 = 5.88 1
4 2 1 -0.03 1/-0.03 = -33.33 0
5 1 0 n/a 0
So the end result of the query then would be an updated tweets table:
tweets
-------------------------------------
id stock_id nyse_date class quality
-------------------------------------
1 1 2011-03-12 3 0
2 1 2011-03-12 1 0
3 1 2011-03-15 2 1
4 2 2011-03-15 2 0
5 2 2011-03-16 1 0
I don't know how to put all this into a query. I now have this, but it only selects the class and return but doesn't set class_value or implement the expression.
UPDATE tweets SET quality = (
SELECT t.class, s.return FROM tweets t
LEFT JOIN stocks s ON t.nyse_date = s._date
)
Any help is greatly appreciated :-)
update tweets
set quality = ifnull(case tweets.class
when 1 then 0
when 2 then 1
when 3 then -1
end
-- If there is no match, subquery will evaluate to null
-- Division will also evaluate to null
/ (select `return`
from stocks
where tweets.nyse_date = stocks._date
and tweets.stock_id = stocks.stock_id
)
-- If there was no match, quality = 0
, 0)
-- Shortcut to set 1 if condition is satisfied
-- If there is an error try encapsulating
-- whole expression into case when (ex) then 1 else 0 end
> 0
I wanted to test this, as I'm not working with MySql, but my favourite Sql Fiddle is down at the moment.

MySQL query to count non-null values in a single row

I'm trying to put together a MYSQL query that will count the number of Non-Null (or better yet, non-zero) values in select fields in a single row and then sort from lowest to highest (based on the count). For example, I have a table with 5 fields... ID, Name, Score_1, Score_2, Score_3. I want to count how many times the value "0" exists in Score_1, Score_2 and Score_3 for each record, then sort from most non zero values to least.
ID Name Score_1 Score_2 Score_3
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
4 Mike 4 5 5
I assume the query has to look something like this...
Select ID, Name, Score_1, Score_2, Score_3 where (???) ORDER BY (???)
Output should look like this (ID 4 is displayed first since it has the least amount of non-zero entries)...
ID Name Score_1 Score_2 Score_3
4 Mike 4 5 5
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
I'm somewhat new to mysql query's, so any help would be greatly appreciated. I thought the COUNT function would help, but that function appears to count columns from all rows. Perhaps there is a way to use the COUNT function and limit it to a singel row so it can be sorted by that row count?
This should do what you want:
SELECT ID, Name, Score_1, Score_2, Score_3
FROM Table1
ORDER BY (Score_1 = 0) + (Score_2 = 0) + (Score_3 = 0)
Result:
ID Name Score_1 Score_2 Score_3
4 Mike 4 5 5
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
try This:
Select id, Count1, Count2, Count3, Count4
From
(Select
Sum(Case When IsNull(Score_1,0) = 0 Then 1 Else 0 End) Count1,
Sum(Case When IsNull(Score_2,0) = 0 Then 1 Else 0 End) Count2,
Sum(Case When IsNull(Score_3,0) = 0 Then 1 Else 0 End) Count3,
Sum(Case When IsNull(Score_4,0) = 0 Then 1 Else 0 End) Count4
From Table
Group By Id) Z -- This column (Id) better not be the PK for this table!!!
Order By Count1 + Count2 + Count3 + Count4