What is the query for table 3? - sql-server

I have extracted these two tables from a SQL Database:
query for table 1:
SELECT POM_DOCNO,
POM_DATE,
SUP_CODE,
POM_CREATEDBY
FROM SI_PURORDERMASTER
WHERE POM_YEAR = 2012
AND POM_PERIOD = 6
query for table 2:
SELECT POM_DOCNO,
ITM_ITEMCODE,
ITM_ITEMDESC,
POD_QTY,
POD_RATE
FROM SI_PURORDERDETAIL
WHERE POM_YEAR = 2012
AND POM_PERIOD = 6
query to get table 3 ?
I have tried using joins but always end up with wrong result :/
Is there anyway to get table 3 with just table 1 & 2 ?
Both table 1 and 2 have "POM_DOCNO" column in common.

Try something like that;
SELECT TOP 5
SIR.POM_DOCNO,
SIR.POM_DATE,
SIR.SUP_CODE,
SIL.ITM_ITEMCODE,
SIL.POD_QTY,
SIL.POD_RATE,
SIR.POM_CREATEDBY
FROM SI_PURORDERMASTER SIR inner join SI_PURORDERDETAIL SIL ON SIR.POM_DOCNO = SIL.POM_DOCNO
WHERE SIR.POM_YEAR = 2012
AND SIR.POM_PERIOD = 6
Also, just use TOP to limit results. If you want to specift order you should use order by

Related

How to return just one result from SELECT CASE query?

i have a table like this
DBName p_server_fqdn p_server_alias q_server_fqdn q_server_alias
cube1 server1.com p1server.com server5.com q1server.com
cube1 server2.com p1server.com server6.com q1server.com
cube2 server3.com p2server.com server7.com q2server.com
cube2 server4.com p2server.com server8.com q2server.com
I want to run a case select query in which i get the alias of a server input that matches a server column with corresponding DBName
this is what im trying so far
$SAlias = Invoke-sqlcmd -Query "SELECT DISTINCT CASE
WHEN ($cubeTable.DBName like $CUBE_input) AND ($cubeTable.p_server_fqdn) like $server_input THEN p_server_alias
WHEN ($cubeTable.DBName like $CUBE_input) AND ($cubeTable.q_server_fqdn) like $server_input THEN q_server_alias
ELSE 'unknown'
END as SAlias
FROM table $cubeTable" -ConnectionString "connectionstuff" | Select -ExpandProperty SAlias
but when i try the query itself in SSMS (with hardcoded values like cube1 and server2.com), i get back 2 rows with the row that dont match the DBName as "unknown" while 1 row shows p_server_alias
result im getting:
i should only get back the 1st row: p1server.com in this case, so why am i also getting unknown?
set #cubeInput = 'cube1';
set #serverInput = 'server6.com';
select
case when count(*) = 0 then 'UNKNOWN'
when m.p_server_fqdn = #serverInput then m.p_server_alias
when m.q_server_fqdn = #serverInput then m.q_server_alias
end as alias
from mytable m
where DBName = #cubeInput and (
p_server_fqdn = #serverInput
or q_server_fqdn = #serverInput
);
here is the implementation of my answer : http://sqlfiddle.com/#!9/b967a22/61
#Cataster solution return 2 rows becouse actualy he get 4 rows (3 rows 'unkown' and 1 row 'p1server.com') then he put distinct in the query. it's make result become 2 rows.
my solution little bit tricky :). Using filter in the query. than if we get no row as the result use the count function. So we get 1 row and the value is 0 than show it as 'UNKNOWN'.

Microsoft SQL Server: wrong query execution plan taking too long

