I'm moving a project from MySQL to MSSQL. And since I have a non-standard use of grouping on the Mssql side, I can't bring up the correct data. I need a suggestion. The stok_adet field displays the wrong data. How can we overcome this? Thank you in advance for your help.
My current screenshots are as follows.
Mssql Query
SELECT sm.*,
(SELECT COUNT(*)
FROM stok_durum d
WHERE sm.stok_durum_id = sm.stok_durum_id
) as stok_adet
FROM (SELECT s.*, m.*,
ROW_NUMBER() OVER (PARTITION BY s.bundle_no, s.boy, s.yukseklik, s.hatali
ORDER BY (SELECT NULL)
) as seqnum
FROM stok s
CROSS JOIN mermer_cins m
WHERE m.mermer_cins_id = '5' AND s.blok_no = 'M6320'
) sm
WHERE seqnum = 1
ORDER BY sm.blok_no ASC, sm.bundle_no ASC, sm.stok_tarih DESC
MySql side
SELECT Count(*) AS stok_adet, s.*, m.*,d.*
FROM stok AS s
CROSS JOIN mermer_cins AS m
JOIN stok_durum AS d ON s.stok_durum_id = d.stok_durum_id
WHERE m.mermer_cins_id = '5' AND s.blok_no = 'M6320'
GROUP BY s.bundle_no, s.boy, s.yukseklik, s.hatali
ORDER BY s.blok_no ASC, s.bundle_no ASC, s.stok_tarih DESC
MsSql side
MySql side
When calculating stok_adet you have a comparison between the same column:
WHERE sm.stok_durum_id = sm.stok_durum_id
but you maybee want:
WHERE d.stok_durum_id = sm.stok_durum_id
Try:
SELECT sm.*,
(SELECT COUNT(*)
FROM stok_durum d
WHERE d.stok_durum_id = sm.stok_durum_id
) as stok_adet
FROM (SELECT s.*, m.*,
ROW_NUMBER() OVER (PARTITION BY s.bundle_no, s.boy, s.yukseklik, s.hatali
ORDER BY (SELECT NULL)
) as seqnum
FROM stok s
CROSS JOIN mermer_cins m
WHERE m.mermer_cins_id = '5' AND s.blok_no = 'M6320'
) sm
WHERE seqnum = 1
ORDER BY sm.blok_no ASC, sm.bundle_no ASC, sm.stok_tarih DESC
Related
how to change this MYSQL query to sql server
SELECT
id.value AS ICDCode,
items.itemName AS ProblemListDescription,
COUNT(*) as UsageCount
FROM problemlist pl
INNER JOIN items
ON pl.asmtId = items.itemId
LEFT OUTER JOIN itemdetail id
ON (items.itemId=id.itemId AND id.propId=13 )
WHERE (pl.SNOMED='' OR pl.SNOMED IS NULL) AND pl.deleteflag=0
group by id.value, items.itemName
ORDER BY UsageCount DESC, ICDCode ASC
LIMIT 0,10 ;
I have tried this for sql server but its throwing error
select * from
(
SELECT
id.value AS ICDCode,
items.itemName AS ProblemListDescription,
COUNT(*)as UsageCount ,
row_number() over (ORDER BY UsageCount DESC, ICDCode ASC ) as rownum
FROM problemlist pl
INNER JOIN items
ON pl.asmtId=items.itemId
LEFT OUTER JOIN itemdetail id
ON (items.itemId=id.itemId AND id.propId=13 )
WHERE (pl.SNOMED='' OR pl.SNOMED IS NULL) AND pl.deleteflag=0
group by id.value, items.itemName
) sno
WHERE rownum BETWEEN 0 AND 10 ;
error message is
column usagecount is invalid
column icdcode is invalid
what is the mistake or i have to do it in another way ? guide me
I would probably just use TOP here:
SELECT TOP 10
id.value AS ICDCode,
items.itemName AS ProblemListDescription,
COUNT(*) as UsageCount
FROM problemlist pl
INNER JOIN items
ON pl.asmtId = items.itemId
LEFT OUTER JOIN itemdetail id
ON (items.itemId=id.itemId AND id.propId=13 )
WHERE (pl.SNOMED='' OR pl.SNOMED IS NULL) AND pl.deleteflag=0
GROUP BY
id.value,
items.itemName
ORDER BY
UsageCount DESC, ICDCode
By the way, the error in your query is that you were referring to an alias in the ROW_NUMBER function, but the alias is not yet available at that point in the query. You could use the following instead:
ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC, ICDCode) AS rownum
In the following query:
SELECT
(SELECT nick FROM nicks n WHERE n.pid=p.id LIMIT 1 ORDER BY id DESC) as nick
, (
(
( SELECT COUNT(*) FROM kills k WHERE k.pid = p.id )
+
( SELECT COUNT(*) FROM votos v WHERE v.pid = p.id )
)
- (SELECT COUNT(*) FROM deaths d WHERE d.pid = p.id )
) as score
, (SELECT COUNT(*) FROM kills k WHERE k.pid = p.id ) as kills
, (SELECT COUNT(*) FROM deaths d WHERE d.pid = p.id ) as deaths
, (SELECT COUNT(*) FROM headshots h WHERE h.pid = p.id ) as headshots
, (SELECT COUNT(*) FROM votos v WHERE v.pid = p.id ) as reputation
FROM players p
WHERE p.uuid='STEAM_x:x:xxxxxx'
GROUP BY kills
This query works fine... but i think there exists a better way to do this.
Can anyone help me optimize this query?
Here is a somewhat better way to write the query:
SELECT p.*, (kills + reputation - deaths) as score
FROM (SELECT (SELECT nick FROM nicks n WHERE n.pid = p.id ORDER BY id DESC LIMIT 1
) as nick,
(SELECT COUNT(*) FROM kills k WHERE k.pid = p.id ) as kills,
(SELECT COUNT(*) FROM deaths d WHERE d.pid = p.id ) as deaths,
(SELECT COUNT(*) FROM headshots h WHERE h.pid = p.id ) as headshots,
(SELECT COUNT(*) FROM votos v WHERE v.pid = p.id ) as reputation
FROM players p
WHERE p.uuid = 'STEAM_x:x:xxxxxx'
) p
GROUP BY kills;
Note: I don't understand what the GROUP BY is doing. You are only aggregating by one column, so the rest of the columns have indeterminate values. Perhaps you intend ORDER BY.
I am guessing that the overhead for materializing the subquery before the group by is slightly less than the additional subqueries. But your version may have very comparable performance.
For either version, you want the following indexes:
players(uuid)
kills(pid)
deaths(pid)
headshots(pid)
votos(pid)
I wrote the following query to return the the records with the latest date.
select fs.company_id, max(fs.create_dt) as latestcreatedate
from field_sale fs
group by fs.company_id
order by fs.company_id
The query all works fine. But I need to retrieve the record with all related columns attached to it. Such as, id, title, desc and etc.
How can I retrieve the records with its corresponding columns?
Couple ways of doing so :
-- 1.
SELECT a.*
FROM field_sale a
INNER JOIN
(
select fs.company_id, max(fs.create_dt) as latestcreatedate
from field_sale fs
group by fs.company_id
)b
ON b.company_id = a.company_id AND b.latestcreatedate = a.create_dt
order by a.company_id;
-- 2.
SELECT b.* FROM
(
SELECT a.* , ROW_NUMBER()
OVER (PARTITION BY a.company_id ORDER BY a.create_dt DESC)
AS rn
FROM field_sale a
)b WHERE b.rn = 1
ORDER BY company_id
WITH t AS (
SELECT fs.company_id,
fs.create_dt AS latestcreatedate,
id,
title,
etc,
ROW_NUMBER() OVER ( PARTITION BY fs.company_id ORDER BY fs.create_dt DESC ) AS rowNum
FROM field_sale fs
)
SELECT t.company_id,
t.latestcreatedate,
t.id,
t.title,
t.etc
FROM t
WHERE t.rowNum = 1
ORDER BY t.company_id
I have a table called Request.
Other tables are linked to the Request table through a request id.
There is a TwitterTweet table and a FacebookPost table.
So a single request can have 50 TwitterTweets and/or 20 FacebookPosts or any amount of Tweets/Posts
We can add them together for a total count of 70.
I'm trying to create a query that could tell me what is the request with the highest total count.
I know this is wrong:
(I attempted to just order them by the counts within the TwitterTweet, but it would not let me do an OUTER JOIN which I thought
would bring back the Count.count column. It forced me to do a Left Join for it to compile. My Logic was to do a join so
that the results were calculated for each row by the requestid)
SELECT r1.`id` AS requestid, r1 . *
FROM `Request` AS r1
LEFT JOIN
(SELECT COUNT( * ) AS count, rid
FROM
((SELECT `TwitterTweet`.`id` AS `smid` , `TwitterTweet`.`requestid` AS rid
FROM `TwitterTweet`
WHERE `TwitterTweet`.`requestid` = requestid
AND `TwitterTweet`.`active` =1) AS talias
)) AS Count ON ( Count.rid = requestid )
ORDER BY Count.count
*When I tried to add in the Facebook side it would not compile any more
(The concept is that the results are added from TwitterTweet with the results from FacebookPost
that are attached to the specific requestid which would give us a count. The entire result
set should be ordered by that count)
SELECT r1.`id` AS requestid, r1 . *
FROM `Request` AS r1
LEFT JOIN
(SELECT COUNT( * ) AS count, rid
FROM
((SELECT `TwitterTweet`.`id` AS `smid` , `TwitterTweet`.`requestid` AS rid
FROM `TwitterTweet`
WHERE `TwitterTweet`.`requestid` = requestid
AND `TwitterTweet`.`active` =1 ) AS talias
UNION All
(SELECT `FacebookPost`.`id` AS `smid`, `FacebookPost`.`requestid` AS rid
FROM `FacebookPost`
WHERE `FacebookPost`.`requestid` = requestid
AND `FacebookPost`.`active` = 1) as falias
)) AS Count ON ( Count.rid = requestid )
ORDER BY Count.count
I updated the Query with an attempt to add an alias:
SELECT rid, SUM(count) total_count
FROM
(
(SELECT COUNT(*) AS count, r.rid
FROM request r
JOIN TwitterTweet tt
ON r.id = tt.requestid
WHERE tt.active = 1
GROUP BY r.rid) AS twitter
UNION ALL
(SELECT COUNT(*) AS count, r.rid
FROM request r
JOIN FacebookPost fp
ON r.id = fp.requestid
WHERE fp.active = 1
GROUP BY r.rid ) AS fbook
)
GROUP BY rid
ORDER BY SUM(count) DESC
I made another adjustment to give the middle subquery an alias, but now I only get one row returned with a zero in the rid column and 5686 in the total_count column...the 5686 might be all of the results.
SELECT counts.rid, SUM(count) total_count
FROM
(
SELECT COUNT(*) AS count, r.requestid AS rid
FROM request r
JOIN TwitterTweet tt
ON r.id = tt.requestid
WHERE tt.active = 1
GROUP BY r.requestid
UNION ALL
SELECT COUNT(*) AS count, r.requestid AS rid
FROM request r
JOIN FacebookPost fp
ON r.id = fp.requestid
WHERE fp.active = 1
GROUP BY r.requestid
) AS counts
GROUP BY counts.rid
ORDER BY SUM(count) DESC
Got it!!!
Thanks for your help guys, I had to remove those joins on the request:
SELECT counts.rid, SUM(count) total_count
FROM
(
SELECT COUNT(*) AS count, tt.requestid AS rid
FROM TwitterTweet tt
WHERE tt.active = 1
GROUP BY tt.requestid
UNION ALL
SELECT COUNT(*) AS count, fp.requestid AS rid
FROM FacebookPost fp
WHERE fp.active = 1
GROUP BY fp.requestid
) AS counts
GROUP BY counts.rid
ORDER BY SUM(count) DESC
SELECT id, SUM(count) total_count
FROM
(
SELECT COUNT(*) AS count, r.id
FROM request r
JOIN TwitterTweet tt
ON r.id = tt.requestid
WHERE tt.active = 1
GROUP BY r.id
UNION ALL
SELECT COUNT(*) AS count, r.id
FROM request r
JOIN FacebookPost fp
ON r.id = fp.requestid
WHERE fp.active = 1
GROUP BY r.id
) sub
GROUP BY id
ORDER BY SUM(count) DESC
;
How do I change the following dependent subquery to self join?
SELECT d.name, d.created,
(SELECT SUM( q1.payout ) FROM client AS q1 WHERE q1.uid = d.uid) AS payout,
(SELECT COUNT( q2.uid ) FROM client AS q2 WHERE q2.uid = d.uid AND q2.winning =1) AS cnt
FROM client AS d group by d.name, d.created ORDER BY cnt DESC LIMIT 0 , 10;
SELECT d.name, d.created, SUM(q1.payout) AS psum, COUNT(q2.uid) AS cnt
FROM client d
LEFT JOIN
client q1
ON q1.uid = d.uid
LEFT JOIN
client q2
ON q2.uid = d.uid
AND q2.winning =1
GROUP BY
d.name, d.created
ORDER BY
cnt DESC
LIMIT 0, 10
If uid is a PRIMARY KEY, you can rewrite it like this:
SELECT d.name, d.created, SUM(payout) AS psum, COUNT(IF(winning = 1, uid, NULL)) AS cnt
FROM client d
GROUP BY
d.name, d.created
ORDER BY
cnt DESC
LIMIT 0, 10
SELECT d.name, d.created, SUM(d.payout) AS allpayout, COUNT(alt.uid) as cnt
FROM client AS d
LEFT JOIN client AS alt
ON alt.uid = d.uid AND alt.winning = 1
GROUP BY d.name, d.created
ORDER BY cnt DESC
LIMIT 0, 10
SELECT d.name, d.created, SUM( d.payout ) AS payout, SUM( IF( d.winning = 1, 1, 0 ) ) AS cnt
FROM client AS d
GROUP BY
d.name, d.created
ORDER BY cnt DESC LIMIT 0 , 10;