SQL Server row_number and datatables - sql-server-2008

I'm working with SQL Server and Datatables in a PHP environment; I've researched a bit, and found that by using row_number I can achieve paged results, but I am getting:
The multi-part identifier "Flags.FlagValue" could not be bound.
SELECT dbo.PlayingCharacters.PlayerName,dbo.PlayingCharacters.CurrentLevel,dbo.PlayingCharacters.XP,(SELECT CASE WHEN dbo.PlayingCharacters.Karma >= 1 THEN 2 WHEN dbo.PlayingCharacters.Karma <= -1 THEN 1 ELSE 0 END) as Karma,ISNULL(Flags.FlagValue,0) AS Remort FROM ( SELECT row_number() OVER (ORDER BY dbo.PlayingCharacters.XP desc) AS CI_offset_row_number, dbo.PlayingCharacters.PlayerName, dbo.PlayingCharacters.CurrentLevel, dbo.PlayingCharacters.XP, (SELECT CASE WHEN dbo.PlayingCharacters.Karma >= 1 THEN 2 WHEN dbo.PlayingCharacters.Karma <= -1 THEN 1 ELSE 0 END) as Karma, ISNULL(Flags.FlagValue, 0) AS Remort FROM dbo.PlayingCharacters LEFT JOIN dbo.Flags ON dbo.Flags.OwnerID = dbo.PlayingCharacters.UserID AND Flags.FlagID = 30419 WHERE dbo.PlayingCharacters.AccountName NOT IN (SELECT DISTINCT AccountName FROM UserFlags WHERE FlagBitPosition BETWEEN 0 AND 40) AND PlayingCharacters.AccountName NOT LIKE 'DeletedFrom:%' ) AS A WHERE A.CI_offset_row_number BETWEEN (141) AND (150)
Upon examining the query, and a bit of further research I found that "AS A" near the end of the query may be the culprit.. But I am unsure. I am familiar with SQL to an extent, but with this it seems I am a fish out of water.. I cannot seem to figure out how to fix this query.
I apologize if this may have been asked prior, I found a few results regarding my error but couldn't put together any combination of answers with any success.
As a side note, this is my query before trying to add a limit/offset with row_number()
SELECT TOP 30 dbo.PlayingCharacters.PlayerName, dbo.PlayingCharacters.CurrentLevel, dbo.PlayingCharacters.XP, (SELECT CASE WHEN dbo.PlayingCharacters.Karma >= 1 THEN 2 WHEN dbo.PlayingCharacters.Karma <= -1 THEN 1 ELSE 0 END) as Karma, ISNULL(Flags.FlagValue, 0) AS Remort
FROM dbo.PlayingCharacters
LEFT JOIN dbo.Flags ON dbo.Flags.OwnerID = dbo.PlayingCharacters.UserID AND Flags.FlagID = 30419
WHERE dbo.PlayingCharacters.AccountName NOT IN (SELECT DISTINCT AccountName FROM UserFlags WHERE FlagBitPosition BETWEEN 0 AND 40) AND PlayingCharacters.AccountName NOT LIKE 'DeletedFrom:%'
ORDER BY dbo.PlayingCharacters.XP desc
Which works, but obviously doesn't perform the desired limit/offset. The code I am using to generate the query in question was grabbed from: http://codeigniter.com/forums/viewthread/160626/P10/#985759

try this one
SELECT temp.PlayerName,temp.CurrentLevel,temp.XP,
temp.Karma,temp.Remort FROM ( SELECT row_number() OVER (ORDER BY
dbo.PlayingCharacters.XP desc) AS CI_offset_row_number,
dbo.PlayingCharacters.PlayerName, dbo.PlayingCharacters.CurrentLevel,
dbo.PlayingCharacters.XP, (SELECT CASE WHEN
dbo.PlayingCharacters.Karma >= 1 THEN 2 WHEN
dbo.PlayingCharacters.Karma <= -1 THEN 1 ELSE 0 END) as Karma,
ISNULL(Flags.FlagValue, 0) AS Remort FROM dbo.PlayingCharacters
LEFT JOIN dbo.Flags ON dbo.Flags.OwnerID =
dbo.PlayingCharacters.UserID AND Flags.FlagID = 30419 WHERE
dbo.PlayingCharacters.AccountName NOT IN (SELECT DISTINCT AccountName
FROM UserFlags WHERE FlagBitPosition BETWEEN 0 AND 40) AND
PlayingCharacters.AccountName NOT LIKE 'DeletedFrom:%' ) temp WHERE
temp.CI_offset_row_number BETWEEN (141) AND (150)

