|
It is not clear that what is your problem. As i understood your problem is to view both results of those query in a single output. To that you have to use 'UNION' .
select * from tblPackages where id = 1 and code = 58
UNION
select * from tblPackages where id = 3 and code = 31
It'll produce this output:
package id value code
------- ---- ------ ------
pak 1 | 1 | 0 | 58
pak 1 | 3 | 10 | 31
You have to use 'DISTINCT' to eliminate duplicate entries.
You can also try using JOIN but I can't understand what is you problem.
|
|
|
|
|
Hi,
You didn't quite specify what relates those to rows so I assume it's the package. In that case could have something like:
SELECT *
FROM tblPackages main
WHERE Id IN (1,3)
AND Code IN (58, 31)
AND Package = (SELECT DISTINCT Package
FROM (SELECT Package
FROM tblPackages
WHERE id = 1
AND code = 58) a,
(SELECT Package
FROM tblPackages
WHERE id = 3
AND code = 31) b
WHERE a.Package = b.Package) Don't mind about typos etc, it's not tested at all.
In the above if you can have several matching package use IN instead of equality for the package comparison. Also the statement can be simplified so this is just one version.
|
|
|
|
|
Huh? I think this[^] holds the answer, and
... WHERE (id = 1 AND code = 58) OR (id = 3 AND code = 31)
should do it.
CodingLover wrote: all the package details that id = 1 / code = 58 and id = 3 / code = 31
I guess your problem is linguistic: the "and" in the above sentence isn't really an "and" (a row couldn't have ID=1 AND ID=3 at the same time) it is more of an "and also", which actually indicates an "or" situation.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Wouldn't that lead to a situation where the row
pak 3 | 3 | 8 | 31 is included? I got the impression that it should be eliminated from the result.
|
|
|
|
|
yes it would.
however I found "so I want to find all the package details that id = 1 / code = 58 and id = 3 / code = 31. So the package should be 'pak 1'" a bit confusing, it would result in two rows IMO.
The enquirer will have to make up his mind and tell us what he really wants.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Luc Pattyn wrote: The enquirer will have to make up his mind and tell us what he really wants
That's true
|
|
|
|
|
Hi,
Is it possible to dynamically change column name in sql select statement
eg:
DECLARE @Currency NVARCHAR(50)
SET @Currency ='USD'
Select Col1 as Currency+@Currency ,col2 from tbl_Emp
|
|
|
|
|
As far as I am aware no, unless you create a dynamic sql statement.
Further Reading for dynamic SQL by Erland Sommarskog[^]
As barmey as a sack of badgers
Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.
|
|
|
|
|
Yup, you can change the name using an alias. Make sure that it doesn't contain spaces and stuff - Sql isn't the correct place to change the fieldname into a headername.
--edit; example added
DECLARE @Currency VARCHAR(50)
SET @Currency =' USD'
SELECT CAST(Currency AS VARCHAR) + @Currency AS [Currency]
FROM tbl_Emp
I are Troll
|
|
|
|
|
Eddy Vluggen wrote: Sql isn't the correct place to change the fieldname into a headername
Sure it is. You should have the SQL do as much as possible and avoid having to do work in code. That's one of the functions of views.
|
|
|
|
|
Not my downvote, I appreciate sarcasm
It'd be hard to globalize, since Sql server would have to know the culture of the client.
I are Troll
|
|
|
|
|
Eddy Vluggen wrote: Not my downvote
I didn't think so.
Eddy Vluggen wrote: I appreciate sarcasm
As do I, but none was intended.
Eddy Vluggen wrote: hard to globalize
I don't see the problem, and I was making a general response to your general statement.
|
|
|
|
|
PIEBALDconsult wrote: I didn't think so.
I rather see a discussion than a mere anonymous judgement.
PIEBALDconsult wrote: As do I, but none was intended.
My apologies, but your statement does require some additional musings on your motivation. The downvote indicates that someone didn't understand your point of view, whereas they might have agreed if they could read why you take that standpoint.
PIEBALDconsult wrote: I don't see the problem
It's not "a problem"; it's an approach like many, with it's own merits. I don't like accented letters and spaces in a columnname (like Scheisse in German or Ohm in Hindi, because it messes with the report-generator and the ORM) I know that Sql Server can use long names, just like DOS moved from 8.3 to long filenames. That doesn't mean that other vendors' updated their products and also handle long names.
It's not hard to fetch a list of translated names. The endusers all have powerfull PC's that are mostly used for surfing, so I like to offload some of the work to the clients. The clients' PC knows well enough in what formatting the client his dates and doubles wants. There's also a chance that your data is being reused in multiple platforms; so, Sql Server would have to know whether it can use the full columnname "Phone number" or the abbreviated one "Phone" for the Mobile-platform.
I can also understand why you'd put as much logic as possible in your dataserver; give one the chance to update part of the logic by changing things in the server, without the need for recompilation of redistribution. You'll have another way of translating those headers; so either way it'll be a solved problem.
..now I'm curious to how you overcame said obstacles in Oracle.
I are Troll
|
|
|
|
|
PIEBALDconsult wrote: Sure it is. You should have the SQL do as much as possible and avoid having to do work in code. That's one of the functions of views.
I would say that without qualification that is not true.
If I have a requirement that an Oracle proc should produce a report file then the point of that proc is in fact dedicated to correctly producing a format that represents a 'report'. So it must of course provide headers.
But if I am writing a java/C# application whose point is to produce a report then the java code should provide the format. The java/C# code should provide the headers. Not the SQL.
And for headers that require spaces it requires a a quoted identifier on a view. I would never do that. I would rather write a view to support the data constraints and provide additional functionality that provides the actual formatting.
|
|
|
|
|
jschell wrote: I would say that without qualification that is not true.
I'm not sure what you're saying -- are you saying I should qualify my statement? I said "should ... as much as possible", that's the qualification.
jschell wrote: producing a format that represents a 'report'. So it must of course provide
headers.
Exactly.
jschell wrote: if I am writing a java/C# application whose point is to produce a report
That's generally a rather poor scheme, unless the reports are very specialized and something like Crystal ( ) won't do. For most reports a general report engine is a far better technique. I do that to output XML of complex data.
I wrote such a reporting system on my last job and there is one at my new job. Neither has the ability to alter the column headings so it must all be done in the query. The engine simply outputs the contents of a DataTable. I can add the ability to mine a couple of different ways.
|
|
|
|
|
My upvote, I like explanation
PIEBALDconsult wrote: I wrote such a reporting system on my last job and there is one at my new job. Neither has the ability to alter the column headings so it must all be done in the query. The engine simply outputs the contents of a DataTable.
Another good reason to do put formatted data and columnnames in the table.
I are Troll
|
|
|
|
|
PIEBALDconsult wrote: That's generally a rather poor scheme, unless the reports are very specialized and something like Crystal ( ) won't do.
Presumably you are comparing the difference between creating a report engine versus using an existing commercial/free one.
In either case the engine itself contains the headers, while the data comes from the database.
PIEBALDconsult wrote: <layer>I wrote such a reporting system on my last job and there is one at my new job. Neither has the ability to alter the column headings so it must all be done in the query. The engine simply outputs the contents of a DataTable. I can add the ability to mine a couple of different ways.
I have written report engines and used several others including Crystal.
Whether the headers need to be modified (presumably by the user) has nothing to do with where they originate.
Nor does it have anything to do with what customizable features are presented to the user.
What is has to do with is separation of concerns. Your argument could lead to the supposition that one should use quoted identifiers for most tables because most tables will be used in a report and so one might as well start with the report header name.
|
|
|
|
|
I think you have pretty good justifications for handling the names in the query, so 5 from me.
|
|
|
|
|
Table [Sold]
ID PID Qty Date
-- --- --- ---------
1 1 8 jun 1, 2010
2 3 5 jul 1, 2010
3 3 2 aug 1, 2010
4 1 1 sep 1, 2010
5 3 4 sep 30, 2010
6 2 3 oct 8, 2011
7 2 5 nov 1, 2011
8 3 2 dec 1, 2011
9 1 8 jan 1, 2011
10 2 5 feb 1, 2011
Hi all,
I have difficulty composing a rather complex (for me) query. I would like to know the average qty sold of each PID per day based on the last 6-month data. If data is less than 6 month, then adjust accordingly.
So on the data above:
Today: Mar 24, 2011
6-month cut-off: Sep 24, 2010
I want to get:
PID Qty/day
--- -------
1 8/182 = 0.0440
2 (3+5+5)/168 = 0.0774 -> 168: because the oldest sale is oct 8
3 (4+2)/182 = 0.0330
Is there any way I can do this? Somebody help me, please? Thanks in advance.
|
|
|
|
|
select PID
,sum(qty)/(sysdate-min(date)) <<-- fixed this to account for sign.
from (
select PID
,qty
,date
from your_table
where date >= sysdate - 182
)
group by PID;
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
modified on Thursday, March 24, 2011 9:13 AM
|
|
|
|
|
Thanks a lot. Your answer pointed me to the answer I am looking for.
|
|
|
|
|
If there is only one entry for a PID and the sell was today, then date == sysdate
--> Division by zero ...
|
|
|
|
|
But good catch. Off the top of my head, it was the best I could figure out. Implementation details are left to the user to decide upon.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
|
|
|
|
|
But wouldn't this be worth a vote ...
|
|
|
|
|
Assuming you are using you are using sql server
SELECT
PID,
AVG(qty)/DATEDIFF (d,MAX(date), MIN(date))
FROM TableName
WHERE date > DATEADD(-6, GETDATE())
GROUP BY pid
The where clause may not be as accurate as you need.
I build this by breaking the requirements down into parts
Get the average and no of days for each pid
do the calc
Then I combined the 2 queries. Note this is untested code.
Never underestimate the power of human stupidity
RAH
|
|
|
|