Access sql Query problem - sql

I'm not getting proper result from this query.
SELECT Qty as op,
0 as secunit
FROM tbl_master
WHERE tb_sno = 1
UNION
SELECT main_qty as op,
main_unit as secunit
FROM purchase
WHERE tb_sno = 1
AND TRN_DATE < #2011/05/14#
AND trn_sno2 <> 0
This show less one record from actual. is there any way to get actual result

Have you tried using UNION ALL? Perhaps there is a duplicate record in one of those recordsets. (making my comment an answer)

It looks like both of your queries set the exact same criteria on the tb_sno field (WHERE tb_sno = 1). Because of that, if op and sec_unit have the same resulting values in each query, a UNION will eliminate one of the duplicate rows. A UNION ALL will keep all duplicates, as fortheworld mentioned.

Related

Joining two results of sql

I have two queries and both works on two tables with similar columns,
Query 1 produces the following results, since the result has 46 rows – a part of it is shown here.
Query 2 produces the following results,
Now I want to combine then as follows, result should have more than 46 rows (depending on how many similarities they have based on WC). Both result has the SS (Session for SS) column.
This is what I’ve tried,
select ss, wc, wc_efficiency1, wc_efficiency2 from
(
Query 1
) t1
join
(
Query 2
) t2
ON t1.ss = t2.ss
But query doesn’t execute, showing errors and even if I make some changes to overcome the errors, it runs for infinite time, no output.
There are a couple ways you can approach this and it depends on whether the results from query 1 and query 2 are unique, need to appear as a single row, or need to have their efficiencies aggregated/calculated somehow.
If the results are unique (there will not be an SS + WC combination that is the same) then you can do the following:
SELECT ss, wc, wc_efficiency
FROM query1
UNION ALL
SELECT ss, wc, wc_efficiency
FROM query2
If the results are not unique, meaning you may have an SS + WC combination that is in both queries then you could do the following:
SELECT
q1.ss,
q1.wc,
q1.wc_efficiency AS [wc_efficiency1],
q2.wc_efficiency AS [wc_efficiency2]
FROM QUERY1 q1, QUERY2 q2
WHERE q1.ss = q2.ss
AND q1.wc = q2.wc
OR
SELECT
CASE
WHEN q1.ss IS NOT NULL THEN q1.ss
WHEN q2.ss IS NOT NULL THEN q2.ss
END AS [ss],
CASE
WHEN q1.wc IS NOT NULL THEN q1.wc
WHEN q2.wc IS NOT NULL THEN q2.wc
END AS [wc],
q1.wc_efficiency AS [wc_efficiency1],
q2.wc_efficiency AS [wc_efficiency2]
FROM QUERY1 q1
FULL OUTER JOIN QUERY2 q2
ON q1.ss = q2.ss AND q1.wc = q2.wc
I prefer the latter option, but they should both work similarly and you can use ISNULL to set a wc_efficiency of 0 if you don't want null values for reporting/graphing purposes...
edit: forgot that if there is something in the second query not in the first set of results you'd need to account for that, you may need a different join but can also use a case statement and I'll edit the third option above to reflect that.

What is "Select -1", and how is it different from "Select 1"?

I have the following query that is part of a common table expression. I don't understand the function of the "Select -1" statement. It is obviously different than the "Select 1" that is used in "EXISTS" statements. Any ideas?
select days_old,
count(express_cd),
count(*),
case
when round(count(express_cd)*100.0/count(*),2) < 1 then '0'
else ''
end ||
cast(decimal(round(count(express_cd)*100.0/count(*),2),5,2) as varchar(7)) ||
'%'
from foo.bar
group by days_old
union all
select -1, -- Selecting the -1 here
count(express_cd),
count(*),
case
when round(count(express_cd)*100.0/count(*),2) < 1 then '0'
else ''
end ||
cast(decimal(round(count(express_cd)*100.0/count(*),2),5,2) as varchar(7)) ||
'%'
from foo.bar
where days_old between 1 and 7
It's just selecting the number "minus one" for each row returned, just like "select 1" will select the number "one" for each row returned.
There is nothing special about the "select 1" syntax uses in EXISTS statements by the way; it's just selecting some random value because EXISTS requires a record to be returned and a record needs data; the number 1 is sufficient.
Why you would do this, I have no idea.
When you have a union statement, each part of the union must contain the same columns. From what I read when I look at this, the first statement is giving you one line for each days old value and then some stats for each day old. The second part of the union is giving you a summary of all the records that are only a week or so less. Since days old column is not relevant here, they put in a fake value as a placeholder in order to do the union. OF course this is just a guess based on reading thousands of queries through the years. To be sure, I would need to actually run teh code.
Since you say this is a CTE, to really understand why this is is happening, you may need to look at the data it generates and how that data is used in the next query that uses the CTE. That might answer your question.
What you have asked is basically about a business rule unique to your company. The true answer should lie in any requirements documents for the original creation of the code. You should go look for them and read them. We can make guesses based on our own experience but only people in your company can answer the why question here.
If you can't find the documentation, then you need to talk (Yes directly talk, preferably in person) to the Stakeholders who use the data and find out what their needs were. Only do this after running the code and analyzing the results to better understand the meaning of the data returned.
Based on your query, all the records with days_old between 1 and 7 will be output as '-1', that is what select -1 does, nothing special here and there is no difference between select -1 and select 1 in exists, both will output the records as either 1 or -1, they are doing the same thing to check whether if there has any data.
Back to your query, I noticed that you have a union all and compare each four columns you select connected by union all, I am guessing your task is to get a final result with days_old not between 1 and 7 and combine the result with day_old, which is one because you take all between 1 and 7.
It is just a grouping logic there.
Your query returns aggregated
data (counts and rounds) grouped by days_old column plus one more group for data where days_old between 1 and 7.
So, -1 is just another additional group there, it cannot be 1 because days_old=1 is an another valid group.
result will be like this:
row1: days_old=1 count(*)=2 ...
row2: days_old=3 count(*)=5 ...
row3: days_old=9 count(*)=6 ...
row4: days_old=-1 count(*)=7