Related

MySQL - join/group concat returning too many items

I've got a simple query which brings up wins, draws and losses in a head-to-head matches table.
SELECT
SUM(CASE WHEN score_w > score_m THEN 1 ELSE 0 END) AS wins_w,
SUM(CASE WHEN score_m > score_w THEN 1 ELSE 0 END) AS wins_m,
SUM(CASE WHEN score_w = score_m THEN 1 ELSE 0 END) AS draws
FROM 6dos7me3xn8
All is well. I get a single row, with the data I need as columns.
Now I want to also pull out a group concat'd list of the most recent three match dates. I tried:
SELECT
SUM(CASE WHEN mn.score_w, 0) > mn.score_m THEN 1 ELSE 0 END) AS wins_w,
SUM(CASE WHEN mn.score_m > mn.score_w THEN 1 ELSE 0 END) AS wins_m,
SUM(CASE WHEN mn.score_w = mn.score_m THEN 1 ELSE 0 END) AS draws,
GROUP_CONCAT(jn.date) AS recent
FROM 6dos7me3xn8 mn
JOIN (SELECT date FROM 6dos7me3xn8 ORDER BY date DESC LIMIT 3) jn
...but the LIMIT is having no effect, seemingly - I get all of the dates group concat'd, not just 3.
I also tried removing the JOIN and replacing the GROUP_CONCAT with
GROUP_CONCAT((SELECT date FROM 6dos7me3xn8 ORDER BY date DESC LIMIT 3)) AS recent
...but that errors with 'Subquery returns more than 1 row.'
I'm sure it's something simple, but what am I doing wrong?
If you are running MySQL 8.0, you can do this with window functions:
select
sum(score_w > score_m) as wins_w,
sum(score_m > score_w) as wins_m,
sum(score_w = score_m) as draws,
group_concat(case when rn <= 3 then date end) as recent
from (
select t.*, row_number() over(order by date desc) rn
from `6dos7me3xn8` t
) t
The subquery ranks records by descending date; we can then use that information in the outer query. Note that you don't need the case expressions: MySQL evaluates true/false conditions as 1/0 in numeric contet.
In earlier versions, the simpler approach is probably a row-limiting subquery:
select
sum(score_w > score_m) as wins_w,
sum(score_m > score_w) as wins_m,
sum(score_w = score_m) as draws,
(select group_concat(date) from (select date from `6dos7me3xn8` order by date desc limit 3) t) as recent
from `6dos7me3xn8`
You are doing a cross join. TO get the most recent three dates, you can use:
FROM (SELECT mn.*, DENSE_RANK() OVER (ORDER BY date desc) as seqnum
FROM 6dos7me3xn8 mn
) mn
WHERE seqnum <= 3
You have no ON clause to specify the relationship between the subquery and the table you're joining it to. So you get a full cross product.
You also need ORDER BY to make it return the 3 most recent dates, not any 3 dates.
SELECT
SUM(CASE WHEN mn.score_w, 0) > mn.score_m THEN 1 ELSE 0 END) AS wins_w,
SUM(CASE WHEN mn.score_m > mn.score_w THEN 1 ELSE 0 END) AS wins_m,
SUM(CASE WHEN mn.score_w = mn.score_m THEN 1 ELSE 0 END) AS draws,
GROUP_CONCAT(jn.date) AS recent
FROM 6dos7me3xn8 mn
JOIN (
SELECT DISTINCT date
FROM 6dos7me3xn8
ORDER BY date DESC
LIMIT 3
) jn ON jn.date = mn.date

How do I calculate the difference of two alias for sorting