This is on Windows SQL Server Cluster.
Query is coming from 3rd party application so I can not modify the query permanently.
Query is:
DECLARE #FromBrCode INT = 1001
DECLARE #ToBrCode INT = 1637
DECLARE #Cdate DATE = '31-mar-2017'
SELECT
a.PrdCd, a.Name, SUM(b.Balance4) as Balance
FROM
D009021 a, D010014 b
WHERE
a.PrdCd = LTRIM(RTRIM(SUBSTRING(b.PrdAcctId, 1, 8)))
AND substring(b.PrdAcctId, 9, 24) = '000000000000000000000000'
AND a.LBrCode = b.LBrCode
AND a.LBrCode BETWEEN #FromBrCode AND #ToBrCode
AND b.CblDate = (SELECT MAX(c.CblDate)
FROM D010014 c
WHERE c.PrdAcctId = b.PrdAcctId
AND c.LBrCode = b.LBrCode
AND c.CblDate <= #Cdate)
GROUP BY
a.PrdCd, a.Name
HAVING
SUM(b.Balance4) <> 0
ORDER BY
a.PrdCd
This particular query is taking too much time to complete execution. The same problem happens on a different SQL Server.
No table lock was found, processor and memory usage is normal while the query is running.
Normal "select top 1000" working and showing output instantly in both tables (D009021, D010014)
Reindex and rebuild / update stats done in both tables but problem did not resolve (D009021, D010014)
The same query is working if we reduce number of branch but slowly
(
DECLARE #FromBrCode INT =1001
DECLARE #ToBrCode INT =1001
)
The same query is working faster giving output within 2 mins if we replace any one variable and use the value directly
AND a.LBrCode BETWEEN #FromBrCode AND #ToBrCode
changed to
AND a.LBrCode BETWEEN 1001 AND #ToBrCode
The same query is working faster and giving output within 2 mins if we add "OPTION (RECOMPILE)" at end
I tried to clean cache query execution plan and optimized new one but problem still exists
Found that the query estimate plan and actual execution plan are different (see screenshots)
Table D010014 is aliased twice once as b and once as c
the they are joined to the same table.
Try toto remove the sub query below and create a temp table to store
the values you need. I added * to the fields you self join
SELECT MAX(c.CblDate)
FROM D010014 c
WHERE c.PrdAcctId = b.PrdAcctId
AND c.LBrCode = b.LBrCode
AND c.CblDate <= #Cdate
if you cant do that then try
SELECT TOP 1 c.CblDate
FROM D010014 c
WHERE c.PrdAcctId = b.PrdAcctId
AND c.LBrCode = b.LBrCode
AND c.CblDate <= #Cdate
ORDER BY c.CblDate DESC

Coldfusion query returns no records when exact same query returns records in winSQL

There are two tables: u_case and schedule
There is a one to many relationship between u_case and schedule on u_case.cs_caseid = schedule.sd_caseid
Database is SqlServer
I need all of the records with 13 in u_case.cs_chapter
but do not have a "dline" in u_schedule.sd_class or "pln13" in schedule.sd_type.
This EXACT query works in WinSQL and returns 22 records.
The same EXACT query returns 2 records when using ColdFusion (verified with cfdump)
Are there any suggestions on why Coldfusion has a problem with this and how to fix it? I have found a few queries with this problem.
SELECT a.cs_caseid, a.cs_case_number, a.cs_date_filed, a.cs_short_title, a.cs_office, a.cs_type
FROM u_case a
WHERE a.cs_chapter = 13
AND a.cs_date_term is null
AND 0 = (
select count(b.sd_caseid)
from schedule b
WHERE b.sd_caseid = a.cs_caseid
AND b.sd_class = "dline"
and b.sd_type = "pln13"
)
I have this issue on three servers (2 CF10 and 1 CF9). I also have other queries that this is happening to...often the CF query returns no records.
Thank you in advance.
I wonder if CF is just having trouble parsing the strange-ish SQL format you have there? Try this:
SELECT a.cs_caseid, a.cs_case_number, a.cs_date_filed, a.cs_short_title, a.cs_office, a.cs_type
FROM u_case a
WHERE a.cs_chapter = 13
AND a.cs_date_term is null
AND NOT EXISTS (
SELECT 1
FROM schedule b
WHERE b.sd_caseid = a.cs_caseid
AND b.sd_class = "dline"
AND b.sd_type = "pln13"
)

Update Weight in magento 1.9 on mass

I am trying to create a MySQL statement that I can put in a php script to update the weight on a few thousand products in magento 1.9.
This is the statement I currently have:
UPDATE dp_catalog_product_entity_decimal AS ped JOIN dp_eav_attribute AS ea ON ea.entity_type_id = 10 AND ea.attribute_code = 'weight' AND ped.attribute_id = ea.attribute_id SET ped.value = 8 WHERE ped.entity_id = P1000X3;
It was partially taken from another post so I am not sure if it will work at all but I currently have the error "#1054 - Unknown column 'P1000X3' in 'where clause'"
I am not that great with sql joins and I dont really know the magento databse at all so any help to get this statement to work is much apreciated.
Thanks.
Matt
The correct SQL to update the weight of a product with a specific SKU in Magento is this (replace YOUR-MAGENTO-SKU with the sku of the item you want to update, and the 8 with the weight value).
UPDATE catalog_product_entity_decimal AS cped
JOIN eav_attribute AS ea ON ea.entity_type_id = (SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product')
LEFT JOIN catalog_product_entity AS cpe ON cpe.entity_id = cped.entity_id
AND ea.attribute_code = 'weight'
AND cped.attribute_id = ea.attribute_id
SET cped.value = 8
WHERE cpe.sku = 'YOUR-MAGENTO-SKU';
In your case, you have the table prefix dp_, and the product sku P1000X3, so the correct SQL for you would be:
UPDATE dp_catalog_product_entity_decimal AS cped
JOIN dp_eav_attribute AS ea ON ea.entity_type_id = (SELECT entity_type_id FROM dp_eav_entity_type WHERE entity_type_code = 'catalog_product')
LEFT JOIN dp_catalog_product_entity AS cpe ON cpe.entity_id = cped.entity_id
AND ea.attribute_code = 'weight'
AND cped.attribute_id = ea.attribute_id
SET cped.value = 8
WHERE cpe.sku = 'P1000X3';
After running this, be sure to reindex your Magento Indexes.
Do your Magento tables have the prefix dp_? Make sure of this.
Also on this part:
WHERE ped.entity_id = P1000X3;
ped.entity_id will be an integer value (number).
I'm not sure where you got P1000X3, but using that won't work (it's a string value). Even so, strings should be wrapped with a single quotes ', like this:
'P1000X3';

Update rows from one table to another table based on Id [duplicate]

This question already has answers here:
How do I UPDATE from a SELECT in SQL Server?
(38 answers)
Closed 4 years ago.
Both tables have a client_id column. Need to insert data from #LocalDashboardtable into T004_Dashboard when the client_id column are equal. i have tried this but it does't help shows an error "Incorrect syntax near ','. "
update T004_Dashboard set T004_Dashboard.[GrossCharge],T004_Dashboard.[NetCharge]
= (select #LocalDashboardtable.[GrossCharge] , #LocalDashboardtable.[NetCharge]
from #LocalDashboardtable where
#LocalDashboardtable.client_id =T004_Dashboard.client_id and
#LocalDashboardtable.[month] =T004_Dashboard.[month]
and #LocalDashboardtable.[year] =T004_Dashboard.[year] )
pls help me
This is your query (which looks a lot like SQL Server):
update T004_Dashboard
set T004_Dashboard.[GrossCharge],
T004_Dashboard.[NetCharge] = (select #LocalDashboardtable.[GrossCharge], #LocalDashboardtable.[NetCharge]
from #LocalDashboardtable
where #LocalDashboardtable.client_id = T004_Dashboard.client_id and
#LocalDashboardtable.[month] = T004_Dashboard.[month] and
#LocalDashboardtable.[year] = T004_Dashboard.[year]
);
You cannot set a pair of columns to a pair of columns in a subquery. Instead, use a join:
update T004_Dashboard
set GrossCharge = ld.GrossCharge,
NetCharge = ld.NetCharge
from T004_Dashboard d join
#LocalDashboardtable ld
on ld.[month] = d.[month] and ld.[year] = d.[year] and
ld.client_id = d.client_id;
Also, SQL Server does not allow qualified column names in update/set statements

Resources