SQL SELECT returns same item more than one time

I have the following SQL Command:
SELECT *
FROM Notes
INNER JOIN AuthorizedPersons
ON Notes.idPass = AuthorizedPersons.idPass
AND AuthorizedPersons.Privileged = 0
AND Notes.idUser =7
This returns the correct items! BUT returns the same item twice for each AuthorizedPerson that exists!
(Using DISTINCT does not solve the problem because items can have the same name.)
Query Results:
As you can see in the idPass 15 and 16 the description can be the same BUT idPass cannot since it's the primary key!
The query returns 3 times the idPass 30...
Try to use Where instead of the first AND.
SELECT *
FROM Notes
INNER JOIN AuthorizedPersons
ON Notes.idPass = AuthorizedPersons.idPass
WHERE AuthorizedPersons.Privileged = 0
AND Notes.idUser =7
In the table AuthorizedPersons ,column starting with 'IdUs..' repeating multiple times against the same idPass.That is why you are getting multiple rows against same value of idpass.For avoiding the duplicate records, you can either use a 'DISTINCT' keyword after excluding that particular column or you can choose any one of the record from that duplicated record by eliminating the others.

SQL WHERE subquery with additional refinements?

Is it possible to have a subquery in a WHERE statement with additional qualifiers? For instance, I have two tables on two different machines. I am trying to compare the tables and see which items only exist on one machine, but only a certain type of item. I only sync two types of items. I can run this query and it works:
select
ItemCode as style,
ISNULL(U_certno,'') as U_certno,
U_he, ISNULL(U_mold,'N') as U_mold
from ITEMS Z
where
z.itemcode <> (SELECT distinct Q.style
FROM remote.system.table Q where Q.style = Z.ItemCode)
That much works, I think. If I change the z.itemcode <> to z.itemcode =, I toggle between 0 results (sync is current), and all records. However, when I add:
and (z.U_he like 'y' or z.U_mold like 'y')
it gives me all items in the ITEMS table that have those two criteria, regardless of the subquery. I have tried all manners of parenthesis, to no avail. What am I doing wrong? I didn't bother trying a join, since I want dissimilar items, not matches. Any help would be appreciated. This is MSSQL 2008 R2.
select
ItemCode as style,
ISNULL(U_certno,'') as U_certno,
U_he, ISNULL(U_mold,'N') as U_mold
from ITEMS Z
where not exists (
select 1
from remote.system.table Q
where Q.style = Z.ItemCode
)
and 'y' IN (z.U_he, z.U_mold)

Oracle Group by issue

I have the below query. The problem is the last column productdesc is returning two records and the query fails because of distinct. Now i need to add one more column in where clause of the select query so that it returns one record. The issue is that the column i need
to add should not be a part of group by clause.
SELECT product_billing_id,
billing_ele,
SUM(round(summary_net_amt_excl_gst/100)) gross,
(SELECT DISTINCT description
FROM RES.tariff_nt
WHERE product_billing_id = aa.product_billing_id
AND billing_ele = aa.billing_ele) productdescr
FROM bil.bill_sum aa
WHERE file_id = 38613 --1=1
AND line_type = 'D'
AND (product_billing_id, billing_ele) IN (SELECT DISTINCT
product_billing_id,
billing_ele
FROM bil.bill_l2 )
AND trans_type_desc <> 'Change'
GROUP BY product_billing_id, billing_ele
I want to modify the select statement to the below way by adding a new filter to the where clause so that it returns one record .
(SELECT DISTINCT description
FROM RRES.tariff_nt
WHERE product_billing_id = aa.product_billing_id
AND billing_ele = aa.billing_ele
AND (rate_structure_start_date <= TO_DATE(aa.p_effective_date,'yyyymmdd')
AND rate_structure_end_date > TO_DATE(aa.p_effective_date,'yyyymmdd'))
) productdescr
The aa.p_effective_date should not be a part of GROUP BY clause. How can I do it? Oracle is the Database.
So there are multiple RES.tariff records for a given product_billing_id/billing_ele, differentiated by the start/end dates
You want the description for the record that encompasses the 'p_effective_date' from bil.bill_sum. The kicker is that you can't (or don't want to) include that in the group by. That suggests you've got multiple rows in bil.bill_sum with different effective dates.
The issue is what do you want to happen if you are summarising up those multiple rows with different dates. Which of those dates do you want to use as the one to get the description.
If it doesn't matter, simply use MIN(aa.p_effective_date), or MAX.
Have you looked into the Oracle analytical functions. This is good link Analytical Functions by Example