Considering the following code:
SELECT SUM(w.valor),
SUM(CASE WHEN w.tipo = '+' THEN w.valor ELSE 0 END) AS total_credit,
SUM(CASE WHEN w.tipo = '-' THEN w.valor ELSE 0 END) AS total_debit,
w.clientUNIQUE,
c.client as cclient
FROM wallet AS w
LEFT JOIN clients AS c ON w.clientUNIQUE = c.clientUNIQUE
WHERE w.status='V'
GROUP BY w.clientUNIQUE
ORDER BY total_credit-total_debit
I'm trying to calculate the difference of two aliased calculated values for sorting purposes, but I'm getting the following error:
Reference 'total_credit' not supported (reference to group function)
What am I doing wrong and how can I order results by using the difference value between the two aliases?
You can't refer to columns by their alias in the same select expression, so there are 2 options...
Repeat the expressions in the order by (yuk):
ORDER BY
SUM(CASE WHEN w.tipo = '+' THEN w.valor ELSE 0 END) AS total_credit -
SUM(CASE WHEN w.tipo = '-' THEN w.valor ELSE 0 END) AS total_debit
Or easier on the brain and easier to maintain (DRY), order via a sub query:
select * from (
<your query without the ORDER BY>
) q
ORDER BY total_credit - total_debit

How to do a SELECT for total from beginning until the specified date in MySQL?

I have entry table:
I need to do a SELECT to receive 'Date', 'Number of entries' (in that date), 'Total number of entries until that date'.
When I do the SELECT:
SELECT e1.*,
(select count(*) from entry where date(dateCreated) <= e1.date) as Total
from (
SELECT
DATE(e.dateCreated) as "Date",
count(e.dateCreated) as "No of Entries",
sum( case when e.premium='Y' then 1 else 0 end ) as Premium,
sum( case when e.free='Y' then 1 else 0 end ) as Free,
sum( case when e.affiliateID IS NOT NULL then 1 else 0 end) as Affiliate
FROM entry e
WHERE e.competitionID=166
GROUP BY DATE(e.dateCreated)
) as e1
ORDER BY Date DESC
I've got a result table
but the column 'Total' has a wrong data.
How the correct select should be? Is this logic of select is the best and more efficient one?
Here is a demo
If it is just the 5 vs 7 that is off I think it is because that subquery in your select list, which accesses the inline view e1 (which is filtered to competitionID = 166), is not itself filtered when also utilizing the original entry table (unfiltered). You have to filter the original table to that competitionID as well.
Notice line 3 in sql below (only change)
SELECT e1.*,
(select count(*) from entry where date(dateCreated) <= e1.date
and competitionID=166) as Total
from (
SELECT
DATE(e.dateCreated) as "Date",
count(e.dateCreated) as "No of Entries",
sum( case when e.premium='Y' then 1 else 0 end ) as Premium,
sum( case when e.free='Y' then 1 else 0 end ) as Free,
sum( case when e.affiliateID IS NOT NULL then 1 else 0 end) as Affiliate
FROM entry e
WHERE e.competitionID=166
GROUP BY DATE(e.dateCreated)
) as e1
ORDER BY Date DESC
Fiddle - http://sqlfiddle.com/#!9/e5e88/22/0

Mysql IF statement return issues

I have this query where the IF lines are only partially returning correct result:
SELECT
COALESCE((SELECT COUNT(ID) FROM USERS_BUCKETS WHERE USERS_BUCKETS.USERID = USERS.ID),0) AS ADDED,
COALESCE((SELECT SUM(CASE WHEN USERS_BUCKETS.STATUS='Completed' THEN 1 ELSE 0 END) FROM USERS_BUCKETS WHERE USERS_BUCKETS.USERID = USERS.ID),0) AS DONE,
COALESCE((SELECT COUNT(ID) FROM USERS_LIKES WHERE USERS_LIKES.USERID = USERS.ID),0) AS NUM_LIKES,
COALESCE((SELECT COUNT(ID) FROM FOLLOW WHERE FOLLOW.USER_ID=USERS.ID),0) AS FOLLOWING,
COALESCE((SELECT COUNT(ID) FROM FOLLOW WHERE FOLLOW.FOLLOW_ID=USERS.ID),0) AS FOLLOWERS,
(SELECT IF(ADDED >= 5,1,0)) AS IFADDED,
(SELECT IF(DONE >= 3,1,0)) AS IFDONE,
(SELECT IF(NUM_LIKES >= 5,1,0)) AS IFNUM_LIKES,
(SELECT IF(FOLLOWING >= 5,1,0)) AS IFFOLLOWING,
(SELECT IF(FOLLOWERS >= 3,1,0)) AS IFFOLLOWERS,
(SELECT IF(ADDED >= 5,1,0) + IF(DONE >= 3,1,0) + IF(NUM_LIKES >= 5,1,0) + IF(FOLLOWING >= 5,1,0) + IF(FOLLOWERS >= 3,1,0)) AS PROGRESS
FROM USERS
WHERE USERS.ID=?
Result:
ADDED: 20
IFADDED: 1
DONE: 9
IFDONE: 0 //should be 1
NUM_LIKES: 11
IFNUM_LIKES: 1
FOLLOWING: 11
IFFOLLOWING: 0 //should be 1
FOLLOWERS: 10
IFFOLLOWERS: 0 //should be 1
PROGRESS: 2
What's wrong? All IF values should be 1 and PROGRESS should be 5.
Note: I am using PDO in php, but I don't think that matters at all.
I have no idea why your code is working. The columns are definitely not the column aliases defined earlier in the query.
To do what you want, use a subquery and simplify the logic:
SELECT u.*, (ADDED >= 5) AS IFADDED,
(DONE >= 3) AS IFDONE,
(NUM_LIKES >= 5) AS IFNUM_LIKES,
(FOLLOWING >= 5) AS IFFOLLOWING,
(FOLLOWERS >= 3) AS IFFOLLOWERS,
((ADDED >= 5) + (DONE >= 3) + (NUM_LIKES >= 5) + (FOLLOWING >= 5) +
(FOLLOWERS >= 3)
) AS PROGRESS
FROM (SELECT COALESCE((SELECT COUNT(ID) FROM USERS_BUCKETS WHERE USERS_BUCKETS.USERID = USERS.ID),0) AS ADDED,
COALESCE((SELECT SUM(CASE WHEN USERS_BUCKETS.STATUS='Completed' THEN 1 ELSE 0 END) FROM USERS_BUCKETS WHERE USERS_BUCKETS.USERID = USERS.ID),0) AS DONE,
COALESCE((SELECT COUNT(ID) FROM USERS_LIKES WHERE USERS_LIKES.USERID = USERS.ID),0) AS NUM_LIKES,
COALESCE((SELECT COUNT(ID) FROM FOLLOW WHERE FOLLOW.USER_ID=USERS.ID),0) AS FOLLOWING,
COALESCE((SELECT COUNT(ID) FROM FOLLOW WHERE FOLLOW.FOLLOW_ID=USERS.ID),0) AS FOLLOWERS
FROM USERS
WHERE USERS.ID = ?
) u;
This takes advantage of the fact that MySQL treats booleans as integers in a numeric context, with "1" for true and "0" for false.
EDIT: I should also note that the coalesce() is unnecessary because count() will return 0 when there are no matches.

SQL statement for GROUP BY

I am really stucked with one sql select statement.
This is output/result which I get from sql statement below:
WHAT I need: I need to have columns assignedVouchersNumber and usedVouchersNumber together in one row by msisdn. So for example if you can see "msisdn" 723709656 there are two rows now.. one with assignedVouchersNumber = 1 and second with assignedVouchersNumber = 1 too.
But I need to have it in one row with assignedVouchersNumber = 2. Do you now where is the problem?
SELECT eu.msisdn,
eu.id as userId,
sum(case ev.voucherstate when '1' then 1 else 0 end) as assignedVouchersNumber,
sum(case ev.voucherstate when '2' then 1 else 0 end) as usedVouchersNumber,
ev.extra_offer_id,
ev.create_time,
ev.use_time,
ev.id as voucherId,
ev.voucherstate
FROM extra_users eu
JOIN (SELECT sn.msisdn AS telcislo,
stn.numberid
FROM stats_number sn
JOIN stats_target_number AS stn
ON ( sn.numberid = stn.numberid )
WHERE stn.targetid = 1) xy
ON eu.msisdn = xy.telcislo
JOIN extra_vouchers AS ev
ON ( eu.id = ev.extra_user_id )
WHERE ev.create_time BETWEEN '2012-07-23 00:00:00' AND '2013-08-23 23:59:59'
AND ev.use_time <= '2013-08-23 23:59:59'
AND ev.use_time >= '2012-07-23 00:00:00'
AND ev.voucherstate IN ( 1, 2 )
AND Ifnull(ev.extra_offer_id IN( 2335, 3195, 30538 ), 1)
GROUP BY eu.msisdn, ev.extra_offer_id, ev.voucherState
ORDER BY eu.msisdn ASC
You have two different extra_offer_id for same msisdn and VouchersNumber. Thats why you get two rows.
I got it... there should not be groupping by ev.voucherState in
GROUP BY eu.msisdn, ev.extra_offer_id, ev.voucherState
After then I have removed ev.voucherState it is working now.